From 9fbb6b133b6845eb0f0188f85ee40b7a9b5c20a5 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Sat, 22 Jan 2022 17:50:53 -0800 Subject: [PATCH] job: Make it possible to have a JobHandle not connected to a JobMonitor --- src/job.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/job.rs b/src/job.rs index 22e873c..7ec429e 100644 --- a/src/job.rs +++ b/src/job.rs @@ -109,7 +109,7 @@ pub struct JobHandleInner { job_id: JobId, /// Handle to the mpsc channel. - sender: Sender, + sender: Option, } /// A handle to the meta job. @@ -482,6 +482,14 @@ impl JobState { } impl JobHandleInner { + /// Creates a JobHandle that isn't connected to a JobMonitor. + pub fn null() -> Self { + Self { + job_id: JobId::new(), + sender: None, + } + } + /// Creates a new job with a distinct ID. /// /// This sends out a Creation message with the metadata. @@ -594,8 +602,12 @@ impl JobHandleInner { let event = Event::new(self.job_id, payload); - self.sender.send(event) - .map_err(|e| ColmenaError::unknown(Box::new(e)))?; + if let Some(sender) = &self.sender { + sender.send(event) + .map_err(|e| ColmenaError::unknown(Box::new(e)))?; + } else { + log::debug!("Sending event: {:?}", event); + } Ok(()) } @@ -609,7 +621,7 @@ impl MetaJobHandle { { let normal_handle = Arc::new(JobHandleInner { job_id: self.job_id, - sender: self.sender.clone(), + sender: Some(self.sender.clone()), }); match f(normal_handle).await { @@ -787,6 +799,11 @@ impl Display for JobStats { } } +/// Returns a JobHandle that is not connected to a JobMonitor. +pub fn null_job_handle() -> JobHandle { + Arc::new(JobHandleInner::null()) +} + /// Returns a textual description of a list of nodes. /// /// Example: "alpha, beta, and 5 other nodes"