Cleanly shutdown on ctrl_c

Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
pep 2025-05-03 13:29:43 +02:00
commit e064263f96
3 changed files with 54 additions and 22 deletions

View file

@ -14,9 +14,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::hooks::{format_hook, Hook};
use crate::Error;
use log::debug;
use tokio::sync::mpsc;
use tokio::{signal::ctrl_c, sync::mpsc};
use xmpp::jid::{BareJid, Jid, ResourcePart};
use xmpp::parsers::message::MessageType;
use xmpp::{
@ -85,6 +86,9 @@ impl XmppClient {
pub async fn receive(&mut self, mut rx: mpsc::UnboundedReceiver<Hook>) {
loop {
tokio::select! {
_ = ctrl_c() => {
return; // Disconnecting
},
_ = self.next() => (),
wh = rx.recv() => {
if let Some(hook) = wh {
@ -114,4 +118,9 @@ impl XmppClient {
debug!("XMPP Bot Processed Hook");
}
}
pub async fn disconnect(self) -> Result<(), Error> {
log::info!("Disconnecting...");
Ok(self.agent.disconnect().await?)
}
}