Skip to main content

TaskSpawner

Struct TaskSpawner 

Source
pub struct TaskSpawner<T>
where T: TimeZone + Clone + Send + 'static,
{ /* private fields */ }
Expand description

A cloneable handle for adding tasks to a running TaskScheduler at runtime.

The type-erased SchedulerHandle cannot add tasks because a Task<T> carries the scheduler’s timezone type T and its step closures. A TaskSpawner<T> keeps that type, so it can hand fully-built tasks to the scheduler from anywhere (another task, an API handler, a signal handler). Obtain one with TaskScheduler::spawner before calling run.

Spawned tasks are picked up as soon as the scheduler loop wakes, which a spawn triggers, so they are only processed while the scheduler is running.

§Examples

let mut scheduler = TaskScheduler::new(50, chrono::Local);
let spawner = scheduler.spawner();

// Add a task from another task once the scheduler is running.
tokio::spawn(async move {
    let _ = spawner.spawn(
        TaskBuilder::new(chrono::Local)
            .every("* * * * * * *")
            .add_step_default(|_ctx| async { Ok(tasklet::task::TaskStepStatusOk::Success) })
            .build(),
    );
});

scheduler.run_until(tokio::time::sleep(Duration::from_millis(150))).await;

Implementations§

Source§

impl<T> TaskSpawner<T>
where T: TimeZone + Clone + Send + 'static, <T as TimeZone>::Offset: Send,

Source

pub fn spawn(&self, task: TaskResult<Task<T>>) -> TaskResult<()>

Queue a task to be added to the running scheduler.

A build error in task is returned immediately. The task is otherwise queued fire-and-forget; if it is rejected on the scheduler side (duplicate name, unknown pool) the rejection is logged and published as SchedulerEvent::TaskRejected. Use spawn_get_id to await the assigned id or the rejection.

Returns TaskError::ExecutionError once the scheduler has stopped (its run or run_until returned).

Source

pub async fn spawn_get_id(&self, task: TaskResult<Task<T>>) -> TaskResult<usize>

Queue a task and await the id the scheduler assigns it.

A build error is returned immediately; a duplicate name is reported as TaskError::DuplicateTaskName, an unknown pool as TaskError::UnknownPool, and a stopped scheduler as TaskError::ExecutionError.

Trait Implementations§

Source§

impl<T> Clone for TaskSpawner<T>
where T: TimeZone + Clone + Send + 'static,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Forget for TaskSpawner<T>

§

impl<T> Freeze for TaskSpawner<T>
where UnboundedSender<SpawnRequest<T>>: Freeze,

§

impl<T> Move for TaskSpawner<T>

§

impl<T> RefUnwindSafe for TaskSpawner<T>
where UnboundedSender<SpawnRequest<T>>: RefUnwindSafe,

§

impl<T> Send for TaskSpawner<T>
where UnboundedSender<SpawnRequest<T>>: Send,

§

impl<T> Sync for TaskSpawner<T>
where UnboundedSender<SpawnRequest<T>>: Sync,

§

impl<T> Unpin for TaskSpawner<T>
where UnboundedSender<SpawnRequest<T>>: Unpin,

§

impl<T> UnsafeUnpin for TaskSpawner<T>
where UnboundedSender<SpawnRequest<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for TaskSpawner<T>
where UnboundedSender<SpawnRequest<T>>: UnwindSafe,

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.