use std::{fmt, future::Future};
use futures::{FutureExt, future::BoxFuture};
use crate::{IntoContext, TaskId};
pub struct ShutdownHook {
pub(crate) id: TaskId,
pub(crate) future: BoxFuture<'static, eyre::Result<()>>,
}
impl fmt::Debug for ShutdownHook {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ShutdownHook")
.field("name", &self.id)
.finish()
}
}
impl ShutdownHook {
pub fn new(
name: &'static str,
hook: impl Future<Output = eyre::Result<()>> + Send + 'static,
) -> Self {
Self {
id: name.into(),
future: hook.boxed(),
}
}
}
impl IntoContext for ShutdownHook {
fn into_context(
self,
context: &mut super::ServiceContext<'_>,
) -> Result<(), crate::WiringError> {
context.add_shutdown_hook(self);
Ok(())
}
}