mirror of
https://github.com/awfufu/traudit
synced 2026-03-01 05:29:44 +08:00
fix: notify systemd of new MAINPID during reload to prevent service termination
This commit is contained in:
@@ -100,10 +100,18 @@ pub async fn bind_listener(
|
||||
use std::net::SocketAddr;
|
||||
// AsRawFd removed
|
||||
|
||||
let addr: SocketAddr = addr_str.parse().map_err(|e: std::net::AddrParseError| {
|
||||
error!("[{}] invalid address {}: {}", service_name, addr_str, e);
|
||||
anyhow::anyhow!(e)
|
||||
})?;
|
||||
let normalized_addr = if addr_str.starts_with(":::") {
|
||||
format!("[::]:{}", &addr_str[3..])
|
||||
} else {
|
||||
addr_str.to_string()
|
||||
};
|
||||
|
||||
let addr: SocketAddr = normalized_addr
|
||||
.parse()
|
||||
.map_err(|e: std::net::AddrParseError| {
|
||||
error!("[{}] invalid address {}: {}", service_name, addr_str, e);
|
||||
anyhow::anyhow!(e)
|
||||
})?;
|
||||
|
||||
let domain = if addr.is_ipv4() {
|
||||
socket2::Domain::IPV4
|
||||
|
||||
15
src/main.rs
15
src/main.rs
@@ -132,7 +132,20 @@ async fn main() -> anyhow::Result<()> {
|
||||
match std::process::Command::new(&args[0])
|
||||
.args(&args[1..])
|
||||
.spawn() {
|
||||
Ok(child) => info!("spawned new process with pid: {}", child.id()),
|
||||
Ok(child) => {
|
||||
let child_pid = child.id();
|
||||
info!("spawned new process with pid: {}", child_pid);
|
||||
|
||||
// Notify systemd of NEW main PID so it doesn't kill the service when we exit
|
||||
if let Ok(notify_socket) = std::env::var("NOTIFY_SOCKET") {
|
||||
if let Ok(sock) = std::os::unix::net::UnixDatagram::unbound() {
|
||||
let msg = format!("MAINPID={}\n", child_pid);
|
||||
if let Err(e) = sock.send_to(msg.as_bytes(), notify_socket) {
|
||||
error!("failed to send MAINPID to systemd: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => error!("failed to spawn new process: {}", e),
|
||||
}
|
||||
// Initiate graceful shutdown for this process
|
||||
|
||||
Reference in New Issue
Block a user