Skip to content

Commit

Permalink
handle SIGTERM and SIGINT signals
Browse files Browse the repository at this point in the history
  • Loading branch information
jelni committed May 2, 2024
1 parent fe0254c commit 87fa058
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
122 changes: 122 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ md5 = "0.7"
time = { version = "0.3", features = ["serde", "serde-well-known", "macros"] }
dotenvy = "0.15"
tempfile = "3.4"
async-signal = "0.2"
log = { version = "0.4", features = ["std"] }
colored = "2.0"
rand = "0.9.0-alpha"
Expand Down
12 changes: 11 additions & 1 deletion src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::env;
use std::future::Future;
use std::sync::Arc;

use async_signal::{Signal, Signals};
use futures_util::StreamExt;
use tdlib::enums::{
AuthorizationState, BotCommands, ConnectionState, MessageSender, OptionValue, Update,
};
Expand Down Expand Up @@ -52,7 +54,15 @@ impl Bot {
});

let state = self.state.clone();
self.run_task(async move {
tokio::spawn(async move {
let mut signals = Signals::new([Signal::Term, Signal::Int]).unwrap();
let signal = signals.next().await.unwrap().unwrap();
log::warn!("{signal:?} received");
*state.status.lock().unwrap() = BotStatus::WaitingToClose;
});

let state = self.state.clone();
tokio::spawn(async move {
signal::ctrl_c().await.unwrap();
log::warn!("Ctrl+C received");
*state.status.lock().unwrap() = BotStatus::WaitingToClose;
Expand Down

0 comments on commit 87fa058

Please sign in to comment.