Repository navigation
Count a docket-rs Cron run's next match from its start, and parse as pydocket does #548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| { | ||
| "after": "2026-10-05T07:30:00Z", | ||
| "expressions": [ | ||
| { | ||
| "expression": "0 9 * * 1-5", | ||
| "next": "2026-10-05T09:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "*/15 * * * *", | ||
| "next": "2026-10-05T07:45:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 1 * *", | ||
| "next": "2026-11-01T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 0", | ||
| "next": "2026-10-11T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 7", | ||
| "next": "2026-10-11T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 0-7", | ||
| "next": "2026-10-06T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * MON", | ||
| "next": "2026-10-12T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * mon", | ||
| "next": "2026-10-12T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * MON-FRI", | ||
| "next": "2026-10-06T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 1,3,5", | ||
| "next": "2026-10-07T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 1 JAN *", | ||
| "next": "2027-01-01T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * JAN-MAR *", | ||
| "next": "2027-01-01T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 9-17/2 * * *", | ||
| "next": "2026-10-05T09:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 1-31/10 * *", | ||
| "next": "2026-10-11T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 1,15 * 1", | ||
| "next": "2026-10-12T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 1 * MON", | ||
| "next": "2026-10-12T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 29 2 *", | ||
| "next": "2028-02-29T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 L * *", | ||
| "next": "2026-10-31T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 LW * *", | ||
| "next": "2026-10-30T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 5L", | ||
| "next": "2026-10-30T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 6L", | ||
| "next": "2026-10-31T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 1#2", | ||
| "next": "2026-10-12T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 12 * * SUN#1", | ||
| "next": "2026-11-01T12:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 5#5", | ||
| "next": "2026-10-30T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "15 0 * * * *", | ||
| "next": "2026-10-05T08:00:15Z" | ||
| }, | ||
| { | ||
| "expression": "0 15 * * * *", | ||
| "next": "2026-10-05T08:15:00Z" | ||
| }, | ||
| { | ||
| "expression": "* * * * * *", | ||
| "next": "2026-10-05T07:30:01Z" | ||
| }, | ||
| { | ||
| "expression": "@yearly", | ||
| "next": "2027-01-01T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "@annually", | ||
| "next": "2027-01-01T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "@monthly", | ||
| "next": "2026-11-01T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "@weekly", | ||
| "next": "2026-10-11T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "@daily", | ||
| "next": "2026-10-06T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "@midnight", | ||
| "next": "2026-10-06T00:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "@hourly", | ||
| "next": "2026-10-05T08:00:00Z" | ||
| }, | ||
| { | ||
| "expression": "0 0 15W * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 1W * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 ? * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 31 2 *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 30 2 *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 31 4 *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 * * L", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 0 1 1 * 2027", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 L-3 * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 7-1", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 5-2", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 0 * * 8", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "0 24 * * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "60 * * * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "*/0 * * * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "* * *", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "@reboot", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "@every 5m", | ||
| "next": null | ||
| }, | ||
| { | ||
| "expression": "every tuesday", | ||
| "next": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,14 @@ use crate::task::Task; | |
| /// adds it when it starts, so its arguments need a `Default`. | ||
| /// [`Cron::manual`] runs it only after something adds it. | ||
| /// | ||
| /// Expressions have five fields, or one of `@yearly`, `@annually`, | ||
| /// `@monthly`, `@weekly`, `@daily`, `@midnight`, and `@hourly`. | ||
| /// Expressions have five fields, or six with seconds first, or are one of | ||
| /// `@yearly`, `@annually`, `@monthly`, `@weekly`, `@daily`, `@midnight`, and | ||
| /// `@hourly`. They mean what they mean in pydocket, which refuses a field | ||
| /// for the year, `?`, a `W` other than `LW`, and an expression that never | ||
| /// matches, so this does too. | ||
| /// | ||
| /// Each run's next match counts from when the run started, so a run still | ||
| /// going at its next match is followed by that match's run at once. | ||
| #[derive(Clone, Debug)] | ||
| pub struct Cron<Mode = Automatic> { | ||
| schedule: croner::Cron, | ||
|
|
@@ -37,6 +43,11 @@ impl Cron { | |
| let schedule = croner::Cron::from_str(expression).map_err(|error| { | ||
| Error::Invalid(format!("{expression} is not a cron expression: {error}")) | ||
| })?; | ||
| if let Some(reason) = refused_by_pydocket(expression, &schedule) { | ||
| return Err(Error::Invalid(format!( | ||
| "{expression} is not a cron expression: {reason}" | ||
| ))); | ||
| } | ||
| Ok(Self { | ||
| schedule, | ||
| timezone: Tz::UTC, | ||
|
|
@@ -55,6 +66,25 @@ impl Cron { | |
| } | ||
| } | ||
|
|
||
| /// Why pydocket's parser refuses an expression that croner accepts, if it | ||
| /// does. The day of the month is the third field from the end, both with | ||
| /// and without a field for the seconds. | ||
| fn refused_by_pydocket(expression: &str, schedule: &croner::Cron) -> Option<&'static str> { | ||
| let fields: Vec<&str> = expression.split_whitespace().collect(); | ||
| let day_of_month = fields.len().checked_sub(3).map(|index| fields[index]); | ||
| if fields.len() == 7 { | ||
| Some("it has a field for the year") | ||
| } else if expression.contains('?') { | ||
| Some("it uses ?") | ||
| } else if day_of_month.is_some_and(|day| day.contains('W') && day != "LW") { | ||
| Some("its day of the month uses W, other than LW") | ||
|
Comment on lines
+79
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For lowercase special syntax such as Useful? React with 👍 / 👎. |
||
| } else if schedule.find_next_occurrence(&Utc::now(), false).is_err() { | ||
| Some("it never matches") | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| impl<Mode> Cron<Mode> { | ||
| /// Reads the schedule in `timezone`, so that it follows daylight saving | ||
| /// time there. | ||
|
|
@@ -106,10 +136,9 @@ impl Completion for Schedule { | |
| let control = ctx | ||
| .perpetual() | ||
| .expect("a cron task gives each run its control"); | ||
| match self.cron.next_after(Utc::now()) { | ||
| Some(next) => control.decide(next), | ||
| None => AfterCompletion::Finish, | ||
| } | ||
| self.cron | ||
| .next_after(control.started_at) | ||
| .map_or(AfterCompletion::Finish, |next| control.decide(next)) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The shared 53-expression acceptance/refusal table is exhaustive parser compatibility and error-condition coverage rather than a newcomer-facing happy path. Keeping these parametrizations in
tests/fundamentalsmakes the living-documentation suite depend on a large external fixture and directly conflicts with its prescribed scope; move them to a regular cron test module while retaining the concise overlap example here.AGENTS.md reference: python/tests/fundamentals/AGENTS.md:L144-L147
Useful? React with 👍 / 👎.