Browse Source

Run updater daily

Vecna 2 months ago
parent
commit
d933b3b94b
2 changed files with 18 additions and 1 deletions
  1. 1 0
      Cargo.toml
  2. 17 1
      src/bin/server.rs

+ 1 - 0
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"

+ 17 - 1
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<BridgeDistributor, S
     report_blockages(&distributors, new_blockages).await;
 }
 
+async fn run_updater(updater_tx: mpsc::Sender<Command>) {
+    updater_tx.send(Command::Update {}).await.unwrap();
+}
+
 async fn create_context_manager(
     db_config: DbConfig,
     distributors: BTreeMap<BridgeDistributor, String>,
@@ -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;
 }