Skip to content

Commit

Permalink
feat: added daemon default subcommand, syncing at 5min intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 22, 2023
1 parent ae8f7bf commit 5dd5f34
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions aw-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ struct Opts {

#[derive(Subcommand)]
enum Commands {
/// Daemon subcommand
/// Starts aw-sync as a daemon, which will sync every 5 minutes.
Daemon {},

/// Sync subcommand (basic)
///
/// Pulls remote buckets then pushes local buckets.
Expand Down Expand Up @@ -125,7 +129,12 @@ fn main() -> Result<(), Box<dyn Error>> {
let client = AwClient::new(&opts.host, port, "aw-sync")?;

// if opts.command is None, then we're using the default subcommand (Sync)
match opts.command.unwrap_or(Commands::Sync { host: None }) {
match opts.command.unwrap_or(Commands::Daemon {}) {
// Start daemon
Commands::Daemon {} => {
info!("Starting daemon...");
daemon(&client)?;
}
// Perform basic sync
Commands::Sync { host } => {
// Pull
Expand All @@ -144,7 +153,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// Push
info!("Pushing local data");
sync_wrapper::push(&client)
sync_wrapper::push(&client)?
}
// Perform two-way sync
Commands::SyncAdvanced {
Expand Down Expand Up @@ -178,12 +187,12 @@ fn main() -> Result<(), Box<dyn Error>> {
start: start_date,
};

sync::sync_run(&client, &sync_spec, mode)
sync::sync_run(&client, &sync_spec, mode)?
}

// List all buckets
Commands::List {} => sync::list_buckets(&client),
}?;
Commands::List {} => sync::list_buckets(&client)?,
}

// Needed to give the datastores some time to commit before program is shut down.
// 100ms isn't actually needed, seemed to work fine with as little as 10ms, but I'd rather give
Expand All @@ -192,3 +201,17 @@ fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}

fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
loop {
info!("Pulling from all hosts");
sync_wrapper::pull_all(client)?;

info!("Pushing local data");
sync_wrapper::push(client)?;

info!("Sync pass done, sleeping for 5 minutes");

std::thread::sleep(std::time::Duration::from_secs(300));
}
}

0 comments on commit 5dd5f34

Please sign in to comment.