From b21863298b08d99f074bf70f493fd763b5379769 Mon Sep 17 00:00:00 2001 From: Philipp Hofer Date: Tue, 12 Aug 2025 20:40:57 +0200 Subject: [PATCH] auto-call downloader every day --- Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 5 ++--- src/main.rs | 23 ++++++++++++++++++++++- src/state.rs | 5 ++--- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5da2321..198e7b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,6 +194,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "croner" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c344b0690c1ad1c7176fe18eb173e0c927008fdaaa256e40dfd43ddd149c0843" +dependencies = [ + "chrono", +] + [[package]] name = "displaydoc" version = "0.2.5" @@ -727,6 +736,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -804,6 +824,7 @@ dependencies = [ "serde_json", "stream-download", "tokio", + "tokio-cron-scheduler", "tokio-stream", ] @@ -1323,6 +1344,21 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "tokio-cron-scheduler" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c71ce8f810abc9fabebccc30302a952f9e89c6cf246fafaf170fef164063141" +dependencies = [ + "chrono", + "croner", + "num-derive", + "num-traits", + "tokio", + "tracing", + "uuid", +] + [[package]] name = "tokio-macros" version = "2.5.0" @@ -1481,6 +1517,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "uuid" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f33196643e165781c20a5ead5582283a7dacbb87855d867fbc2df3f81eddc1be" +dependencies = [ + "getrandom 0.3.3", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "want" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index dbbb05d..6e6da9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ async-stream = "0.3" serde_json = "1" stream-download = "0.22" chrono = "0.4" +tokio-cron-scheduler = "0.14.0" diff --git a/src/lib.rs b/src/lib.rs index e1e629c..102ded6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,11 +17,10 @@ async fn get_newest_morning_journal() -> Result Result<(), Box> { @@ -11,7 +13,26 @@ async fn main() -> Result<(), Box> { let app = Router::new() .route("/", get(streamer::stream_handler)) - .with_state(state); + .with_state(state.clone()); + + let scheduler = JobScheduler::new().await.unwrap(); + scheduler + .add( + Job::new_async( + "30 0 7 * * Mon,Tue,Wed,Thu,Fri,Sat", + move |_uuid, _locked| { + let state_for_task = state.clone(); + Box::pin(async move { + state_for_task.check_update().await; + println!("Task executed at: {}", Utc::now()); + }) + }, + ) + .unwrap(), + ) + .await + .unwrap(); + scheduler.start().await.unwrap(); println!("Streaming server running on http://localhost:3029"); diff --git a/src/state.rs b/src/state.rs index dc19cea..0874dfc 100644 --- a/src/state.rs +++ b/src/state.rs @@ -25,11 +25,10 @@ impl AppState { pub async fn check_update(self: Arc) { let today = Local::now().date_naive(); - if let Some(downloaded_on_day) = *self.downloaded_on_day.read().await { - if today == downloaded_on_day { + if let Some(downloaded_on_day) = *self.downloaded_on_day.read().await + && today == downloaded_on_day { return; } - } self.reset().await; *self.downloaded_on_day.write().await = Some(today);