ctrl-c handling

This commit is contained in:
Robin Appelman 2021-08-17 20:21:31 +02:00
commit d70dbbedc3
3 changed files with 29 additions and 3 deletions

11
Cargo.lock generated
View file

@ -798,6 +798,15 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [
"libc",
]
[[package]]
name = "slab"
version = "0.4.3"
@ -884,7 +893,9 @@ dependencies = [
"memchr",
"mio",
"num_cpus",
"once_cell",
"pin-project-lite",
"signal-hook-registry",
"tokio-macros",
"winapi",
]

View file

@ -7,7 +7,7 @@ edition = "2018"
[dependencies]
rss = "1.10"
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "json"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "signal"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
toml = "0.5"

View file

@ -11,6 +11,8 @@ use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use tokio::time::sleep;
use tokio::signal::ctrl_c;
use tokio::select;
#[tokio::main]
async fn main() -> Result<()> {
@ -26,10 +28,23 @@ async fn main() -> Result<()> {
};
let config = Config::from_file(&file)?;
let mut fetcher = FeedFetcher::default();
println!("Running rss trigger for {} feeds", config.feed.len());
let ctrl_c = async { ctrl_c().await.ok(); };
select! {
_ = ctrl_c => {},
_ = main_loop(config) => {
println!("more_async_work() completed first")
}
};
Ok(())
}
async fn main_loop(config: Config) {
let mut fetcher = FeedFetcher::default();
loop {
for feed in config.feed.iter() {
match fetcher.is_feed_updated(&feed.feed).await {
@ -42,7 +57,7 @@ async fn main() -> Result<()> {
if !feed.body.is_null() {
req = req.json(&feed.body);
}
if let Err(e) = req.send().await?.error_for_status() {
if let Err(e) = req.send().await.and_then(|res| res.error_for_status()) {
eprintln!("{:#}", e);
}
}