Skip to content

Commit 5e86d5b

Browse files
committed
feat(sync): gracefully handle SIGTERMs
1 parent 60479cf commit 5e86d5b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

aw-sync/src/main.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ extern crate serde_json;
1515

1616
use std::error::Error;
1717
use std::path::PathBuf;
18+
use std::sync::Arc;
19+
use std::sync::atomic::{AtomicBool, Ordering};
20+
1821

1922
use chrono::{DateTime, Utc};
2023
use clap::{Parser, Subcommand};
@@ -207,7 +210,14 @@ fn main() -> Result<(), Box<dyn Error>> {
207210
}
208211

209212
fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
210-
loop {
213+
let running = Arc::new(AtomicBool::new(true));
214+
let r = running.clone();
215+
216+
ctrlc::set_handler(move || {
217+
r.store(false, Ordering::SeqCst);
218+
})?;
219+
220+
while running.load(Ordering::SeqCst) {
211221
if let Err(e) = daemon_sync_cycle(client) {
212222
error!("Error during sync cycle: {}", e);
213223
// Re-throw the error
@@ -217,6 +227,9 @@ fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
217227
info!("Sync pass done, sleeping for 5 minutes");
218228
std::thread::sleep(std::time::Duration::from_secs(300));
219229
}
230+
231+
info!("Termination signal received, shutting down.");
232+
Ok(())
220233
}
221234

222235
fn daemon_sync_cycle(client: &AwClient) -> Result<(), Box<dyn Error>> {

0 commit comments

Comments
 (0)