pub struct JobScheduler {
    pub context: Arc<Context>,
    pub inited: Arc<RwLock<bool>>,
    pub job_creator: Arc<RwLock<JobCreator>>,
    pub job_deleter: Arc<RwLock<JobDeleter>>,
    pub job_runner: Arc<RwLock<JobRunner>>,
    pub notification_creator: Arc<RwLock<NotificationCreator>>,
    pub notification_deleter: Arc<RwLock<NotificationDeleter>>,
    pub notification_runner: Arc<RwLock<NotificationRunner>>,
    pub scheduler: Arc<RwLock<Scheduler>>,
    pub shutdown_notifier: Option<Arc<RwLock<Box<dyn FnMut() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>>>>,
}
Expand description

The JobScheduler contains and executes the scheduled jobs.

Fields§

§context: Arc<Context>§inited: Arc<RwLock<bool>>§job_creator: Arc<RwLock<JobCreator>>§job_deleter: Arc<RwLock<JobDeleter>>§job_runner: Arc<RwLock<JobRunner>>§notification_creator: Arc<RwLock<NotificationCreator>>§notification_deleter: Arc<RwLock<NotificationDeleter>>§notification_runner: Arc<RwLock<NotificationRunner>>§scheduler: Arc<RwLock<Scheduler>>§shutdown_notifier: Option<Arc<RwLock<Box<dyn FnMut() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>>>>

Implementations§

Get whether the scheduler is initialized

Initialize the actors

Create a new MetaDataStorage and NotificationStore using the SimpleMetadataStore, SimpleNotificationStore, SimpleJobCode and SimpleNotificationCode implementation

Create a new JobsSchedulerLocked using custom metadata and notification runners, job and notification code providers

Add a job to the JobScheduler

use tokio_cron_scheduler::{Job, JobScheduler, JobToRun};
let mut sched = JobScheduler::new();
sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
    println!("I get executed every 10 seconds!");
})).await;

Remove a job from the JobScheduler

use tokio_cron_scheduler::{Job, JobScheduler, JobToRun};
let mut sched = JobScheduler::new();
let job_id = sched.add(Job::new("1/10 * * * * *".parse().unwrap(), || {
    println!("I get executed every 10 seconds!");
}))?.await;
sched.remove(job_id).await;

Note, the UUID of the job can be fetched calling .guid() on a Job.

The start spawns a Tokio task where it loops. Every 500ms it runs the tick method to increment any any pending jobs.

if let Err(e) = sched.start().await {
        eprintln!("Error on scheduler {:?}", e);
    }

The time_till_next_job method returns the duration till the next job is supposed to run. This can be used to sleep until then without waking up at a fixed interval.AsMut

next_tick_for_job returns the date/time for when the next tick will be for a job

Shut the scheduler down

Code that is run after the shutdown was run

Remove the shutdown handler

Get the context

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more