Skip to content

Commit

Permalink
Also listen for terminate events
Browse files Browse the repository at this point in the history
  • Loading branch information
MTRNord committed Nov 4, 2023
1 parent b00dfcd commit 7f990da
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions crates/erooster_smtp/src/servers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ use erooster_deps::{
color_eyre,
futures::{Sink, SinkExt},
serde_json,
tokio::{self, sync::Mutex},
tokio::{
self,
signal::unix::{signal, SignalKind},
sync::Mutex,
},
tracing::{self, error, info, instrument, warn},
yaque::{recovery::recover, ReceiverBuilder, Sender},
yaque::{recovery::recover, Receiver, ReceiverBuilder, Sender},
};

use self::sending::EmailPayload;
Expand Down Expand Up @@ -84,22 +88,28 @@ pub async fn start(
info!("Recovered queue successfully");
}

// Get SIGTERMs
let mut sigterms = signal(SignalKind::terminate())?;

match receiver {
Ok(receiver) => {
let receiver = Arc::new(Mutex::new(receiver));
let receiver_clone = Arc::clone(&receiver);
let receiver_clone_2 = Arc::clone(&receiver);

tokio::spawn(async move {
tokio::signal::ctrl_c()
.await
.expect("failed to listen for ctrl-c event");
info!("Received ctr-c. Cleaning up");
receiver_clone
.lock()
cleanup(receiver_clone).await;
});

tokio::spawn(async move {
sigterms
.recv()
.await
.save()
.expect("Unable to save queue");
exit(0);
.expect("failed to listen for ctrl-c event");
cleanup(receiver_clone_2).await;
});

loop {
Expand Down Expand Up @@ -137,3 +147,9 @@ pub async fn start(
}
}
}

async fn cleanup(receiver: Arc<Mutex<Receiver>>) {
info!("Received ctr-c. Cleaning up");
receiver.lock().await.save().expect("Unable to save queue");
exit(0);
}

0 comments on commit 7f990da

Please sign in to comment.