Skip to content

Commit 0da116b

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

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

aw-sync/src/main.rs

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

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

1921
use chrono::{DateTime, Utc};
2022
use clap::{Parser, Subcommand};
@@ -207,7 +209,14 @@ fn main() -> Result<(), Box<dyn Error>> {
207209
}
208210

209211
fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
210-
loop {
212+
let running = Arc::new(AtomicBool::new(true));
213+
let r = running.clone();
214+
215+
ctrlc::set_handler(move || {
216+
r.store(false, Ordering::SeqCst);
217+
})?;
218+
219+
while running.load(Ordering::SeqCst) {
211220
if let Err(e) = daemon_sync_cycle(client) {
212221
error!("Error during sync cycle: {}", e);
213222
// Re-throw the error
@@ -217,6 +226,9 @@ fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
217226
info!("Sync pass done, sleeping for 5 minutes");
218227
std::thread::sleep(std::time::Duration::from_secs(300));
219228
}
229+
230+
info!("Termination signal received, shutting down.");
231+
Ok(())
220232
}
221233

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

0 commit comments

Comments
 (0)