Skip to content

Commit 630f0df

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

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

aw-sync/src/main.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ extern crate chrono;
1313
extern crate serde;
1414
extern crate serde_json;
1515

16-
use std::error::Error;
17-
use std::path::PathBuf;
18-
1916
use chrono::{DateTime, Utc};
2017
use clap::{Parser, Subcommand};
18+
use std::error::Error;
19+
use std::path::PathBuf;
20+
use std::sync::mpsc::{channel, RecvTimeoutError};
21+
use std::time::Duration;
2122

2223
use aw_client_rust::blocking::AwClient;
2324

@@ -207,6 +208,13 @@ fn main() -> Result<(), Box<dyn Error>> {
207208
}
208209

209210
fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
211+
let (tx, rx) = channel();
212+
let tx_clone = tx.clone();
213+
214+
ctrlc::set_handler(move || {
215+
let _ = tx_clone.send(());
216+
})?;
217+
210218
loop {
211219
if let Err(e) = daemon_sync_cycle(client) {
212220
error!("Error during sync cycle: {}", e);
@@ -215,8 +223,20 @@ fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
215223
}
216224

217225
info!("Sync pass done, sleeping for 5 minutes");
218-
std::thread::sleep(std::time::Duration::from_secs(300));
226+
227+
// Wait for either the sleep duration or a termination signal
228+
match rx.recv_timeout(Duration::from_secs(300)) {
229+
Ok(_) | Err(RecvTimeoutError::Disconnected) => {
230+
info!("Termination signal received, shutting down.");
231+
break;
232+
}
233+
Err(RecvTimeoutError::Timeout) => {
234+
// Continue the loop if the timeout occurs
235+
}
236+
}
219237
}
238+
239+
Ok(())
220240
}
221241

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

0 commit comments

Comments
 (0)