add basic args & graceful shutdown

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-27 11:14:43 +02:00
parent 76e00991d9
commit 2cca16b0c6
3 changed files with 61 additions and 4 deletions

View file

@ -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"

13
bot/src/cli.rs Normal file
View file

@ -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,
}

View file

@ -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<AtomicBool>) {
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(())
}