mirror of
https://github.com/Blah-IM/blahrs.git
synced 2025-05-01 00:31:09 +00:00
feat(blahd): impl graceful shutdown
This commit is contained in:
parent
5143e30fbe
commit
7384888128
4 changed files with 33 additions and 13 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -2138,6 +2138,15 @@ version = "1.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signal-hook-registry"
|
||||||
|
version = "1.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signature"
|
name = "signature"
|
||||||
version = "2.2.0"
|
version = "2.2.0"
|
||||||
|
@ -2381,6 +2390,7 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
|
"signal-hook-registry",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
|
|
|
@ -30,7 +30,7 @@ serde_jcs = "0.1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
serde_urlencoded = "0.7.1"
|
serde_urlencoded = "0.7.1"
|
||||||
sha2 = "0.10"
|
sha2 = "0.10"
|
||||||
tokio = { version = "1", features = ["macros", "rt-multi-thread", "sync", "time"] }
|
tokio = { version = "1", features = ["macros", "rt-multi-thread", "signal", "sync", "time"] }
|
||||||
tokio-stream = { version = "0.1", features = ["sync"] }
|
tokio-stream = { version = "0.1", features = ["sync"] }
|
||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
tower-http = { version = "0.5", features = ["cors", "limit"] }
|
tower-http = { version = "0.5", features = ["cors", "limit"] }
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::future::IntoFuture;
|
||||||
use std::os::fd::{FromRawFd, OwnedFd};
|
use std::os::fd::{FromRawFd, OwnedFd};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -5,6 +6,7 @@ use std::sync::Arc;
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use blahd::config::{Config, ListenConfig};
|
use blahd::config::{Config, ListenConfig};
|
||||||
use blahd::{AppState, Database};
|
use blahd::{AppState, Database};
|
||||||
|
use tokio::signal::unix::{signal, SignalKind};
|
||||||
|
|
||||||
/// Blah Chat Server
|
/// Blah Chat Server
|
||||||
#[derive(Debug, clap::Parser)]
|
#[derive(Debug, clap::Parser)]
|
||||||
|
@ -94,10 +96,16 @@ async fn main_serve(db: Database, config: Config) -> Result<()> {
|
||||||
tracing::info!("listening on {listener_display}");
|
tracing::info!("listening on {listener_display}");
|
||||||
|
|
||||||
let router = blahd::router(Arc::new(st));
|
let router = blahd::router(Arc::new(st));
|
||||||
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
|
|
||||||
|
|
||||||
axum::serve(listener, router)
|
let mut sigterm = signal(SignalKind::terminate()).context("failed to listen on SIGTERM")?;
|
||||||
.await
|
let service = axum::serve(listener, router)
|
||||||
.context("failed to serve")?;
|
.with_graceful_shutdown(async move {
|
||||||
|
sigterm.recv().await;
|
||||||
|
tracing::info!("received SIGTERM, shutting down gracefully");
|
||||||
|
})
|
||||||
|
.into_future();
|
||||||
|
|
||||||
|
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
|
||||||
|
service.await.context("failed to serve")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,7 +104,7 @@ fn socket_activate(#[case] unix_socket: bool) {
|
||||||
}
|
}
|
||||||
ForkResult::Parent { child } => {
|
ForkResult::Parent { child } => {
|
||||||
let guard = scopeguard::guard((), |()| {
|
let guard = scopeguard::guard((), |()| {
|
||||||
let _ = kill(child, Signal::SIGKILL);
|
let _ = kill(child, Signal::SIGTERM);
|
||||||
});
|
});
|
||||||
|
|
||||||
if !unix_socket {
|
if !unix_socket {
|
||||||
|
@ -130,15 +130,17 @@ fn socket_activate(#[case] unix_socket: bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let st = waitpid(child, None).unwrap();
|
let st = waitpid(child, None).unwrap();
|
||||||
if unix_socket {
|
let expect_exit_code = if unix_socket {
|
||||||
// Fail with unsupported error.
|
// Fail with unsupported error.
|
||||||
assert!(matches!(st, WaitStatus::Exited(_, 1)));
|
1
|
||||||
} else {
|
} else {
|
||||||
assert!(
|
// Graceful shutdown.
|
||||||
matches!(st, WaitStatus::Signaled(_, Signal::SIGKILL, _)),
|
0
|
||||||
"unexpected exit status {st:?}",
|
};
|
||||||
);
|
assert!(
|
||||||
}
|
matches!(st, WaitStatus::Exited(_, code) if code == expect_exit_code),
|
||||||
|
"unexpected exit status {st:?}",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue