pub struct JobHandle { /* private fields */ }Expand description
A job-handle enables cancelling a job and awaiting results
A JobHandle can be awaited directly. It returns a
Result<JobCompletion, JobHandleError>
See JobCompletion and JobHandleError for details.
When the JobHandle is dropped a cancellation message is sent to the job
task.
Implementations§
Source§impl JobHandle
impl JobHandle
pub fn is_finished(&self) -> bool
Sourcepub fn cancel(&self) -> Result<(), JobHandleError>
pub fn cancel(&self) -> Result<(), JobHandleError>
sends cancel message to job and returns immediately.
note: await the JobHandle after calling cancel() to ensure the job has
ended and obtain a JobCompletion
Basic example:
use neptune_job_queue::JobQueue;
use neptune_job_queue::JobCompletion;
use neptune_job_queue::traits::Job;
use neptune_job_queue::errors::JobHandleError;
async fn add_and_cancel_job(job_queue: &mut JobQueue<u8>, job: Box<dyn Job>) -> Result<JobCompletion, JobHandleError> {
let job_priority: u8 = 10;
let job_handle = job_queue.add_job(job, job_priority).unwrap();
// some time later...
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
job_handle.cancel()?;
let job_completion_result = job_handle.await;
assert!(matches!(job_completion_result, Ok(JobCompletion::Cancelled)));
job_completion_result
}Sometimes it is necessary to listen for an application message that the job needs to cancel. This can be achieved with tokio::select!{}
Example:
use neptune_job_queue::JobQueue;
use neptune_job_queue::JobCompletion;
use neptune_job_queue::traits::Job;
use neptune_job_queue::errors::JobHandleError;
async fn do_some_work(
job_queue: &mut JobQueue<u8>,
job: Box<dyn Job>,
cancel_work_rx: tokio::sync::oneshot::Receiver<()>,
) -> Result<JobCompletion, JobHandleError> {
// add the job to queue
let job_priority: u8 = 10;
let job_handle = job_queue.add_job(job, job_priority).unwrap();
// pin job_handle, so borrow checker knows the address can't change
// and it is safe to use in both select branches
tokio::pin!(job_handle);
// execute job and simultaneously listen for cancel msg from elsewhere
let job_completion_result = tokio::select! {
// case: job completion.
completion = &mut job_handle => completion,
// case: sender cancelled, or sender dropped.
_ = cancel_work_rx => {
job_handle.cancel()?;
job_handle.await
}
};
job_completion_result
}Trait Implementations§
Source§impl Future for JobHandle
impl Future for JobHandle
Source§type Output = Result<JobCompletion, JobHandleError>
type Output = Result<JobCompletion, JobHandleError>
The type of value produced on completion.
Auto Trait Implementations§
impl !RefUnwindSafe for JobHandle
impl !UnwindSafe for JobHandle
impl Freeze for JobHandle
impl Send for JobHandle
impl Sync for JobHandle
impl Unpin for JobHandle
impl UnsafeUnpin for JobHandle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more