From 30bfeae4d25f946e5e1e54242ea706ef7f0a88c4 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Sat, 20 Jan 2024 23:04:51 +0100 Subject: [PATCH] feat: add graceful shutdown --- src/cli.rs | 2 +- src/main.rs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index fd5beb0..b09378c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -5,7 +5,7 @@ use humantime::Duration; use crate::error::ParseError; -#[derive(Debug, Parser)] +#[derive(Debug, Parser, Clone)] pub struct CliArgs { /// The time to wait before refreshing repositories. /// eg. 10m, 60s, etc. diff --git a/src/main.rs b/src/main.rs index 75d3e0a..bb67a93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::env; use clap::Parser; use cli::CliArgs; use log::{error, info}; -use tokio::{process::Command, time::sleep}; +use tokio::{process::Command, signal, time::sleep}; use walkdir::WalkDir; mod cli; @@ -14,8 +14,17 @@ async fn main() { env::set_var("RUST_LOG", "info"); env_logger::init(); + let args = CliArgs::parse(); + loop { - do_sync_task(CliArgs::parse()).await + tokio::select! { + _ = do_sync_task(args.clone()) => {} + + _ = signal::ctrl_c() => { + info!("Shutting down..."); + break; + } + } } } -- libgit2 1.7.2