1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
#[cfg(not(feature = "has_bytes"))]
use crate::job::job_data::{JobState, JobType};
#[cfg(feature = "has_bytes")]
use crate::job::job_data_prost::{JobState, JobType};
use crate::job_scheduler::JobsSchedulerLocked;
use crate::{JobScheduler, JobSchedulerError, JobStoredData};
use chrono::{DateTime, Offset, TimeZone, Utc};
use cron_job::CronJob;
use croner::Cron;
use non_cron_job::NonCronJob;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::oneshot::Receiver;
use tracing::error;
use uuid::Uuid;
mod builder;
mod creator;
mod cron_job;
mod deleter;
#[cfg(not(feature = "has_bytes"))]
pub mod job_data;
#[cfg(feature = "has_bytes")]
pub mod job_data_prost;
mod non_cron_job;
mod runner;
pub mod to_code;
use crate::notification::{NotificationCreator, NotificationDeleter};
use crate::JobSchedulerError::ParseSchedule;
pub use builder::JobBuilder;
pub use creator::JobCreator;
pub use deleter::JobDeleter;
pub use runner::JobRunner;
pub type JobId = Uuid;
pub type NotificationId = Uuid;
pub type JobToRun = dyn FnMut(JobId, JobsSchedulerLocked) + Send + Sync;
pub type JobToRunAsync =
dyn FnMut(JobId, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync;
pub type OnJobNotification = dyn FnMut(JobId, NotificationId, JobState) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync;
fn nop(_uuid: Uuid, _jobs: JobsSchedulerLocked) {
// Do nothing
}
fn nop_async(_uuid: Uuid, _jobs: JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>> {
Box::pin(async move {})
}
///
/// A schedulable Job
#[derive(Clone)]
pub struct JobLocked(pub(crate) Arc<RwLock<Box<dyn Job + Send + Sync>>>);
pub trait Job {
fn is_cron_job(&self) -> bool;
fn schedule(&self) -> Option<Cron>;
fn repeated_every(&self) -> Option<u64>;
fn last_tick(&self) -> Option<DateTime<Utc>>;
fn set_last_tick(&mut self, tick: Option<DateTime<Utc>>);
fn next_tick(&self) -> Option<DateTime<Utc>>;
fn set_next_tick(&mut self, tick: Option<DateTime<Utc>>);
fn set_count(&mut self, count: u32);
fn count(&self) -> u32;
fn increment_count(&mut self);
fn job_id(&self) -> Uuid;
fn job_type(&self) -> JobType;
fn ran(&self) -> bool;
fn set_ran(&mut self, ran: bool);
fn stop(&self) -> bool;
fn set_stopped(&mut self);
fn set_started(&mut self);
fn job_data_from_job(&mut self) -> Result<Option<JobStoredData>, JobSchedulerError>;
fn set_job_data(&mut self, job_data: JobStoredData) -> Result<(), JobSchedulerError>;
fn run(&mut self, jobs: JobScheduler) -> Receiver<bool>;
fn fixed_offset_west(&self) -> i32;
}
impl JobLocked {
/// Create a new cron job.
///
/// ```rust,ignore
/// 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());
/// ```
pub fn new<S, T>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) + Send + Sync,
S: ToString,
{
Self::new_tz(schedule, Utc, run)
}
/// Create a new cron job at a timezone.
///
/// ```rust,ignore
/// 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());
/// ```
pub fn new_tz<S, T, TZ>(schedule: S, timezone: TZ, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) + Send + Sync,
S: ToString,
TZ: TimeZone,
{
let schedule = Self::schedule_to_cron(schedule)?;
let time_offset_seconds = timezone
.offset_from_utc_datetime(&Utc::now().naive_local())
.fix()
.local_minus_utc();
let schedule = Cron::new(&schedule)
.with_seconds_required()
.with_dom_and_dow()
.parse()
.map_err(|_| ParseSchedule)?;
let job_id = Uuid::new_v4();
Ok(Self(Arc::new(RwLock::new(Box::new(CronJob {
data: JobStoredData {
id: Some(job_id.into()),
last_updated: None,
last_tick: None,
next_tick: schedule
.iter_from(Utc::now().with_timezone(&timezone))
.next()
.map(|t| t.timestamp() as u64)
.unwrap_or(0),
job_type: JobType::Cron.into(),
count: 0,
extra: vec![],
ran: false,
stopped: false,
#[cfg(feature = "has_bytes")]
job: Some(job_data_prost::job_stored_data::Job::CronJob(
job_data_prost::CronJob {
schedule: schedule.pattern.to_string(),
},
)),
#[cfg(not(feature = "has_bytes"))]
job: Some(job_data::job_stored_data::Job::CronJob(job_data::CronJob {
schedule: schedule.pattern.to_string(),
})),
time_offset_seconds,
},
run: Box::new(run),
run_async: Box::new(nop_async),
async_job: false,
})))))
}
/// Create a new async cron job.
///
/// ```rust,ignore
/// 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());
/// ```
pub fn new_async<S, T>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync,
S: ToString,
{
Self::new_async_tz(schedule, Utc, run)
}
/// Create a new async cron job at a timezone.
///
/// ```rust,ignore
/// 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());
/// ```
pub fn new_async_tz<S, T, TZ>(
schedule: S,
timezone: TZ,
run: T,
) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync,
S: ToString,
TZ: TimeZone,
{
let schedule = Self::schedule_to_cron(schedule)?;
let time_offset_seconds = timezone
.offset_from_utc_datetime(&Utc::now().naive_local())
.fix()
.local_minus_utc();
let schedule = Cron::new(&schedule)
.with_seconds_required()
.with_dom_and_dow()
.parse()
.map_err(|_| ParseSchedule)?;
let job_id = Uuid::new_v4();
Ok(Self(Arc::new(RwLock::new(Box::new(CronJob {
data: JobStoredData {
id: Some(job_id.into()),
last_updated: None,
last_tick: None,
next_tick: schedule
.iter_from(Utc::now().with_timezone(&timezone))
.next()
.map(|t| t.timestamp() as u64)
.unwrap_or(0),
job_type: JobType::Cron.into(),
count: 0,
extra: vec![],
ran: false,
stopped: false,
#[cfg(feature = "has_bytes")]
job: Some(job_data_prost::job_stored_data::Job::CronJob(
job_data_prost::CronJob {
schedule: schedule.pattern.to_string(),
},
)),
#[cfg(not(feature = "has_bytes"))]
job: Some(job_data::job_stored_data::Job::CronJob(job_data::CronJob {
schedule: schedule.pattern.to_string(),
})),
time_offset_seconds,
},
run: Box::new(nop),
run_async: Box::new(run),
async_job: true,
})))))
}
/// Create a new cron job.
///
/// ```rust,ignore
/// 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());
/// ```
pub fn new_cron_job<S, T, E>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) + Send + Sync,
S: ToString,
{
JobLocked::new(schedule, run)
}
/// Create a new async cron job using UTC as timezone.
///
/// ```rust,ignore
/// 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());
/// ```
pub fn new_cron_job_async<S, T>(schedule: S, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync,
S: ToString,
{
JobLocked::new_async_tz(schedule, Utc, run)
}
/// Create a new async cron job using a specified timezone.
///
/// ```rust,ignore
/// 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());
/// ```
pub fn new_cron_job_async_tz<S, T, TZ>(
schedule: S,
timezone: TZ,
run: T,
) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync,
S: ToString,
TZ: TimeZone,
{
JobLocked::new_async_tz(schedule, timezone, run)
}
fn make_one_shot_job(
duration: Duration,
run: Box<JobToRun>,
run_async: Box<JobToRunAsync>,
async_job: bool,
) -> Result<Self, JobSchedulerError> {
let id = Uuid::new_v4();
let job = NonCronJob {
run,
run_async,
async_job,
data: JobStoredData {
id: Some(id.into()),
last_updated: None,
last_tick: None,
next_tick: SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|p| p.as_secs())
.unwrap_or(0)
+ duration.as_secs(),
job_type: JobType::OneShot.into(),
count: 0,
extra: vec![],
ran: false,
stopped: false,
#[cfg(feature = "has_bytes")]
job: Some(job_data_prost::job_stored_data::Job::NonCronJob(
job_data_prost::NonCronJob {
repeating: false,
repeated_every: duration.as_secs(),
},
)),
#[cfg(not(feature = "has_bytes"))]
job: Some(job_data::job_stored_data::Job::NonCronJob(
job_data::NonCronJob {
repeating: false,
repeated_every: duration.as_secs(),
},
)),
time_offset_seconds: 0,
},
};
let job: Arc<RwLock<Box<dyn Job + Send + Sync + 'static>>> =
Arc::new(RwLock::new(Box::new(job)));
Ok(Self(job))
}
/// Create a new one shot job.
///
/// This will schedule a job that is only run once after the duration has passed.
///
/// ```rust,ignore
/// 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
pub fn new_one_shot<T>(duration: Duration, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) + Send + Sync,
{
JobLocked::make_one_shot_job(duration, Box::new(run), Box::new(nop_async), false)
}
/// Create a new async one shot job.
///
/// This will schedule a job that is only run once after the duration has passed.
///
/// ```rust,ignore
/// 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
pub fn new_one_shot_async<T>(duration: Duration, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync,
{
JobLocked::make_one_shot_job(duration, Box::new(nop), Box::new(run), true)
}
fn make_new_one_shot_at_an_instant(
instant: Instant,
run: Box<JobToRun>,
run_async: Box<JobToRunAsync>,
async_job: bool,
) -> Result<Self, JobSchedulerError> {
let id = Uuid::new_v4();
let job = NonCronJob {
run,
run_async,
async_job,
data: JobStoredData {
id: Some(id.into()),
last_updated: None,
last_tick: None,
next_tick: chrono::Utc::now()
.checked_add_signed(chrono::Duration::seconds(
instant.duration_since(Instant::now()).as_secs() as i64,
))
.map(|t| t.timestamp() as u64)
.unwrap_or(0),
job_type: JobType::OneShot.into(),
count: 0,
extra: vec![],
ran: false,
stopped: false,
#[cfg(feature = "has_bytes")]
job: Some(job_data_prost::job_stored_data::Job::NonCronJob(
job_data_prost::NonCronJob {
repeating: false,
repeated_every: instant.duration_since(Instant::now()).as_secs(),
},
)),
#[cfg(not(feature = "has_bytes"))]
job: Some(job_data::job_stored_data::Job::NonCronJob(
job_data::NonCronJob {
repeating: false,
repeated_every: instant.duration_since(Instant::now()).as_secs(),
},
)),
time_offset_seconds: 0,
},
};
let job: Arc<RwLock<Box<dyn Job + Send + Sync + 'static>>> =
Arc::new(RwLock::new(Box::new(job)));
Ok(Self(job))
}
/// Create a new one shot job that runs at an instant
///
/// ```rust,ignore
/// // 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());
/// ```
pub fn new_one_shot_at_instant<T>(instant: Instant, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) + Send + Sync,
{
JobLocked::make_new_one_shot_at_an_instant(
instant,
Box::new(run),
Box::new(nop_async),
false,
)
}
/// Create a new async one shot job that runs at an instant
///
/// ```rust,ignore
/// // 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());
/// ```
pub fn new_one_shot_at_instant_async<T>(
instant: Instant,
run: T,
) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync,
{
JobLocked::make_new_one_shot_at_an_instant(instant, Box::new(nop), Box::new(run), true)
}
fn make_new_repeated(
duration: Duration,
run: Box<JobToRun>,
run_async: Box<JobToRunAsync>,
async_job: bool,
) -> Result<Self, JobSchedulerError> {
let id = Uuid::new_v4();
let job = NonCronJob {
run,
run_async,
async_job,
data: JobStoredData {
id: Some(id.into()),
last_updated: None,
last_tick: None,
next_tick: chrono::Utc::now()
.checked_add_signed(chrono::Duration::seconds(duration.as_secs() as i64))
.map(|t| t.timestamp() as u64)
.unwrap_or(0),
job_type: JobType::Repeated.into(),
count: 0,
extra: vec![],
ran: false,
stopped: false,
#[cfg(feature = "has_bytes")]
job: Some(job_data_prost::job_stored_data::Job::NonCronJob(
job_data_prost::NonCronJob {
repeating: true,
repeated_every: duration.as_secs(),
},
)),
#[cfg(not(feature = "has_bytes"))]
job: Some(job_data::job_stored_data::Job::NonCronJob(
job_data::NonCronJob {
repeating: true,
repeated_every: duration.as_secs(),
},
)),
time_offset_seconds: 0,
},
};
let job: Arc<RwLock<Box<dyn Job + Send + Sync + 'static>>> =
Arc::new(RwLock::new(Box::new(job)));
Ok(Self(job))
}
/// Create a new repeated job.
///
/// This is checked if it is running only after 500ms in 500ms intervals.
/// ```rust,ignore
/// 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());
/// ```
pub fn new_repeated<T>(duration: Duration, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) + Send + Sync,
{
JobLocked::make_new_repeated(duration, Box::new(run), Box::new(nop_async), false)
}
/// Create a new async repeated job.
///
/// This is checked if it is running only after 500ms in 500ms intervals.
/// ```rust,ignore
/// 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());
/// ```
pub fn new_repeated_async<T>(duration: Duration, run: T) -> Result<Self, JobSchedulerError>
where
T: 'static,
T: FnMut(Uuid, JobsSchedulerLocked) -> Pin<Box<dyn Future<Output = ()> + Send>>
+ Send
+ Sync,
{
JobLocked::make_new_repeated(duration, Box::new(nop), Box::new(run), true)
}
///
/// 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
pub fn tick(&mut self) -> Result<bool, JobSchedulerError> {
let now = Utc::now();
let (job_type, last_tick, next_tick, schedule, repeated_every, ran, count) = {
let r = self.0.read().map_err(|_| JobSchedulerError::TickError)?;
(
r.job_type(),
r.last_tick(),
r.next_tick(),
r.schedule(),
r.repeated_every(),
r.ran(),
r.count(),
)
};
// Don't bother processing a cancelled job
if next_tick.is_none() {
return Err(JobSchedulerError::NoNextTick);
}
let must_run = match (last_tick.as_ref(), next_tick.as_ref(), job_type) {
(None, Some(next_tick), JobType::OneShot) => {
let now_to_next = now.cmp(next_tick);
matches!(now_to_next, std::cmp::Ordering::Greater)
|| matches!(now_to_next, std::cmp::Ordering::Equal)
}
(None, Some(next_tick), JobType::Repeated) => {
let now_to_next = now.cmp(next_tick);
matches!(now_to_next, std::cmp::Ordering::Greater)
|| matches!(now_to_next, std::cmp::Ordering::Equal)
}
(Some(last_tick), Some(next_tick), _) => {
let now_to_next = now.cmp(next_tick);
let last_to_next = last_tick.cmp(next_tick);
(matches!(now_to_next, std::cmp::Ordering::Greater)
|| matches!(now_to_next, std::cmp::Ordering::Equal))
&& (matches!(last_to_next, std::cmp::Ordering::Less)
|| matches!(last_to_next, std::cmp::Ordering::Equal))
}
_ => false,
};
let next_tick = if must_run {
match job_type {
JobType::Cron => schedule.and_then(|s| s.iter_after(now).next()),
JobType::OneShot => None,
JobType::Repeated => repeated_every.and_then(|r| {
next_tick
.and_then(|nt| nt.checked_add_signed(chrono::Duration::seconds(r as i64)))
}),
}
} else {
next_tick
};
let last_tick = Some(now);
let job_data = self.job_data();
if let Err(e) = job_data {
error!("Could not get job data");
return Err(e);
}
{
let mut w = self.0.write().map_err(|_| JobSchedulerError::JobTick)?;
w.set_next_tick(next_tick);
w.set_last_tick(last_tick);
w.set_ran(ran || must_run);
let count = if must_run {
if count == u32::MAX {
0
} else {
count + 1
}
} else {
count
};
w.set_count(count);
}
Ok(must_run)
}
///
/// Get the GUID for the job
///
pub fn guid(&self) -> Uuid {
let r = self.0.read().unwrap();
r.job_id()
}
///
/// Add a notification to run on a list of state notifications
pub async fn on_notifications_add(
&self,
job_scheduler: &JobsSchedulerLocked,
run: Box<OnJobNotification>,
states: Vec<JobState>,
) -> Result<Uuid, JobSchedulerError> {
if !job_scheduler.inited().await {
let mut job_scheduler = job_scheduler.clone();
job_scheduler.init().await?;
}
let job_id = self.guid();
let context = job_scheduler.context();
NotificationCreator::add(&context, run, states, &job_id).await
}
///
/// 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`.
pub async fn on_start_notification_add(
&self,
job_scheduler: &JobsSchedulerLocked,
on_start: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError> {
self.on_notifications_add(job_scheduler, on_start, vec![JobState::Started])
.await
}
///
/// Remove a notification optionally for a certain type of states
pub async fn on_notification_removal(
&self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
states: Option<Vec<JobState>>,
) -> Result<(NotificationId, bool), JobSchedulerError> {
if !job_scheduler.inited().await {
let mut job_scheduler = job_scheduler.clone();
job_scheduler.init().await?;
}
let context = job_scheduler.context();
NotificationDeleter::remove(&context, notification_id, states)
}
///
/// Remove the notification when the task was started. Uses the same UUID that was returned by
/// `on_start_notification_add`
pub async fn on_start_notification_remove(
&self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError> {
self.on_notification_removal(
job_scheduler,
notification_id,
Some(vec![JobState::Started]),
)
.await
.map(|(_, deleted)| deleted)
}
///
/// 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`.
pub async fn on_done_notification_add(
&mut self,
job_scheduler: &JobsSchedulerLocked,
on_stop: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError> {
self.on_notifications_add(job_scheduler, on_stop, vec![JobState::Done])
.await
}
///
/// Remove the notification when the task was stopped. Uses the same UUID that was returned by
/// `on_done_notification_add`
pub async fn on_done_notification_remove(
&mut self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError> {
self.on_notification_removal(job_scheduler, notification_id, Some(vec![JobState::Done]))
.await
.map(|(_, deleted)| deleted)
}
///
/// 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`.
pub async fn on_removed_notification_add(
&mut self,
job_scheduler: &JobsSchedulerLocked,
on_removed: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError> {
self.on_notifications_add(job_scheduler, on_removed, vec![JobState::Removed])
.await
}
///
/// Remove the notification when the task was removed. Uses the same UUID that was returned by
/// `on_removed_notification_add`
pub async fn on_removed_notification_remove(
&mut self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError> {
self.on_notification_removal(
job_scheduler,
notification_id,
Some(vec![JobState::Removed]),
)
.await
.map(|(_, deleted)| deleted)
}
///
/// 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`.
pub async fn on_stop_notification_add(
&mut self,
job_scheduler: &JobsSchedulerLocked,
on_removed: Box<OnJobNotification>,
) -> Result<Uuid, JobSchedulerError> {
self.on_notifications_add(job_scheduler, on_removed, vec![JobState::Stop])
.await
}
///
/// Remove the notification when the task was removed. Uses the same UUID that was returned by
/// `on_removed_notification_add`
pub async fn on_stop_notification_remove(
&mut self,
job_scheduler: &JobsSchedulerLocked,
notification_id: &Uuid,
) -> Result<bool, JobSchedulerError> {
self.on_notification_removal(job_scheduler, notification_id, Some(vec![JobState::Stop]))
.await
.map(|(_, deleted)| deleted)
}
///
/// Override the job's data for use in data storage
pub fn set_job_data(&mut self, job_data: JobStoredData) -> Result<(), JobSchedulerError> {
let mut w = self
.0
.write()
.map_err(|_| JobSchedulerError::UpdateJobData)?;
w.set_job_data(job_data)
}
///
/// Set whether this job has been stopped
pub fn set_stop(&mut self, stop: bool) -> Result<(), JobSchedulerError> {
let mut w = self
.0
.write()
.map_err(|_| JobSchedulerError::UpdateJobData)?;
if stop {
w.set_stopped();
} else {
w.set_started();
}
Ok(())
}
///
/// Get the job data
pub fn job_data(&mut self) -> Result<JobStoredData, JobSchedulerError> {
let mut w = self.0.write().map_err(|_| JobSchedulerError::GetJobData)?;
match w.job_data_from_job() {
Ok(Some(job_data)) => Ok(job_data),
_ => Err(JobSchedulerError::GetJobData),
}
}
#[cfg(not(feature = "english"))]
pub fn schedule_to_cron<T: ToString>(schedule: T) -> Result<String, JobSchedulerError> {
Ok(schedule.to_string())
}
#[cfg(feature = "english")]
pub fn schedule_to_cron<T: ToString>(schedule: T) -> Result<String, JobSchedulerError> {
let schedule = schedule.to_string();
match Cron::new(&schedule)
.with_seconds_required()
.with_dom_and_dow()
.parse()
{
Ok(_) => Ok(schedule),
Err(_) => match english_to_cron::str_cron_syntax(&schedule) {
Ok(english_to_cron) => {
if english_to_cron != schedule {
if english_to_cron == "0 * * * * ? *" {
Err(ParseSchedule)
} else {
// english-to-cron adds the year field which we can't put off (currently)
let cron = english_to_cron
.split(' ')
.take(6)
.collect::<Vec<_>>()
.join(" ");
Ok(cron)
}
} else {
Ok(schedule)
}
}
Err(_) => Err(ParseSchedule),
},
}
}
}