Run updater daily

This commit is contained in:
Vecna 2024-03-25 20:41:34 -04:00
parent 32d68893a7
commit d933b3b94b
2 changed files with 18 additions and 1 deletions

View File

@ -29,3 +29,4 @@ sha3 = "0.10"
sled = "0.34.7" sled = "0.34.7"
time = "0.3.30" time = "0.3.30"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tokio-cron = "0.1.2"

View File

@ -18,6 +18,7 @@ use tokio::{
sync::{broadcast, mpsc, oneshot}, sync::{broadcast, mpsc, oneshot},
time::sleep, time::sleep,
}; };
use tokio_cron::{Job, Scheduler};
async fn shutdown_signal() { async fn shutdown_signal() {
tokio::signal::ctrl_c() 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; 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( async fn create_context_manager(
db_config: DbConfig, db_config: DbConfig,
distributors: BTreeMap<BridgeDistributor, String>, distributors: BTreeMap<BridgeDistributor, String>,
@ -137,11 +142,14 @@ async fn main() {
let (request_tx, request_rx) = mpsc::channel(32); let (request_tx, request_rx) = mpsc::channel(32);
let updater_tx = request_tx.clone();
let shutdown_cmd_tx = request_tx.clone(); let shutdown_cmd_tx = request_tx.clone();
// create the shutdown broadcast channel and clone for every thread // create the shutdown broadcast channel and clone for every thread
let (shutdown_tx, mut shutdown_rx) = broadcast::channel(16); let (shutdown_tx, mut shutdown_rx) = broadcast::channel(16);
let kill = shutdown_tx.subscribe(); 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 // Listen for ctrl_c, send signal to broadcast shutdown to all threads by dropping shutdown_tx
let shutdown_handler = spawn(async move { 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 { let context_manager = spawn(async move {
create_context_manager(config.db, config.distributors, request_rx, kill).await create_context_manager(config.db, config.distributors, request_rx, kill).await
}); });
@ -186,5 +202,5 @@ async fn main() {
if let Err(e) = graceful.await { if let Err(e) = graceful.await {
eprintln!("server error: {}", e); eprintln!("server error: {}", e);
} }
future::join_all([context_manager, shutdown_handler]).await; future::join_all([context_manager, updater, shutdown_handler]).await;
} }