pub struct Job(/* private fields */);
Expand description
A schedulable Job
Implementations§
Source§impl JobLocked
impl JobLocked
Sourcepub fn new<S, T>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
pub fn new<S, T>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
Create a new cron job.
let mut sched = JobScheduler::new();
// Run at second 0 of the 15th minute of the 6th, 8th, and 10th hour
// of any day in March and June that is a Friday of the year 2017.
let job = Job::new("0 15 6,8,10 * Mar,Jun Fri 2017", |_uuid, _lock| {
println!("{:?} Hi I ran", chrono::Utc::now());
});
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_tz<S, T, TZ>(
schedule: S,
timezone: TZ,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_tz<S, T, TZ>( schedule: S, timezone: TZ, run: T, ) -> Result<Self, JobSchedulerError>
Create a new cron job at a timezone.
let mut sched = JobScheduler::new();
// Run at second 0 of the 15th minute of the 6th, 8th, and 10th hour
// of any day in March and June that is a Friday of the year 2017.
let job = Job::new("0 15 6,8,10 * Mar,Jun Fri 2017", |_uuid, _lock| {
println!("{:?} Hi I ran", chrono::Utc::now());
});
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_async<S, T>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
pub fn new_async<S, T>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
Create a new async cron job.
let mut sched = JobScheduler::new();
// Run at second 0 of the 15th minute of the 6th, 8th, and 10th hour
// of any day in March and June that is a Friday of the year 2017.
let job = Job::new_async("0 15 6,8,10 * Mar,Jun Fri 2017", |_uuid, _lock| Box::pin( async move {
println!("{:?} Hi I ran", chrono::Utc::now());
}));
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_async_tz<S, T, TZ>(
schedule: S,
timezone: TZ,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_async_tz<S, T, TZ>( schedule: S, timezone: TZ, run: T, ) -> Result<Self, JobSchedulerError>
Create a new async cron job at a timezone.
let mut sched = JobScheduler::new();
// Run at second 0 of the 15th minute of the 6th, 8th, and 10th hour
// of any day in March and June that is a Friday of the year 2017.
let job = Job::new_async_tz("0 15 6,8,10 * Mar,Jun Fri 2017", Utc, |_uuid, _lock| Box::pin( async move {
println!("{:?} Hi I ran", chrono::Utc::now());
}));
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_cron_job<S, T, E>(
schedule: S,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_cron_job<S, T, E>( schedule: S, run: T, ) -> Result<Self, JobSchedulerError>
Create a new cron job.
let mut sched = JobScheduler::new();
// Run at second 0 of the 15th minute of the 6th, 8th, and 10th hour
// of any day in March and June that is a Friday of the year 2017.
let job = Job::new_cron_job("0 15 6,8,10 * Mar,Jun Fri 2017", |_uuid, _lock| {
println!("{:?} Hi I ran", chrono::Utc::now());
});
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_cron_job_async<S, T>(
schedule: S,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_cron_job_async<S, T>( schedule: S, run: T, ) -> Result<Self, JobSchedulerError>
Create a new async cron job using UTC as timezone.
let mut sched = JobScheduler::new();
// Run at second 0 of the 15th minute of the 6th, 8th, and 10th hour
// of any day in March and June that is a Friday of the year 2017.
let job = Job::new("0 15 6,8,10 * Mar,Jun Fri 2017", |_uuid, _lock| Box::pin( async move {
println!("{:?} Hi I ran", chrono::Utc::now());
}));
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_cron_job_async_tz<S, T, TZ>(
schedule: S,
timezone: TZ,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_cron_job_async_tz<S, T, TZ>( schedule: S, timezone: TZ, run: T, ) -> Result<Self, JobSchedulerError>
Create a new async cron job using a specified timezone.
let mut sched = JobScheduler::new();
// Run at second 0 of the 15th minute of the 6th, 8th, and 10th hour
// of any day in March and June that is a Friday of the year 2017.
let job = Job::new("0 15 6,8,10 * Mar,Jun Fri 2017", Utc, |_uuid, _lock| Box::pin( async move {
println!("{:?} Hi I ran", chrono::Utc::now());
}));
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_one_shot<T>(
duration: Duration,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_one_shot<T>( duration: Duration, run: T, ) -> Result<Self, JobSchedulerError>
Create a new one shot job.
This will schedule a job that is only run once after the duration has passed.
let mut sched = JobScheduler::new();
let job = Job::new_one_shot(Duration::from_secs(18), |_uuid, _l| {
println!("{:?} I'm only run once", chrono::Utc::now());
}
sched.add(job)
tokio::spawn(sched.start());
Above will run the code after 18 seconds, only once
Sourcepub fn new_one_shot_async<T>(
duration: Duration,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_one_shot_async<T>( duration: Duration, run: T, ) -> Result<Self, JobSchedulerError>
Create a new async one shot job.
This will schedule a job that is only run once after the duration has passed.
let mut sched = JobScheduler::new();
let job = Job::new_one_shot_async(Duration::from_secs(16), |_uuid, _l| {
Box::pin(async move {
info!("I'm only run once async");
})
})
.unwrap();
sched.add(job)
tokio::spawn(sched.start());
Above will run the code after 18 seconds, only once
Sourcepub fn new_one_shot_at_instant<T>(
instant: Instant,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_one_shot_at_instant<T>( instant: Instant, run: T, ) -> Result<Self, JobSchedulerError>
Create a new one shot job that runs at an instant
// Run after 20 seconds
let mut sched = JobScheduler::new();
let instant = std::time::Instant::now().checked_add(std::time::Duration::from_secs(20));
let job = Job::new_one_shot_at_instant(instant, |_uuid, _lock| println!("I run once after 20 seconds") );
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_one_shot_at_instant_async<T>(
instant: Instant,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_one_shot_at_instant_async<T>( instant: Instant, run: T, ) -> Result<Self, JobSchedulerError>
Create a new async one shot job that runs at an instant
// Run after 20 seconds
let mut sched = JobScheduler::new();
let instant = std::time::Instant::now().checked_add(std::time::Duration::from_secs(20));
let job = Job::new_one_shot_at_instant(instant, |_uuid, _lock| Box::pin(async move {println!("I run once after 20 seconds")}) );
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_repeated<T>(
duration: Duration,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_repeated<T>( duration: Duration, run: T, ) -> Result<Self, JobSchedulerError>
Create a new repeated job.
This is checked if it is running only after 500ms in 500ms intervals.
let mut sched = JobScheduler::new();
let job = Job::new_repeated(Duration::from_secs(8), |_uuid, _lock| {
println!("{:?} I'm repeated every 8 seconds", chrono::Utc::now());
}
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn new_repeated_async<T>(
duration: Duration,
run: T,
) -> Result<Self, JobSchedulerError>
pub fn new_repeated_async<T>( duration: Duration, run: T, ) -> Result<Self, JobSchedulerError>
Create a new async repeated job.
This is checked if it is running only after 500ms in 500ms intervals.
let mut sched = JobScheduler::new();
let job = Job::new_repeated_async(Duration::from_secs(8), |_uuid, _lock| Box::pin(async move {
println!("{:?} I'm repeated every 8 seconds", chrono::Utc::now());
}));
sched.add(job)
tokio::spawn(sched.start());
Sourcepub fn tick(&mut self) -> Result<bool, JobSchedulerError>
pub fn tick(&mut self) -> Result<bool, JobSchedulerError>
The tick
method returns a true if there was an invocation needed after it was last called
This method will also change the last tick on itself
Sourcepub async fn on_notifications_add(
&self,
job_scheduler: &JobsSchedulerLocked,
run: Box<OnJobNotification>,
states: Vec<JobState>,
) -> Result<Uuid, JobSchedulerError>
pub async fn on_notifications_add( &self, job_scheduler: &JobsSchedulerLocked, run: Box<OnJobNotification>, states: Vec<JobState>, ) -> Result<Uuid, JobSchedulerError>
Add a notification to run on a list of state notifications
Sourcepub async fn on_start_notification_add(
&self,
job_scheduler: &JobsSchedulerLocked,
on_start: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError>
pub async fn on_start_notification_add( &self, job_scheduler: &JobsSchedulerLocked, on_start: Box<OnJobNotification>, ) -> Result<Uuid, JobSchedulerError>
Run something when the task is started. Returns a UUID as handle for this notification. This
UUID needs to be used when you want to remove the notification handle using on_start_notification_remove
.
Sourcepub async fn on_notification_removal(
&self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
states: Option<Vec<JobState>>,
) -> Result<(Uuid, bool), JobSchedulerError>
pub async fn on_notification_removal( &self, job_scheduler: &JobsSchedulerLocked, notification_id: &Uuid, states: Option<Vec<JobState>>, ) -> Result<(Uuid, bool), JobSchedulerError>
Remove a notification optionally for a certain type of states
Sourcepub async fn on_start_notification_remove(
&self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError>
pub async fn on_start_notification_remove( &self, job_scheduler: &JobsSchedulerLocked, notification_id: &Uuid, ) -> Result<bool, JobSchedulerError>
Remove the notification when the task was started. Uses the same UUID that was returned by
on_start_notification_add
Sourcepub async fn on_done_notification_add(
&mut self,
job_scheduler: &JobsSchedulerLocked,
on_stop: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError>
pub async fn on_done_notification_add( &mut self, job_scheduler: &JobsSchedulerLocked, on_stop: Box<OnJobNotification>, ) -> Result<Uuid, JobSchedulerError>
Run something when the task is stopped. Returns a UUID as handle for this notification. This
UUID needs to be used when you want to remove the notification handle using on_stop_notification_remove
.
Sourcepub async fn on_done_notification_remove(
&mut self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError>
pub async fn on_done_notification_remove( &mut self, job_scheduler: &JobsSchedulerLocked, notification_id: &Uuid, ) -> Result<bool, JobSchedulerError>
Remove the notification when the task was stopped. Uses the same UUID that was returned by
on_done_notification_add
Sourcepub async fn on_removed_notification_add(
&mut self,
job_scheduler: &JobsSchedulerLocked,
on_removed: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError>
pub async fn on_removed_notification_add( &mut self, job_scheduler: &JobsSchedulerLocked, on_removed: Box<OnJobNotification>, ) -> Result<Uuid, JobSchedulerError>
Run something when the task was removed. Returns a UUID as handle for this notification. This
UUID needs to be used when you want to remove the notification handle using on_removed_notification_remove
.
Sourcepub async fn on_removed_notification_remove(
&mut self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError>
pub async fn on_removed_notification_remove( &mut self, job_scheduler: &JobsSchedulerLocked, notification_id: &Uuid, ) -> Result<bool, JobSchedulerError>
Remove the notification when the task was removed. Uses the same UUID that was returned by
on_removed_notification_add
Sourcepub async fn on_stop_notification_add(
&mut self,
job_scheduler: &JobsSchedulerLocked,
on_removed: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError>
pub async fn on_stop_notification_add( &mut self, job_scheduler: &JobsSchedulerLocked, on_removed: Box<OnJobNotification>, ) -> Result<Uuid, JobSchedulerError>
Run something when the task was removed. Returns a UUID as handle for this notification. This
UUID needs to be used when you want to remove the notification handle using on_removed_notification_remove
.
Sourcepub async fn on_stop_notification_remove(
&mut self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError>
pub async fn on_stop_notification_remove( &mut self, job_scheduler: &JobsSchedulerLocked, notification_id: &Uuid, ) -> Result<bool, JobSchedulerError>
Remove the notification when the task was removed. Uses the same UUID that was returned by
on_removed_notification_add
Sourcepub fn set_job_data(
&mut self,
job_data: JobStoredData,
) -> Result<(), JobSchedulerError>
pub fn set_job_data( &mut self, job_data: JobStoredData, ) -> Result<(), JobSchedulerError>
Override the job’s data for use in data storage
Sourcepub fn set_stop(&mut self, stop: bool) -> Result<(), JobSchedulerError>
pub fn set_stop(&mut self, stop: bool) -> Result<(), JobSchedulerError>
Set whether this job has been stopped
Sourcepub fn job_data(&mut self) -> Result<JobStoredData, JobSchedulerError>
pub fn job_data(&mut self) -> Result<JobStoredData, JobSchedulerError>
Get the job data