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]
pub async fn until_shutdown(self: Arc<Self>) {
pub async fn until_shutdown(self: &Arc<Self>) {
while self.running() {
self.signal.subscribe().recv().await.ok();
}

View file

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