Skip to content

Commit 5dd5f34

Browse files
committed
feat: added daemon default subcommand, syncing at 5min intervals
1 parent ae8f7bf commit 5dd5f34

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

aw-sync/src/main.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ struct Opts {
5252

5353
#[derive(Subcommand)]
5454
enum Commands {
55+
/// Daemon subcommand
56+
/// Starts aw-sync as a daemon, which will sync every 5 minutes.
57+
Daemon {},
58+
5559
/// Sync subcommand (basic)
5660
///
5761
/// Pulls remote buckets then pushes local buckets.
@@ -125,7 +129,12 @@ fn main() -> Result<(), Box<dyn Error>> {
125129
let client = AwClient::new(&opts.host, port, "aw-sync")?;
126130

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

145154
// Push
146155
info!("Pushing local data");
147-
sync_wrapper::push(&client)
156+
sync_wrapper::push(&client)?
148157
}
149158
// Perform two-way sync
150159
Commands::SyncAdvanced {
@@ -178,12 +187,12 @@ fn main() -> Result<(), Box<dyn Error>> {
178187
start: start_date,
179188
};
180189

181-
sync::sync_run(&client, &sync_spec, mode)
190+
sync::sync_run(&client, &sync_spec, mode)?
182191
}
183192

184193
// List all buckets
185-
Commands::List {} => sync::list_buckets(&client),
186-
}?;
194+
Commands::List {} => sync::list_buckets(&client)?,
195+
}
187196

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

193202
Ok(())
194203
}
204+
205+
fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
206+
loop {
207+
info!("Pulling from all hosts");
208+
sync_wrapper::pull_all(client)?;
209+
210+
info!("Pushing local data");
211+
sync_wrapper::push(client)?;
212+
213+
info!("Sync pass done, sleeping for 5 minutes");
214+
215+
std::thread::sleep(std::time::Duration::from_secs(300));
216+
}
217+
}

0 commit comments

Comments
 (0)