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

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