use std::sync::Arc;
use tokio::sync::Mutex;
use crate::{CtrlFuture, ProcessControlHandler, RuntimeControlMessage};
#[derive(Debug, Clone)]
pub struct RuntimeHandle {
control_ch: Arc<Mutex<tokio::sync::mpsc::Sender<RuntimeControlMessage>>>,
}
impl RuntimeHandle {
pub(crate) fn new(
control_ch: Arc<Mutex<tokio::sync::mpsc::Sender<RuntimeControlMessage>>>,
) -> Self {
Self { control_ch }
}
pub fn control(&self, msg: RuntimeControlMessage) -> CtrlFuture<'_> {
Box::pin(async move {
let ch = self.control_ch.lock().await;
let _ = ch.send(msg).await;
})
}
pub fn custom<T>(&self, message: T) -> CtrlFuture<'_>
where
T: std::any::Any + Send + Sync + 'static,
{
self.control(RuntimeControlMessage::Custom(Arc::new(message)))
}
}
impl ProcessControlHandler for RuntimeHandle {
fn shutdown(&self) -> CtrlFuture<'_> {
self.control(RuntimeControlMessage::Shutdown)
}
fn reload(&self) -> CtrlFuture<'_> {
self.control(RuntimeControlMessage::Reload)
}
}