Struct tokio_cron_scheduler::JobScheduler
source · 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§
source§impl JobsSchedulerLocked
impl JobsSchedulerLocked
sourcepub async fn init(&mut self) -> Result<(), JobSchedulerError>
pub async fn init(&mut self) -> Result<(), JobSchedulerError>
Initialize the actors
sourcepub async fn new() -> Result<Self, JobSchedulerError>
pub async fn new() -> Result<Self, JobSchedulerError>
Create a new MetaDataStorage
and NotificationStore
using the SimpleMetadataStore
, SimpleNotificationStore
,
SimpleJobCode
and SimpleNotificationCode
implementation
sourcepub async fn new_with_storage_and_code(
metadata_storage: Box<dyn MetaDataStorage + Send + Sync>,
notification_storage: Box<dyn NotificationStore + Send + Sync>,
job_code: Box<dyn JobCode + Send + Sync>,
notification_code: Box<dyn NotificationCode + Send + Sync>
) -> Result<Self, JobSchedulerError>
pub async fn new_with_storage_and_code(
metadata_storage: Box<dyn MetaDataStorage + Send + Sync>,
notification_storage: Box<dyn NotificationStore + Send + Sync>,
job_code: Box<dyn JobCode + Send + Sync>,
notification_code: Box<dyn NotificationCode + Send + Sync>
) -> Result<Self, JobSchedulerError>
Create a new JobsSchedulerLocked
using custom metadata and notification runners, job and notification
code providers
sourcepub async fn add(&self, job: JobLocked) -> Result<Uuid, JobSchedulerError>
pub async fn add(&self, job: JobLocked) -> Result<Uuid, JobSchedulerError>
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;
sourcepub async fn remove(&self, to_be_removed: &Uuid) -> Result<(), JobSchedulerError>
pub async fn remove(&self, to_be_removed: &Uuid) -> Result<(), JobSchedulerError>
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.
sourcepub async fn start(&self) -> Result<(), JobSchedulerError>
pub async fn start(&self) -> Result<(), JobSchedulerError>
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);
}
sourcepub async fn time_till_next_job(
&mut self
) -> Result<Option<Duration>, JobSchedulerError>
pub async fn time_till_next_job(
&mut self
) -> Result<Option<Duration>, JobSchedulerError>
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
sourcepub async fn next_tick_for_job(
&mut self,
job_id: Uuid
) -> Result<Option<DateTime<Utc>>, JobSchedulerError>
pub async fn next_tick_for_job(
&mut self,
job_id: Uuid
) -> Result<Option<DateTime<Utc>>, JobSchedulerError>
next_tick_for_job
returns the date/time for when the next tick will
be for a job
sourcepub async fn shutdown(&mut self) -> Result<(), JobSchedulerError>
pub async fn shutdown(&mut self) -> Result<(), JobSchedulerError>
Shut the scheduler down
sourcepub fn set_shutdown_handler(
&mut self,
job: Box<dyn FnMut() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>
)
pub fn set_shutdown_handler(
&mut self,
job: Box<dyn FnMut() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>
)
Code that is run after the shutdown was run
sourcepub fn remove_shutdown_handler(&mut self)
pub fn remove_shutdown_handler(&mut self)
Remove the shutdown handler