diff --git a/bot/Cargo.toml b/bot/Cargo.toml index d9d79dc..889bb27 100644 --- a/bot/Cargo.toml +++ b/bot/Cargo.toml @@ -5,4 +5,10 @@ edition = "2021" [dependencies] tokio = { version = "1", features = ["full"] } - +clap = { version = "4.5.23", features = ["derive"] } +post = {path = "../post"} +infrastructure = { path = "../infrastructure"} +signal-hook = "0.3.17" +log = "0.4.22" +env_logger = "0.11.6" +anyhow = "1.0.95" diff --git a/bot/src/cli.rs b/bot/src/cli.rs new file mode 100644 index 0000000..1427e08 --- /dev/null +++ b/bot/src/cli.rs @@ -0,0 +1,13 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct CliArgs { + /// Redis host + #[arg(short, long)] + pub redis_connection_string: String, + + /// Redis stream name + #[arg(short = 't', long)] + pub redis_stream_name: String, +} diff --git a/bot/src/main.rs b/bot/src/main.rs index 7e3d561..7209312 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,4 +1,42 @@ -#[tokio::main] -async fn main() { - println!("Hello, world!"); +use crate::cli::CliArgs; +use clap::Parser; +use log::{error, info}; +use signal_hook::consts::{SIGINT, SIGTERM}; +use signal_hook::iterator::Signals; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; +use std::thread; + +mod cli; + +//noinspection DuplicatedCode +/// Sets up a signal handler in a separate thread to handle SIGINT and SIGTERM signals. +fn setup_graceful_shutdown(running: Arc) { + let r = running.clone(); + 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}") + } + } + }); +} + +#[tokio::main] +async fn main() -> Result<(), anyhow::Error> { + env_logger::init(); + let args = CliArgs::parse(); + info!("Starting the program"); + + // Graceful shutdown. + let running = Arc::new(AtomicBool::new(true)); + setup_graceful_shutdown(running); + + Ok(()) }