Skip to main content

JobHandle

Struct JobHandle 

Source
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

Source

pub fn is_finished(&self) -> bool

Source

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
}
Source

pub fn job_id(&self) -> JobId

obtain the job identifier

Trait Implementations§

Source§

impl Debug for JobHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for JobHandle

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Future for JobHandle

Source§

type Output = Result<JobCompletion, JobHandleError>

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more