#[cfg(all(feature = "rt", feature = "tokio"))]
mod gate {
use std::time::Duration;
use tokio::{sync::mpsc, time};
use qsu::{
async_trait,
rt::{
InitCtx, RunCtx, RunEnv, SrvAppRt, SvcEvt, TermCtx, TokioServiceHandler,
UserSig
},
tracing
};
#[derive(Debug)]
enum MyError {}
struct MyService {
rx_term: mpsc::UnboundedReceiver<()>
}
#[async_trait]
impl TokioServiceHandler for MyService {
type AppErr = MyError;
async fn init(&mut self, ictx: &mut InitCtx) -> Result<(), Self::AppErr> {
ictx.report(Some("Initializing service handler"));
ictx.report(Some("Finalized service handler initialization"));
Ok(())
}
async fn run(&mut self, _re: &RunEnv) -> Result<(), Self::AppErr> {
tracing::info!("Running TokioServiceHandler::run()");
tracing::info!("Wait for termination event for up to 4 seconds ..");
tokio::select! {
_ = self.rx_term.recv() => {
tracing::info!("Got kill signal from svc");
}
() = time::sleep(Duration::from_secs(4)) => {
tracing::info!("Got timeout");
}
}
Ok(())
}
async fn shutdown(
&mut self,
tctx: &mut TermCtx
) -> Result<(), Self::AppErr> {
tracing::info!("Running TokioServiceHandler::shutdown()");
tctx.report(Some("Terminating service handler"));
tctx.report(Some("Finalized service handler termination"));
Ok(())
}
}
pub fn main() {
let svcname = qsu::default_service_name().unwrap();
let (tx, rx) = mpsc::unbounded_channel::<()>();
let svcevt_handler = move |msg| match msg {
SvcEvt::Shutdown(_) => {
tracing::debug!("Shutdown event from service subsystem");
let _ = tx.send(());
}
SvcEvt::User(us) => match us {
UserSig::Sig1 => {
log::info!("User signal 1");
}
UserSig::Sig2 => {
log::info!("User signal 2");
}
},
_ => {}
};
let svcrt = MyService { rx_term: rx };
let apprt = SrvAppRt::Tokio {
rtbldr: None,
svcevt_handler: Box::new(svcevt_handler),
rt_handler: Box::new(svcrt)
};
let rctx = RunCtx::new(&svcname);
rctx.run(apprt).unwrap();
}
}
fn main() {
#[cfg(all(feature = "rt", feature = "tokio"))]
gate::main();
#[cfg(not(all(feature = "rt", feature = "tokio")))]
println!("simpletokio example requires the rt and tokio features");
}