wire_framework/service/
shutdown_hook.rs1use std::{fmt, future::Future};
2
3use futures::{FutureExt, future::BoxFuture};
4
5use crate::{IntoContext, TaskId};
6
7pub struct ShutdownHook {
15 pub(crate) id: TaskId,
16 pub(crate) future: BoxFuture<'static, eyre::Result<()>>,
17}
18
19impl fmt::Debug for ShutdownHook {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 f.debug_struct("ShutdownHook")
22 .field("name", &self.id)
23 .finish()
24 }
25}
26
27impl ShutdownHook {
28 pub fn new(
29 name: &'static str,
30 hook: impl Future<Output = eyre::Result<()>> + Send + 'static,
31 ) -> Self {
32 Self {
33 id: name.into(),
34 future: hook.boxed(),
35 }
36 }
37}
38
39impl IntoContext for ShutdownHook {
40 fn into_context(
41 self,
42 context: &mut super::ServiceContext<'_>,
43 ) -> Result<(), crate::WiringError> {
44 context.add_shutdown_hook(self);
45 Ok(())
46 }
47}