Add gracefull shutdown

This commit is contained in:
2024-01-08 00:58:08 +01:00
parent 8431b27ce0
commit da6f5161fe
2 changed files with 16 additions and 3 deletions

View File

@@ -4,7 +4,13 @@ use axum::routing::post;
use reqwest::StatusCode;
use std::net::SocketAddr;
use std::{
net::SocketAddr,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
use teloxide::types::{Update, UpdateKind};
@@ -14,7 +20,7 @@ use tracing::log;
use crate::bots_manager::{internal::start_bot, BOTS_DATA, BOTS_ROUTES, SERVER_PORT};
pub async fn start_axum_server() {
pub async fn start_axum_server(stop_signal: Arc<AtomicBool>) {
async fn telegram_request(Path(token): Path<String>, input: String) -> impl IntoResponse {
let (_, r_tx) = match BOTS_ROUTES.get(&token).await {
Some(tx) => tx,
@@ -80,6 +86,13 @@ pub async fn start_axum_server() {
axum::Server::bind(&addr)
.serve(router.into_make_service())
.with_graceful_shutdown(async move {
loop {
if !stop_signal.load(Ordering::SeqCst) {
break;
};
}
})
.await
.expect("Axum server error");

View File

@@ -98,7 +98,7 @@ impl BotsManager {
}
pub async fn start(running: Arc<AtomicBool>) {
start_axum_server().await;
start_axum_server(running.clone()).await;
let mut interval = time::interval(Duration::from_secs(5));