This commit is contained in:
houseme
2025-04-10 00:43:55 +08:00
parent 10b787b852
commit 6d31834799
+63 -64
View File
@@ -288,8 +288,7 @@ async fn run(opt: config::Opt) -> Result<()> {
// Create an oneshot channel to wait for the service to start // Create an oneshot channel to wait for the service to start
let (tx, rx) = tokio::sync::oneshot::channel(); let (tx, rx) = tokio::sync::oneshot::channel();
// 启动服务
notify_service_state(ServiceState::Starting);
tokio::spawn(async move { tokio::spawn(async move {
// 错误处理改进 // 错误处理改进
let sigterm_inner = match signal(SignalKind::terminate()) { let sigterm_inner = match signal(SignalKind::terminate()) {
@@ -482,65 +481,65 @@ async fn run(opt: config::Opt) -> Result<()> {
Ok(()) Ok(())
} }
#[allow(dead_code)] // #[allow(dead_code)]
#[derive(Debug)] // #[derive(Debug)]
enum ShutdownSignal { // enum ShutdownSignal {
CtrlC, // CtrlC,
Sigterm, // Sigterm,
Sigint, // Sigint,
} // }
#[allow(dead_code)] // #[allow(dead_code)]
async fn wait_for_shutdown() -> ShutdownSignal { // async fn wait_for_shutdown() -> ShutdownSignal {
let mut sigterm = signal(SignalKind::terminate()).unwrap(); // let mut sigterm = signal(SignalKind::terminate()).unwrap();
let mut sigint = signal(SignalKind::interrupt()).unwrap(); // let mut sigint = signal(SignalKind::interrupt()).unwrap();
//
tokio::select! { // tokio::select! {
_ = tokio::signal::ctrl_c() => { // _ = tokio::signal::ctrl_c() => {
info!("Received Ctrl-C signal"); // info!("Received Ctrl-C signal");
ShutdownSignal::CtrlC // ShutdownSignal::CtrlC
} // }
_ = sigint.recv() => { // _ = sigint.recv() => {
info!("Received SIGINT signal"); // info!("Received SIGINT signal");
ShutdownSignal::Sigint // ShutdownSignal::Sigint
} // }
_ = sigterm.recv() => { // _ = sigterm.recv() => {
info!("Received SIGTERM signal"); // info!("Received SIGTERM signal");
ShutdownSignal::Sigterm // ShutdownSignal::Sigterm
} // }
} // }
} // }
#[allow(dead_code)] // #[allow(dead_code)]
#[derive(Debug)] // #[derive(Debug)]
enum ServiceState { // enum ServiceState {
Starting, // Starting,
Ready, // Ready,
Stopping, // Stopping,
Stopped, // Stopped,
} // }
#[allow(dead_code)] // #[allow(dead_code)]
fn notify_service_state(state: ServiceState) { // fn notify_service_state(state: ServiceState) {
match state { // match state {
ServiceState::Starting => { // ServiceState::Starting => {
info!("Service is starting..."); // info!("Service is starting...");
#[cfg(target_os = "linux")] // #[cfg(target_os = "linux")]
if let Err(e) = libsystemd::daemon::notify(false, &[libsystemd::daemon::NotifyState::Status("Starting...")]) { // if let Err(e) = libsystemd::daemon::notify(false, &[libsystemd::daemon::NotifyState::Status("Starting...")]) {
error!("Failed to notify systemd of starting state: {}", e); // error!("Failed to notify systemd of starting state: {}", e);
} // }
} // }
ServiceState::Ready => { // ServiceState::Ready => {
info!("Service is ready"); // info!("Service is ready");
notify_systemd("ready"); // notify_systemd("ready");
} // }
ServiceState::Stopping => { // ServiceState::Stopping => {
info!("Service is stopping..."); // info!("Service is stopping...");
notify_systemd("stopping"); // notify_systemd("stopping");
} // }
ServiceState::Stopped => { // ServiceState::Stopped => {
info!("Service has stopped"); // info!("Service has stopped");
#[cfg(target_os = "linux")] // #[cfg(target_os = "linux")]
if let Err(e) = libsystemd::daemon::notify(false, &[libsystemd::daemon::NotifyState::Status("Stopped")]) { // if let Err(e) = libsystemd::daemon::notify(false, &[libsystemd::daemon::NotifyState::Status("Stopped")]) {
error!("Failed to notify systemd of stopped state: {}", e); // error!("Failed to notify systemd of stopped state: {}", e);
} // }
} // }
} // }
} // }