Skip to content

Commit

Permalink
fix(sync): important FIXME comments, improvements to sync scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 2, 2022
1 parent d6b3992 commit 922f9b3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
40 changes: 21 additions & 19 deletions aw-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use aw_models::{Bucket, Event};

use crate::accessmethod::AccessMethod;

#[derive(PartialEq)]
#[derive(PartialEq, Eq)]
pub enum SyncMode {
Push,
Pull,
Expand Down Expand Up @@ -51,15 +51,21 @@ impl Default for SyncSpec {

/// Performs a single sync pass
pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Result<(), String> {
let ds_localremote = setup_local_remote(&client, sync_spec.path.as_path())?;

let info = client.get_info().map_err(|e| e.to_string())?;
let remote_dbfiles = find_remotes_nonlocal(sync_spec.path.as_path(), info.device_id.as_str());

// FIXME: Here it is assumed that the device_id for the local server is the one used by
// aw-server-rust, which is not necessarily true (aw-server-python has seperate device_id).
// Therefore, this may sometimes fail to pick up the correct local datastore.
let device_id = info.device_id.as_str();

// FIXME: Bad device_id assumption?
let ds_localremote = setup_local_remote(sync_spec.path.as_path(), device_id)?;
let remote_dbfiles = find_remotes_nonlocal(sync_spec.path.as_path(), device_id);

// Log if remotes found
// TODO: Only log remotes of interest
if !remote_dbfiles.is_empty() {
println!(
info!(
"Found {} remote db files: {:?}",
remote_dbfiles.len(),
remote_dbfiles
Expand All @@ -74,7 +80,7 @@ pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Resul
.collect();

if !ds_remotes.is_empty() {
println!(
info!(
"Found {} remote datastores: {:?}",
ds_remotes.len(),
ds_remotes
Expand All @@ -92,13 +98,7 @@ pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Resul
// Push local server buckets to sync folder
if mode == SyncMode::Push || mode == SyncMode::Both {
info!("Pushing...");
sync_datastores(
&client,
&ds_localremote,
true,
Some(info.device_id.as_str()),
sync_spec,
);
sync_datastores(&client, &ds_localremote, true, Some(device_id), sync_spec);
}

// Close open database connections
Expand All @@ -121,10 +121,13 @@ pub fn sync_run(client: AwClient, sync_spec: &SyncSpec, mode: SyncMode) -> Resul

#[allow(dead_code)]
pub fn list_buckets(client: &AwClient, sync_directory: &Path) -> Result<(), String> {
let ds_localremote = setup_local_remote(client, sync_directory)?;

let info = client.get_info().map_err(|e| e.to_string())?;
let remote_dbfiles = find_remotes_nonlocal(sync_directory, info.device_id.as_str());

// FIXME: Incorrect device_id assumption?
let device_id = info.device_id.as_str();
let ds_localremote = setup_local_remote(sync_directory, device_id)?;

let remote_dbfiles = find_remotes_nonlocal(sync_directory, device_id);
info!("Found remotes: {:?}", remote_dbfiles);

// TODO: Check for compatible remote db version before opening
Expand All @@ -143,12 +146,11 @@ pub fn list_buckets(client: &AwClient, sync_directory: &Path) -> Result<(), Stri
Ok(())
}

fn setup_local_remote(client: &AwClient, path: &Path) -> Result<Datastore, String> {
fn setup_local_remote(path: &Path, device_id: &str) -> Result<Datastore, String> {
// FIXME: Don't run twice if already exists
fs::create_dir_all(path).unwrap();

let info = client.get_info().map_err(|e| e.to_string())?;
let remotedir = path.join(info.device_id.as_str());
let remotedir = path.join(device_id);
fs::create_dir_all(&remotedir).unwrap();

let dbfile = remotedir.join("test.db");
Expand Down
30 changes: 26 additions & 4 deletions aw-sync/test-sync-pull.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# exit on fail
set -e

# get script path
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
pushd $SCRIPTPATH
Expand All @@ -20,19 +23,38 @@ fi
sleep 1;
SYNCROOTDIR="$HOME/ActivityWatchSync"

# For each host in the sync directory, pull the data from each database file using aw-sync
for host in $(ls $SYNCROOTDIR); do

function sync_host() {
host=$1
SYNCDIR="$SYNCROOTDIR/$host"
for db in $(ls $SYNCDIR/*/*.db); do
AWSYNCPARAMS="--port $PORT --sync-dir $SYNCDIR"
BUCKETS="aw-watcher-window_$host,aw-watcher-afk_$host"

echo "Syncing $db to $host"
cargo run --bin aw-sync -- $AWSYNCPARAMS sync --mode pull --buckets $BUCKETS
# TODO: If there are no buckets from the expected host, emit a warning at the end.
# (push-script should not have created them to begin with)
done
done
}

host=$1

# if no host given, sync all, otherwise sync only the given host
if [ -z "$host" ]; then
echo "Syncing all hosts"
sleep 0.5
# For each host in the sync directory, pull the data from each database file using aw-sync
for host in $(ls $SYNCROOTDIR); do
sync_host $host
done
else
echo "Syncing host $1"
sleep 0.5
sync_host $host
fi

# kill aw-server-rust
# kill aw-server-rust (if started by us)
if [ "$SERVER_PID" ]; then
kill $SERVER_PID
fi
18 changes: 16 additions & 2 deletions aw-sync/test-sync-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@
# Helper script meant to be used to test aw-sync
# Example of a single-entry for cronjobs and the like

HOSTNAME=$(hostnamectl --static)
# exit on fail
set -e

# on Linux, use `hostnamectl`, on macOS, use `hostname`
if [ -x "$(command -v hostnamectl)" ]; then
HOSTNAME=$(hostnamectl --static)
else
HOSTNAME=$(hostname)
fi

# TODO: Fetch in a cross-platform way (from aw-client command output?)
AWSERVERCONF=~/.config/activitywatch/aw-server/aw-server.toml

# trim everything in file AWSERVERCONF before '[server-testing]' section
# grep for the aw-server port in aw-server.toml
PORT=$(sed '/\[server-testing\]/,/\[.*\]/{//!d}' $AWSERVERCONF | grep -oP 'port = "\K[0-9]+')
# if config doesn't exist, assume 5600
if [ -f "$AWSERVERCONF" ]; then
PORT=$(sed '/\[server-testing\]/,/\[.*\]/{//!d}' $AWSERVERCONF | grep -oP 'port = "\K[0-9]+')
else
PORT=5600
fi

SYNCDIR="$HOME/ActivityWatchSync/$HOSTNAME"
AWSYNCPARAMS="--port $PORT --sync-dir $SYNCDIR"
Expand Down

0 comments on commit 922f9b3

Please sign in to comment.