diff --git a/Cargo.toml b/Cargo.toml index 626acbc..22f8ea7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,4 @@ sha3 = "0.10" sled = "0.34.7" time = "0.3.30" tokio = { version = "1", features = ["full"] } +tokio-cron = "0.1.2" diff --git a/src/bin/server.rs b/src/bin/server.rs index a1a9e70..ab36705 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -18,6 +18,7 @@ use tokio::{ sync::{broadcast, mpsc, oneshot}, time::sleep, }; +use tokio_cron::{Job, Scheduler}; async fn shutdown_signal() { tokio::signal::ctrl_c() @@ -65,6 +66,10 @@ async fn update_daily_info(db: &Db, distributors: &BTreeMap) { + updater_tx.send(Command::Update {}).await.unwrap(); +} + async fn create_context_manager( db_config: DbConfig, distributors: BTreeMap, @@ -137,11 +142,14 @@ async fn main() { let (request_tx, request_rx) = mpsc::channel(32); + let updater_tx = request_tx.clone(); let shutdown_cmd_tx = request_tx.clone(); // create the shutdown broadcast channel and clone for every thread let (shutdown_tx, mut shutdown_rx) = broadcast::channel(16); let kill = shutdown_tx.subscribe(); + // TODO: Gracefully shut down updater + let kill_updater = shutdown_tx.subscribe(); // Listen for ctrl_c, send signal to broadcast shutdown to all threads by dropping shutdown_tx let shutdown_handler = spawn(async move { @@ -158,6 +166,14 @@ async fn main() { } }); + let updater = spawn(async move { + // Run updater once per day + let mut sched = Scheduler::utc(); + sched.add(Job::new("* * 22 * * * *", move || { + run_updater(updater_tx.clone()) + })); + }); + let context_manager = spawn(async move { create_context_manager(config.db, config.distributors, request_rx, kill).await }); @@ -186,5 +202,5 @@ async fn main() { if let Err(e) = graceful.await { eprintln!("server error: {}", e); } - future::join_all([context_manager, shutdown_handler]).await; + future::join_all([context_manager, updater, shutdown_handler]).await; }