Skip to content

Commit

Permalink
Add shutdown listening (non-graceful)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Feb 26, 2024
1 parent 07ff678 commit 014a738
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod keys;
pub mod serde_utils;
pub mod server;
pub mod service;
pub mod shutdown;
pub mod task_runner;
pub mod tasks;
pub mod types;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
use tx_sitter::config::load_config;
use tx_sitter::service::Service;
use tx_sitter::shutdown::spawn_await_shutdown_task;

#[derive(Parser)]
#[command(author, version, about)]
Expand Down Expand Up @@ -55,6 +56,8 @@ async fn main() -> eyre::Result<()> {
)?;
}

spawn_await_shutdown_task();

tracing::info!(?config, "Starting service");
let service = Service::new(config).await?;
service.wait().await?;
Expand Down
28 changes: 28 additions & 0 deletions src/shutdown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::panic;

use tokio::signal::unix::{signal, SignalKind};

pub fn spawn_await_shutdown_task() {
tokio::spawn(async {
let result = await_shutdown_signal().await;
if let Err(err) = result {
tracing::error!("Error while waiting for shutdown signal: {}", err);
panic!("Error while waiting for shutdown signal: {}", err);
}

tracing::info!("Shutdown complete");
std::process::exit(0);
});
}

pub async fn await_shutdown_signal() -> eyre::Result<()> {
let mut sigint = signal(SignalKind::interrupt())?;
let mut sigterm = signal(SignalKind::terminate())?;

tokio::select! {
_ = sigint.recv() => { tracing::info!("SIGINT received, shutting down"); }
_ = sigterm.recv() => { tracing::info!("SIGTERM received, shutting down"); }
};

Ok(())
}

0 comments on commit 014a738

Please sign in to comment.