2022-09-03 11:05:58 +00:00
|
|
|
#![doc = include_str!("../README.md")]
|
2022-08-18 11:39:10 +00:00
|
|
|
|
|
|
|
use std::time::Duration;
|
2022-08-29 16:59:22 +00:00
|
|
|
use thiserror::Error;
|
2022-10-18 06:30:17 +00:00
|
|
|
use typed_builder::TypedBuilder;
|
2022-08-29 16:59:22 +00:00
|
|
|
|
2022-10-18 06:30:17 +00:00
|
|
|
/// Represents a schedule for scheduled tasks.
|
|
|
|
///
|
|
|
|
/// It's used in the [`AsyncRunnable::cron`] and [`Runnable::cron`]
|
|
|
|
#[derive(Debug, Clone)]
|
2022-08-29 16:59:22 +00:00
|
|
|
pub enum Scheduled {
|
2022-10-18 06:30:17 +00:00
|
|
|
/// A cron pattern for a periodic task
|
|
|
|
///
|
|
|
|
/// For example, `Scheduled::CronPattern("0/20 * * * * * *")`
|
2022-08-29 16:59:22 +00:00
|
|
|
CronPattern(String),
|
2022-10-18 06:30:17 +00:00
|
|
|
/// A datetime for a scheduled task that will be executed once
|
|
|
|
///
|
|
|
|
/// For example, `Scheduled::ScheduleOnce(chrono::Utc::now() + std::time::Duration::seconds(7i64))`
|
2022-08-29 16:59:22 +00:00
|
|
|
ScheduleOnce(DateTime<Utc>),
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:30:17 +00:00
|
|
|
/// List of error types that can occur while working with cron schedules.
|
2022-08-29 16:59:22 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum CronError {
|
2022-10-18 06:30:17 +00:00
|
|
|
/// A problem occured during cron schedule parsing.
|
2022-08-29 16:59:22 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
LibraryError(#[from] cron::error::Error),
|
2022-10-18 06:30:17 +00:00
|
|
|
/// [`Scheduled`] enum variant is not provided
|
2022-08-29 16:59:22 +00:00
|
|
|
#[error("You have to implement method `cron()` in your AsyncRunnable")]
|
|
|
|
TaskNotSchedulableError,
|
2022-10-18 06:30:17 +00:00
|
|
|
/// The next execution can not be determined using the current [`Scheduled::CronPattern`]
|
2022-08-29 16:59:22 +00:00
|
|
|
#[error("No timestamps match with this cron pattern")]
|
|
|
|
NoTimestampsError,
|
|
|
|
}
|
2022-08-18 11:39:10 +00:00
|
|
|
|
2022-10-18 06:30:17 +00:00
|
|
|
/// All possible options for retaining tasks in the db after their execution.
|
|
|
|
///
|
|
|
|
/// The default mode is [`RetentionMode::RemoveAll`]
|
2022-07-23 14:24:22 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum RetentionMode {
|
2022-10-18 06:30:17 +00:00
|
|
|
/// Keep all tasks
|
2022-07-23 14:24:22 +00:00
|
|
|
KeepAll,
|
2022-10-18 06:30:17 +00:00
|
|
|
/// Remove all tasks
|
2022-07-23 14:24:22 +00:00
|
|
|
RemoveAll,
|
2022-10-18 06:30:17 +00:00
|
|
|
/// Remove only successfully finished tasks
|
2022-07-23 14:24:22 +00:00
|
|
|
RemoveFinished,
|
|
|
|
}
|
2022-10-18 06:30:17 +00:00
|
|
|
|
2022-07-27 17:05:05 +00:00
|
|
|
impl Default for RetentionMode {
|
|
|
|
fn default() -> Self {
|
|
|
|
RetentionMode::RemoveAll
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 13:05:27 +00:00
|
|
|
|
2022-10-18 06:30:17 +00:00
|
|
|
/// Configuration parameters for putting workers to sleep
|
|
|
|
/// while they don't have any tasks to execute
|
|
|
|
#[derive(Clone, Debug, TypedBuilder)]
|
2022-07-23 14:24:22 +00:00
|
|
|
pub struct SleepParams {
|
2022-10-18 06:30:17 +00:00
|
|
|
/// the current sleep period
|
2022-08-18 11:39:10 +00:00
|
|
|
pub sleep_period: Duration,
|
2022-10-18 06:30:17 +00:00
|
|
|
/// the maximum period a worker is allowed to sleep.
|
|
|
|
/// After this value is reached, `sleep_period` is not increased anymore
|
2022-08-18 11:39:10 +00:00
|
|
|
pub max_sleep_period: Duration,
|
2022-10-18 06:30:17 +00:00
|
|
|
/// the initial value of the `sleep_period`
|
2022-08-18 11:39:10 +00:00
|
|
|
pub min_sleep_period: Duration,
|
2022-10-18 06:30:17 +00:00
|
|
|
/// the step that `sleep_period` is increased by on every iteration
|
2022-08-18 11:39:10 +00:00
|
|
|
pub sleep_step: Duration,
|
2022-07-23 14:24:22 +00:00
|
|
|
}
|
2022-08-18 11:39:10 +00:00
|
|
|
|
2022-07-23 14:24:22 +00:00
|
|
|
impl SleepParams {
|
2022-10-18 06:30:17 +00:00
|
|
|
/// Reset the `sleep_period` if `sleep_period` > `min_sleep_period`
|
2022-07-23 14:24:22 +00:00
|
|
|
pub fn maybe_reset_sleep_period(&mut self) {
|
|
|
|
if self.sleep_period != self.min_sleep_period {
|
|
|
|
self.sleep_period = self.min_sleep_period;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:30:17 +00:00
|
|
|
/// Increase the `sleep_period` by the `sleep_step` if the `max_sleep_period` is not reached
|
2022-07-23 14:24:22 +00:00
|
|
|
pub fn maybe_increase_sleep_period(&mut self) {
|
|
|
|
if self.sleep_period < self.max_sleep_period {
|
|
|
|
self.sleep_period += self.sleep_step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 11:39:10 +00:00
|
|
|
|
2022-07-23 14:24:22 +00:00
|
|
|
impl Default for SleepParams {
|
|
|
|
fn default() -> Self {
|
|
|
|
SleepParams {
|
2022-08-18 11:39:10 +00:00
|
|
|
sleep_period: Duration::from_secs(5),
|
|
|
|
max_sleep_period: Duration::from_secs(15),
|
|
|
|
min_sleep_period: Duration::from_secs(5),
|
|
|
|
sleep_step: Duration::from_secs(5),
|
2022-07-23 14:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 10:17:02 +00:00
|
|
|
|
2022-10-18 06:30:17 +00:00
|
|
|
/// An error that can happen during executing of tasks
|
2022-08-31 17:45:13 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FangError {
|
2022-10-18 06:30:17 +00:00
|
|
|
/// A description of an error
|
2022-08-31 17:45:13 +00:00
|
|
|
pub description: String,
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:30:17 +00:00
|
|
|
#[doc(hidden)]
|
2022-07-20 16:08:02 +00:00
|
|
|
#[cfg(feature = "blocking")]
|
2022-07-17 06:10:32 +00:00
|
|
|
extern crate diesel;
|
2021-07-11 07:26:20 +00:00
|
|
|
|
2021-07-25 12:20:16 +00:00
|
|
|
#[doc(hidden)]
|
2022-07-20 16:08:02 +00:00
|
|
|
#[cfg(feature = "blocking")]
|
2021-07-25 12:20:16 +00:00
|
|
|
pub use diesel::pg::PgConnection;
|
2022-07-20 16:08:02 +00:00
|
|
|
|
2021-07-11 07:26:20 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub use typetag;
|
2022-07-17 06:10:32 +00:00
|
|
|
|
2022-08-03 08:37:53 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub extern crate serde;
|
|
|
|
|
2022-08-27 15:58:38 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub extern crate chrono;
|
|
|
|
|
2022-08-03 08:37:53 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub use serde_derive::{Deserialize, Serialize};
|
|
|
|
|
2022-09-03 11:05:58 +00:00
|
|
|
#[doc(hidden)]
|
2022-08-27 15:58:38 +00:00
|
|
|
pub use chrono::DateTime;
|
2022-09-03 11:05:58 +00:00
|
|
|
#[doc(hidden)]
|
2022-08-27 15:58:38 +00:00
|
|
|
pub use chrono::Utc;
|
|
|
|
|
2022-07-20 16:08:02 +00:00
|
|
|
#[cfg(feature = "blocking")]
|
|
|
|
pub mod blocking;
|
2022-10-18 06:30:17 +00:00
|
|
|
|
2022-07-20 16:08:02 +00:00
|
|
|
#[cfg(feature = "blocking")]
|
|
|
|
pub use blocking::*;
|
2022-07-17 07:13:45 +00:00
|
|
|
|
2022-07-20 16:08:02 +00:00
|
|
|
#[cfg(feature = "asynk")]
|
2022-07-17 07:13:45 +00:00
|
|
|
pub mod asynk;
|
2022-07-31 13:32:37 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "asynk")]
|
|
|
|
pub use asynk::*;
|
2022-08-03 08:37:53 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "asynk")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub use bb8_postgres::tokio_postgres::tls::NoTls;
|
|
|
|
|
|
|
|
#[cfg(feature = "asynk")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub use async_trait::async_trait;
|