Skip to content

feat(sync): gracefully handle SIGTERMs #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aw-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ clap = { version = "4.1", features = ["derive"] }
appdirs = "0.2.0"
dirs = "5.0.1"
gethostname = "0.4.3"
ctrlc = "3.4.5"

aw-server = { path = "../aw-server" }
aw-models = { path = "../aw-models" }
Expand Down
27 changes: 23 additions & 4 deletions aw-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ extern crate chrono;
extern crate serde;
extern crate serde_json;

use std::error::Error;
use std::path::PathBuf;

use chrono::{DateTime, Utc};
use clap::{Parser, Subcommand};
use std::error::Error;
use std::path::PathBuf;
use std::sync::mpsc::{channel, RecvTimeoutError};
use std::time::Duration;

use aw_client_rust::blocking::AwClient;

Expand Down Expand Up @@ -207,6 +208,12 @@ fn main() -> Result<(), Box<dyn Error>> {
}

fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
let (tx, rx) = channel();

ctrlc::set_handler(move || {
let _ = tx.send(());
})?;

loop {
if let Err(e) = daemon_sync_cycle(client) {
error!("Error during sync cycle: {}", e);
Expand All @@ -215,8 +222,20 @@ fn daemon(client: &AwClient) -> Result<(), Box<dyn Error>> {
}

info!("Sync pass done, sleeping for 5 minutes");
std::thread::sleep(std::time::Duration::from_secs(300));

// Wait for either the sleep duration or a termination signal
match rx.recv_timeout(Duration::from_secs(300)) {
Ok(_) | Err(RecvTimeoutError::Disconnected) => {
info!("Termination signal received, shutting down.");
break;
}
Err(RecvTimeoutError::Timeout) => {
// Continue the loop if the timeout occurs
}
}
}

Ok(())
}

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