pub struct TaskSpawner<T>{ /* 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>
impl<T> TaskSpawner<T>
Sourcepub fn spawn(&self, task: TaskResult<Task<T>>) -> TaskResult<()>
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).
Sourcepub async fn spawn_get_id(&self, task: TaskResult<Task<T>>) -> TaskResult<usize>
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.