spawner.rs
1 use fedimint_core::task::TaskGroup; 2 use fedimint_logging::LOG_CONSENSUS; 3 use tracing::warn; 4 5 #[derive(Clone)] 6 pub struct Spawner { 7 task_group: TaskGroup, 8 } 9 10 impl Spawner { 11 pub fn new(task_group: TaskGroup) -> Self { 12 Self { task_group } 13 } 14 } 15 16 impl aleph_bft::SpawnHandle for Spawner { 17 fn spawn(&self, name: &str, task: impl futures::Future<Output = ()> + Send + 'static) { 18 self.task_group.spawn(name, move |_| task); 19 } 20 21 fn spawn_essential( 22 &self, 23 name: &str, 24 task: impl futures::Future<Output = ()> + Send + 'static, 25 ) -> aleph_bft::TaskHandle { 26 let (sender, receiver) = futures::channel::oneshot::channel(); 27 28 self.task_group.spawn(name, move |_| async { 29 task.await; 30 31 if sender.send(()).is_err() { 32 warn!(target: LOG_CONSENSUS, "Unable to send essential spawned task completion. Are we shutting down?"); 33 } 34 }); 35 36 Box::pin(async move { receiver.await.map_err(|_| ()) }) 37 } 38 }