Skip to content

Commit 72993e7

Browse files
committed
feat: allow specifying exactly which db file to sync with
1 parent 49b2026 commit 72993e7

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

aw-datastore/src/worker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl DatastoreWorker {
171171
Ok((req, res_sender)) => (req, res_sender),
172172
Err(_) => {
173173
// All references to responder is gone, quit
174-
info!("DB worker quitting");
174+
debug!("DB worker quitting");
175175
self.quit = true;
176176
break;
177177
}
@@ -205,7 +205,7 @@ impl DatastoreWorker {
205205
break;
206206
};
207207
}
208-
info!("DB Worker thread finished");
208+
debug!("DB Worker thread finished");
209209
}
210210

211211
fn handle_request(

aw-sync/src/main.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate serde_json;
1515

1616
use std::error::Error;
1717
use std::path::Path;
18+
use std::path::PathBuf;
1819

1920
use chrono::{DateTime, Datelike, TimeZone, Utc};
2021
use clap::{Parser, Subcommand};
@@ -41,10 +42,15 @@ struct Opts {
4142
/// Convenience option for using the default testing host and port.
4243
#[clap(long)]
4344
testing: bool,
44-
/// Path to sync directory.
45+
/// Full path to sync directory.
4546
/// If not specified, exit.
4647
#[clap(long)]
4748
sync_dir: String,
49+
/// Full path to sync db file
50+
/// Useful for syncing buckets from a specific db file in the sync directory.
51+
/// Must be a valid absolute path to a file in the sync directory.
52+
#[clap(long)]
53+
sync_db: Option<String>,
4854
}
4955

5056
#[derive(Subcommand)]
@@ -90,6 +96,10 @@ fn main() -> Result<(), Box<dyn Error>> {
9096
};
9197
info!("Using sync dir: {}", sync_directory.display());
9298

99+
if let Some(sync_db) = &opts.sync_db {
100+
info!("Using sync db: {}", sync_db);
101+
}
102+
93103
let port = if opts.testing && opts.port == DEFAULT_PORT {
94104
"5666"
95105
} else {
@@ -121,8 +131,20 @@ fn main() -> Result<(), Box<dyn Error>> {
121131
.as_ref()
122132
.map(|b| b.split(',').map(|s| s.to_string()).collect());
123133

134+
let sync_db: Option<PathBuf> = opts.sync_db.as_ref().map(|db| {
135+
let db_path = Path::new(db);
136+
if !db_path.is_absolute() {
137+
panic!("Sync db path must be absolute");
138+
}
139+
if !db_path.starts_with(sync_directory) {
140+
panic!("Sync db path must be in sync directory");
141+
}
142+
db_path.to_path_buf()
143+
});
144+
124145
let sync_spec = sync::SyncSpec {
125146
path: sync_directory.to_path_buf(),
147+
path_db: sync_db,
126148
buckets: buckets_vec,
127149
start,
128150
};

aw-sync/src/sync.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub enum SyncMode {
3131
pub struct SyncSpec {
3232
/// Path of sync folder
3333
pub path: PathBuf,
34+
/// Path of sync db
35+
/// If None, will use all
36+
pub path_db: Option<PathBuf>,
3437
/// Bucket IDs to sync
3538
pub buckets: Option<Vec<String>>,
3639
/// Start of time range to sync
@@ -43,6 +46,7 @@ impl Default for SyncSpec {
4346
let path = Path::new("/tmp/aw-sync").to_path_buf();
4447
SyncSpec {
4548
path,
49+
path_db: None,
4650
buckets: None,
4751
start: None,
4852
}
@@ -60,7 +64,11 @@ pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Resul
6064

6165
// FIXME: Bad device_id assumption?
6266
let ds_localremote = setup_local_remote(sync_spec.path.as_path(), device_id)?;
63-
let remote_dbfiles = find_remotes_nonlocal(sync_spec.path.as_path(), device_id);
67+
let remote_dbfiles = find_remotes_nonlocal(
68+
sync_spec.path.as_path(),
69+
device_id,
70+
sync_spec.path_db.as_ref(),
71+
);
6472

6573
// Log if remotes found
6674
// TODO: Only log remotes of interest
@@ -127,7 +135,7 @@ pub fn list_buckets(client: &AwClient, sync_directory: &Path) -> Result<(), Stri
127135
let device_id = info.device_id.as_str();
128136
let ds_localremote = setup_local_remote(sync_directory, device_id)?;
129137

130-
let remote_dbfiles = find_remotes_nonlocal(sync_directory, device_id);
138+
let remote_dbfiles = find_remotes_nonlocal(sync_directory, device_id, None);
131139
info!("Found remotes: {:?}", remote_dbfiles);
132140

133141
// TODO: Check for compatible remote db version before opening
@@ -177,11 +185,15 @@ fn find_remotes(sync_directory: &Path) -> std::io::Result<Vec<PathBuf>> {
177185
}
178186

179187
/// Returns a list of all remotes, excluding local ones
180-
fn find_remotes_nonlocal(sync_directory: &Path, device_id: &str) -> Vec<PathBuf> {
188+
fn find_remotes_nonlocal(
189+
sync_directory: &Path,
190+
device_id: &str,
191+
sync_db: Option<&PathBuf>,
192+
) -> Vec<PathBuf> {
181193
let remotes_all = find_remotes(sync_directory).unwrap();
182-
// Filter out own remote
183194
remotes_all
184195
.into_iter()
196+
// Filter out own remote
185197
.filter(|path| {
186198
!(path
187199
.clone()
@@ -190,6 +202,14 @@ fn find_remotes_nonlocal(sync_directory: &Path, device_id: &str) -> Vec<PathBuf>
190202
.unwrap()
191203
.contains(device_id))
192204
})
205+
// If sync_db is Some, return only remotes in that path
206+
.filter(|path| {
207+
if let Some(sync_db) = sync_db {
208+
path.starts_with(sync_db)
209+
} else {
210+
true
211+
}
212+
})
193213
.collect()
194214
}
195215

@@ -231,7 +251,10 @@ fn get_or_create_sync_bucket(
231251
serde_json::json!(bucket_from.hostname),
232252
);
233253
ds_to.create_bucket(&bucket_new).unwrap();
234-
ds_to.get_bucket(new_id.as_str()).unwrap()
254+
match ds_to.get_bucket(new_id.as_str()) {
255+
Ok(bucket) => bucket,
256+
Err(e) => panic!("{e:?}"),
257+
}
235258
}
236259
Err(e) => panic!("{e:?}"),
237260
}

aw-sync/test-sync-pull.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ SYNCROOTDIR="$HOME/ActivityWatchSync"
2727
function sync_host() {
2828
host=$1
2929
SYNCDIR="$SYNCROOTDIR/$host"
30-
for db in $(ls $SYNCDIR/*/*.db); do
31-
AWSYNCPARAMS="--port $PORT --sync-dir $SYNCDIR"
30+
dbs=$(find $SYNCDIR -name "*.db")
31+
for db in $dbs; do
32+
# workaround to avoid trying to sync empty database files (size 45056)
33+
if [ "$(stat -c%s $db)" -lt 50000 ]; then
34+
continue
35+
fi
36+
37+
AWSYNCPARAMS="--port $PORT --sync-dir $SYNCDIR --sync-db $db"
3238
BUCKETS="aw-watcher-window_$host,aw-watcher-afk_$host"
3339

3440
echo "Syncing $db to $host"
@@ -45,7 +51,18 @@ if [ -z "$host" ]; then
4551
echo "Syncing all hosts"
4652
sleep 0.5
4753
# For each host in the sync directory, pull the data from each database file using aw-sync
48-
for host in $(ls $SYNCROOTDIR); do
54+
# Use `find` to get all directories in the sync directory
55+
hostnames=$(find $SYNCROOTDIR -maxdepth 1 -type d -exec basename {} \;)
56+
# filter out "erb-m2.local"
57+
hostnames=$(echo $hostnames | tr ' ' '\n' | grep -v "erb-m2.local")
58+
# filter out folder not containing subfolders with .db files
59+
for host in $hostnames; do
60+
if [ ! "$(find $SYNCROOTDIR/$host -name "*.db")" ]; then
61+
hostnames=$(echo $hostnames | tr ' ' '\n' | grep -v $host)
62+
fi
63+
done
64+
# Sync each host, file-by-file
65+
for host in $hostnames; do
4966
sync_host $host
5067
done
5168
else

0 commit comments

Comments
 (0)