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
use graphile_worker_crontab_parser::CrontabParseError;
use graphile_worker_crontab_types::Crontab;
use super::{CronInput, WorkerOptions};
impl WorkerOptions {
/// Adds cron entries for scheduled jobs.
///
/// This accepts typed cron builders, raw [`Crontab`] values, and crontab
/// text. Typed inputs return `WorkerOptions` directly; text input returns
/// `Result<WorkerOptions, CrontabParseError>`.
///
/// # Typed example
/// ```
/// # use graphile_worker::{Cron, CrontabFill, WorkerOptions};
/// # use graphile_worker::{IntoTaskHandlerResult, TaskHandler, WorkerContext};
/// # use serde::{Deserialize, Serialize};
/// #
/// # #[derive(Deserialize, Serialize)]
/// # struct SendDigest;
/// #
/// # impl TaskHandler for SendDigest {
/// # const IDENTIFIER: &'static str = "send_digest";
/// # async fn run(self, _ctx: WorkerContext) -> impl IntoTaskHandlerResult {}
/// # }
///
/// let options = WorkerOptions::default()
/// .define_job::<SendDigest>()
/// .with_cron(
/// Cron::daily_at::<SendDigest>(8, 0)
/// .expect("valid cron schedule")
/// .fill(CrontabFill::hours(1)),
/// );
/// ```
///
/// # Crontab text example
/// ```
/// # use graphile_worker::WorkerOptions;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let options = WorkerOptions::default()
/// .with_cron("0 8 * * * send_digest")?;
/// # let _ = options;
/// # Ok(())
/// # }
/// ```
pub fn with_cron<C: CronInput>(self, cron: C) -> C::Output {
cron.append_to(self)
}
/// Adds typed cron entries for scheduled jobs.
pub fn with_crons<I, C>(mut self, crontabs: I) -> Self
where
I: IntoIterator<Item = C>,
C: Into<Crontab>,
{
self.append_crontabs(crontabs.into_iter().map(Into::into).collect());
self
}
/// Adds crontab text entries for scheduled jobs.
///
/// Use [`Self::with_cron`] with a string instead.
///
/// # Arguments
/// * `input` - A string containing crontab entries
///
/// # Returns
/// * `Result<Self, CrontabParseError>` - The modified WorkerOptions instance or a parse error
///
/// # Example
/// ```
/// # use graphile_worker::WorkerOptions;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
///
/// // Run the "send_digest" job at 8:00 AM every day.
/// let options = WorkerOptions::default()
/// .with_cron("0 8 * * * send_digest")?;
/// # Ok(())
/// # }
/// ```
#[deprecated(note = "use WorkerOptions::with_cron(...) instead")]
pub fn with_crontab(self, input: &str) -> Result<Self, CrontabParseError> {
self.with_cron(input)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn with_crons_appends_multiple_crontabs() {
let options = WorkerOptions::default().with_crons([
Crontab::new(Default::default(), "send_digest"),
Crontab::new(Default::default(), "sync_metrics"),
]);
let crontabs = options.crontabs.expect("crontabs should be set");
let identifiers = crontabs
.into_iter()
.map(|crontab| crontab.task_identifier)
.collect::<Vec<_>>();
assert_eq!(identifiers, ["send_digest", "sync_metrics"]);
}
}