diff --git a/docker-compose.yaml b/docker-compose.yaml index e2ba075..e963429 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,4 @@ # Docker Compose file used for testing -version: '3.8' services: redis: container_name: bluesky-bot-local-redis diff --git a/scrapper/Cargo.toml b/scrapper/Cargo.toml index 3bfe165..26451bc 100644 --- a/scrapper/Cargo.toml +++ b/scrapper/Cargo.toml @@ -11,7 +11,7 @@ reqwest = "0.12.9" scraper = "0.22.0" clokwerk = "0.4.0" log = "0.4.22" -ctrlc = "3.4.5" clap = { version = "4.5.23", features = ["derive"] } post = {path = "../post"} -infrastructure = { path = "../infrastructure"} \ No newline at end of file +infrastructure = { path = "../infrastructure"} +signal-hook = "0.3.17" \ No newline at end of file diff --git a/scrapper/src/main.rs b/scrapper/src/main.rs index 608f864..77791f5 100644 --- a/scrapper/src/main.rs +++ b/scrapper/src/main.rs @@ -6,9 +6,11 @@ use clokwerk::{AsyncScheduler, Interval, TimeUnits}; use infrastructure::RedisService; use log::{debug, error, info}; use post::NewsPost; +use signal_hook::{consts::SIGINT, consts::SIGTERM, iterator::Signals}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{Receiver, Sender}; use std::sync::{mpsc, Arc}; +use std::thread; use std::time::Duration; use tokio::task::JoinHandle; @@ -67,10 +69,20 @@ async fn main() -> Result<(), anyhow::Error> { // Graceful shutdown. let running = Arc::new(AtomicBool::new(true)); let r = running.clone(); - ctrlc::set_handler(move || { - r.store(false, Ordering::SeqCst); - }) - .expect("Error setting Ctrl-C handler"); + + thread::spawn(move || { + let signals = Signals::new([SIGINT, SIGTERM]); + match signals { + Ok(mut signal_info) => { + if signal_info.forever().next().is_some() { + r.store(false, Ordering::SeqCst); + } + } + Err(error) => { + error!("Failed to setup signal handler: {error}") + } + } + }); run_scrapping_job(&mut scheduler, tx, args.scrape_interval_minutes.minutes());