simplify shutdown signal handlers

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-01-28 20:02:29 +00:00
parent ed3cd99781
commit a567e314e9
3 changed files with 10 additions and 23 deletions

View file

@ -112,7 +112,7 @@ impl Server {
} }
#[inline] #[inline]
pub async fn until_shutdown(self: Arc<Self>) { pub async fn until_shutdown(self: &Arc<Self>) {
while self.running() { while self.running() {
self.signal.subscribe().recv().await.ok(); self.signal.subscribe().recv().await.ok();
} }

View file

@ -9,6 +9,7 @@ use std::{
use axum_server::Handle as ServerHandle; use axum_server::Handle as ServerHandle;
use conduwuit::{debug, debug_error, debug_info, error, info, Error, Result, Server}; use conduwuit::{debug, debug_error, debug_info, error, info, Error, Result, Server};
use futures::FutureExt;
use service::Services; use service::Services;
use tokio::{ use tokio::{
sync::broadcast::{self, Sender}, sync::broadcast::{self, Sender},
@ -109,28 +110,14 @@ pub(crate) async fn stop(services: Arc<Services>) -> Result<()> {
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
async fn signal(server: Arc<Server>, tx: Sender<()>, handle: axum_server::Handle) { async fn signal(server: Arc<Server>, tx: Sender<()>, handle: axum_server::Handle) {
loop { server
let sig: &'static str = server .clone()
.signal .until_shutdown()
.subscribe() .then(move |()| handle_shutdown(server, tx, handle))
.recv() .await;
.await
.expect("channel error");
if !server.running() {
handle_shutdown(&server, &tx, &handle, sig).await;
break;
}
}
} }
async fn handle_shutdown( async fn handle_shutdown(server: Arc<Server>, tx: Sender<()>, handle: axum_server::Handle) {
server: &Arc<Server>,
tx: &Sender<()>,
handle: &axum_server::Handle,
sig: &str,
) {
debug!("Received signal {sig}");
if let Err(e) = tx.send(()) { if let Err(e) = tx.send(()) {
error!("failed sending shutdown transaction to channel: {e}"); error!("failed sending shutdown transaction to channel: {e}");
} }

View file

@ -97,8 +97,8 @@ pub async fn watch(&self, user_id: &UserId, device_id: &DeviceId) -> Result {
); );
// Server shutdown // Server shutdown
let server_shutdown = self.services.server.clone().until_shutdown().boxed(); futures.push(self.services.server.until_shutdown().boxed());
futures.push(server_shutdown);
if !self.services.server.running() { if !self.services.server.running() {
return Ok(()); return Ok(());
} }