Struct Scheduler

Source
pub struct Scheduler<Tz: TimeZoneExt> { /* private fields */ }
Expand description

The interface for interacting with the scheduler.

When launching a scheduler, the scheduler and its service are created simultaneously with a channel connecting them. Job insert and remove messages are sent to the service for automatic management. When the scheduler is dropped, so too will the service its attached to exit.

use smol::Timer;
use chrono::offset::Local;

let (mut scheduler, service) = Scheduler::<Local>::launch(Timer::after);

// Creates a job which executes every 3 seconds.
let job = Job::cron("1/3 * * * * *").unwrap();
let fizz_id = scheduler.insert(job, |id| println!("Fizz")).await;

// Creates a job which executes every 5 seconds.
let job = Job::cron("1/5 * * * * *").unwrap();
let buzz_id = scheduler.insert(job, |id| println!("Buzz")).await;

service.await;

Implementations§

Source§

impl<Tz: TimeZoneExt + 'static> Scheduler<Tz>
where Tz::Offset: Send + Sync,

Source

pub async fn insert( &mut self, job: Job<Tz>, command: impl Fn(JobId) + Send + Sync + 'static, ) -> JobId

Insert a job into the scheduler with the command to call when scheduled.

// Creates a job which executes every 3 seconds.
let job = Job::cron("1/3 * * * * *").unwrap();
let fizz_id = scheduler.insert(job, |id| println!("Fizz")).await;
Source

pub async fn remove(&mut self, job: JobId)

Remove a scheduled job from the scheduler.

scheduler.remove(fizz_id).await;
Source

pub fn launch<F, T, X>( timer: T, ) -> (Self, impl Future<Output = ()> + Send + Sync + 'static)
where F: Future<Output = X> + Send + Sync, T: Fn(Duration) -> F + Send + Sync + 'static,

Initializes the scheduler and its connected service.

The API is designed to not rely on any async runtimes. This is achieved by returning a future to allow the caller to decide how it should be executed, and taking a function for handling sleeps. You can choose to spawn the returned future, or avoid spawning altgether and await it directly from the same thread.

§Smol runtime
let (mut scheduler, sched_service) = Scheduler::<Local>::launch(smol::Timer::after);
smol::spawn(sched_service).detach();
§Tokio runtime
let (mut scheduler, sched_service) = Scheduler::<Local>::launch(tokio::time::sleep);
tokio::spawn(sched_service);

Auto Trait Implementations§

§

impl<Tz> Freeze for Scheduler<Tz>

§

impl<Tz> !RefUnwindSafe for Scheduler<Tz>

§

impl<Tz> Send for Scheduler<Tz>
where <Tz as TimeZone>::Offset: Send,

§

impl<Tz> Sync for Scheduler<Tz>
where <Tz as TimeZone>::Offset: Send,

§

impl<Tz> Unpin for Scheduler<Tz>

§

impl<Tz> !UnwindSafe for Scheduler<Tz>

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> 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, 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.