From 1de2ddac8dec7f53935e71544870077dab5a2128 Mon Sep 17 00:00:00 2001 From: RinChanNOW Date: Fri, 3 Nov 2023 11:22:06 +0800 Subject: [PATCH] chore: log add timestamp and add retry mechanism. (#27) --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 3 ++- src/main.rs | 11 ++++++++--- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 68660a7..b190396 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,18 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backon" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c1a6197b2120bb2185a267f6515038558b019e92b832bb0320e96d66268dcf9" +dependencies = [ + "fastrand", + "futures-core", + "pin-project", + "tokio", +] + [[package]] name = "base64" version = "0.13.0" @@ -85,14 +97,15 @@ version = "0.4.0" dependencies = [ "anyhow", "async-trait", + "backon", "chrono", "clap", "daemonize", "futures", "log", - "pretty_env_logger", "reqwest", "rss", + "sensible-env-logger", "serde", "serfig", "tokio", @@ -300,9 +313,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" dependencies = [ "instant", ] @@ -785,6 +798,26 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +[[package]] +name = "pin-project" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -839,9 +872,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ "unicode-ident", ] @@ -993,6 +1026,17 @@ dependencies = [ "libc", ] +[[package]] +name = "sensible-env-logger" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2110d0fb5cbd1f468bba2470ecd079223c06f1279775351273adf58650702ff7" +dependencies = [ + "chrono", + "log", + "pretty_env_logger", +] + [[package]] name = "serde" version = "1.0.145" diff --git a/Cargo.toml b/Cargo.toml index 7911294..2db26df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,14 +18,15 @@ doctest = false [dependencies] anyhow = "1.0" async-trait = "0.1.68" +backon = "0.4" chrono = "0.4.22" clap = { version = "3.2.22", features = ["derive", "env"] } daemonize = "0.5.0" futures = "0.3.28" log = "0.4" -pretty_env_logger = "0.4" reqwest = { version = "0.11", features = ["json"] } rss = { version = "2.0" } +sensible-env-logger = { version = "0.3", features = ["local-time"] } serde = { version = "1.0.145", features = ["derive"] } serfig = "0.0.2" tokio = { version = "1.28.0", features = ["rt-multi-thread", "macros"] } diff --git a/src/main.rs b/src/main.rs index f7e4382..9fab1e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,8 @@ use std::fs::File; use std::path::PathBuf; use std::sync::Arc; +use backon::ConstantBuilder; +use backon::Retryable; use blooming::notifier; use blooming::source::register; use blooming::source::SourceFactory; @@ -71,12 +73,14 @@ async fn run(source: SourcePtr, notifier: Arc) { let mut last_update = Local::now(); let interval = source.interval(); + let retry_config = ConstantBuilder::default(); loop { tokio::time::sleep(interval).await; let result: Result<()> = try { - let items = source.pull_items().await?; + let fetch = || async { source.pull_items().await }; + let items = fetch.retry(&retry_config).await?; let new_items = items .into_iter() .filter(|item| item.pub_date > last_update) @@ -90,7 +94,8 @@ async fn run(source: SourcePtr, notifier: Arc) { }); // notify by qq bot - notifier.notify(&source.name(), new_items).await?; + let note = || async { notifier.notify(&source.name(), new_items.clone()).await }; + note.retry(&retry_config).await?; } }; @@ -102,7 +107,7 @@ async fn run(source: SourcePtr, notifier: Arc) { #[tokio::main] async fn main() -> Result<()> { - pretty_env_logger::init(); + sensible_env_logger::init_timed_local!(); let args = ClapConfig::parse();