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
sourceimpl JobsSchedulerLocked
impl JobsSchedulerLocked
sourcepub fn init(&mut self) -> Result<(), JobSchedulerError>
pub fn init(&mut self) -> Result<(), JobSchedulerError>
Initialize the actors
sourcepub fn new() -> Result<Self, JobSchedulerError>
pub fn new() -> Result<Self, JobSchedulerError>
Create a new MetaDataStorage
and NotificationStore
using the SimpleMetadataStore
, SimpleNotificationStore
,
SimpleJobCode
and SimpleNotificationCode
implementation
sourcepub 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 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 fn add(&self, job: JobLocked) -> Result<Uuid, JobSchedulerError>
pub 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!");
}));
sourcepub fn remove(&self, to_be_removed: &Uuid) -> Result<(), JobSchedulerError>
pub 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!");
}))?;
sched.remove(job_id);
Note, the UUID of the job can be fetched calling .guid() on a Job.
sourcepub fn tick(&self) -> Result<(), JobSchedulerError>
pub fn tick(&self) -> Result<(), JobSchedulerError>
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));
}
sourcepub fn start(&self) -> Result<JoinHandle<()>, JobSchedulerError>
pub fn start(&self) -> Result<JoinHandle<()>, 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() {
eprintln!("Error on scheduler {:?}", e);
}
sourcepub fn time_till_next_job(
&mut self
) -> Result<Option<Duration>, JobSchedulerError>
pub 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
loop {
sched.tick();
std::thread::sleep(sched.time_till_next_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
Trait Implementations
sourceimpl Clone for JobsSchedulerLocked
impl Clone for JobsSchedulerLocked
Auto Trait Implementations
impl !RefUnwindSafe for JobsSchedulerLocked
impl Send for JobsSchedulerLocked
impl Sync for JobsSchedulerLocked
impl Unpin for JobsSchedulerLocked
impl !UnwindSafe for JobsSchedulerLocked
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more