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!");
}));

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!");
}))?;
sched.remove(job_id);

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

The tick method increments time for the JobScheduler and executes any pending jobs. It is recommended to sleep for at least 500 milliseconds between invocations of this method. This is kept public if you’re running this yourself. It is better to call the start method if you want all of this automated for you.

loop {
    sched.tick();
    std::thread::sleep(Duration::from_millis(500));
}

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() {
        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

loop {
    sched.tick();
    std::thread::sleep(sched.time_till_next_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