|
@@ -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;
|
|
|
}
|