From 17bd44f840f0751c852819248c2b22cb65fe06e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 7 Sep 2023 10:22:14 +0100 Subject: [PATCH] added optional name to TaskManager --- common/task/src/manager.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/common/task/src/manager.rs b/common/task/src/manager.rs index 670f9c7bb6..96210776f9 100644 --- a/common/task/src/manager.rs +++ b/common/task/src/manager.rs @@ -49,6 +49,9 @@ pub enum TaskStatus { /// shutdown. Keeps track of if task stop unexpectedly, such as in a panic. #[derive(Debug)] pub struct TaskManager { + // optional name assigned to the task manager that all subscribed task clients will inherit + name: Option, + // These channels have the dual purpose of signalling it's time to shutdown, but also to keep // track of which tasks we are still waiting for. notify_tx: watch::Sender<()>, @@ -81,6 +84,7 @@ impl Default for TaskManager { // there is a listener. let (task_status_tx, task_status_rx) = futures::channel::mpsc::channel(128); Self { + name: None, notify_tx, notify_rx: Some(notify_rx), shutdown_timer_secs: DEFAULT_SHUTDOWN_TIMER_SECS, @@ -102,6 +106,12 @@ impl TaskManager { } } + #[must_use] + pub fn named>(mut self, name: S) -> Self { + self.name = Some(name.into()); + self + } + #[cfg(not(target_arch = "wasm32"))] pub async fn catch_interrupt(mut self) -> Result<(), SentError> { let res = crate::wait_for_signal_and_error(&mut self).await; @@ -116,7 +126,7 @@ impl TaskManager { } pub fn subscribe(&self) -> TaskClient { - TaskClient::new( + let task_client = TaskClient::new( self.notify_rx .as_ref() .expect("Unable to subscribe to shutdown notifier that is already shutdown") @@ -124,7 +134,13 @@ impl TaskManager { self.task_return_error_tx.clone(), self.task_drop_tx.clone(), self.task_status_tx.clone(), - ) + ); + + if let Some(name) = &self.name { + task_client.named(format!("{name}-child")) + } else { + task_client + } } pub fn signal_shutdown(&self) -> Result<(), SendError<()>> {