Skip scheduled tasks whose cron expression is invalid

rq only parses the cron when the scheduler loads it, then logs and drops
the job while cron_config had already logged it as scheduled. Validate it
up front and log an error instead. croniter was a transitive rq dependency
and is now declared directly, with its typeshed stubs for mypy.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-30 16:32:49 -04:00
parent ef99d147fc
commit 640e8eb96b
4 changed files with 28 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ A task is registered only when it is enabled and has a cron string, so turning
one off is a restart rather than an unschedule.
"""
from croniter import croniter
from rq import cron
from config import TASK_TIMEOUT
@@ -15,6 +16,11 @@ for name, task in SCHEDULED_TASKS.items():
if not task.enabled or not task.cron_string:
continue
# rq parses the cron only when the scheduler loads it, then just logs and skips it.
if not croniter.is_valid(task.cron_string):
log.error(f"Not scheduling '{name}': invalid cron '{task.cron_string}'")
continue
# Cron attaches no failure callback, so a scan goes through a dispatch job
# that can. It only enqueues, so it takes the ordinary task timeout.
is_scan = task.task_type is TaskType.SCAN

View File

@@ -116,6 +116,13 @@ class TestCronConfig:
def test_skips_a_task_with_no_cron_string(self, mocker, registered):
assert registered({"no_cron": _task(mocker, cron_string="")}).call_count == 0
def test_skips_a_task_with_an_invalid_cron_string(self, mocker, registered):
register = registered(
{"bad": _task(mocker, cron_string="hourly"), "good": _task(mocker)}
)
assert [call.kwargs["name"] for call in register.call_args_list] == ["good"]
def test_registers_the_real_schedule(self, mocker):
# Guards the actual catalog: every enabled task with a cron string is
# registered, because nothing else schedules them any more.

View File

@@ -22,6 +22,7 @@ dependencies = [
"authlib ~= 1.8.0",
"certifi >= 2024.7.4",
"colorama ~= 0.4",
"croniter ~= 6.2",
"cryptography>=49,<51",
"defusedxml ~= 0.7",
"fastapi[standard-no-fastapi-cloud-cli] ~= 0.141.1",
@@ -70,6 +71,7 @@ dev = [
"pyinstrument ~= 5.0",
"types-authlib ~= 1.8.0",
"types-colorama ~= 0.4",
"types-croniter ~= 6.2",
"types-defusedxml ~= 0.7",
"types-passlib ~= 1.7",
"types-pyyaml ~= 6.0",

13
uv.lock generated
View File

@@ -2378,6 +2378,7 @@ dependencies = [
{ name = "bcrypt" },
{ name = "certifi" },
{ name = "colorama" },
{ name = "croniter" },
{ name = "cryptography" },
{ name = "defusedxml" },
{ name = "fastapi", extra = ["standard-no-fastapi-cloud-cli"] },
@@ -2427,6 +2428,7 @@ dev = [
{ name = "pyinstrument" },
{ name = "types-authlib" },
{ name = "types-colorama" },
{ name = "types-croniter" },
{ name = "types-defusedxml" },
{ name = "types-passlib" },
{ name = "types-pyyaml" },
@@ -2454,6 +2456,7 @@ requires-dist = [
{ name = "bcrypt", specifier = "<5" },
{ name = "certifi", specifier = ">=2024.7.4" },
{ name = "colorama", specifier = "~=0.4" },
{ name = "croniter", specifier = "~=6.2" },
{ name = "cryptography", specifier = ">=49,<51" },
{ name = "defusedxml", specifier = "~=0.7" },
{ name = "fakeredis", marker = "extra == 'test'", specifier = "~=2.38" },
@@ -2501,6 +2504,7 @@ requires-dist = [
{ name = "strsimpy", specifier = "~=0.2" },
{ name = "types-authlib", marker = "extra == 'dev'", specifier = "~=1.8.0" },
{ name = "types-colorama", marker = "extra == 'dev'", specifier = "~=0.4" },
{ name = "types-croniter", marker = "extra == 'dev'", specifier = "~=6.2" },
{ name = "types-defusedxml", marker = "extra == 'dev'", specifier = "~=0.7" },
{ name = "types-passlib", marker = "extra == 'dev'", specifier = "~=1.7" },
{ name = "types-pyyaml", marker = "extra == 'dev'", specifier = "~=6.0" },
@@ -2755,6 +2759,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/b7/83/6944b4fa01efb2e63ac62b791a8ddf0fee358f93be9f64b8f152648ad9d3/types_colorama-0.4.15.20240311-py3-none-any.whl", hash = "sha256:6391de60ddc0db3f147e31ecb230006a6823e81e380862ffca1e4695c13a0b8e", size = 5840, upload-time = "2024-03-11T02:15:50.43Z" },
]
[[package]]
name = "types-croniter"
version = "6.2.4.20260711"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/c0/e1/ef8900515a103efa41b29ce81e59efb18e72bc9642139c5a3a17f6022641/types_croniter-6.2.4.20260711.tar.gz", hash = "sha256:807793e109f4e0d51ec18f6277a1a5f1d133e425ac6bee3a7c3254ab1793e0ea", size = 12164, upload-time = "2026-07-11T04:51:18.624Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/c8/3f/896a98180a55f855bd068b97e8c99fd4ce1885a4b43f68fd7b29641084f6/types_croniter-6.2.4.20260711-py3-none-any.whl", hash = "sha256:601b68139a47fe1b2177f433132cc81fd91155f3059a49c5cb63404c389dff01", size = 9746, upload-time = "2026-07-11T04:51:17.662Z" },
]
[[package]]
name = "types-defusedxml"
version = "0.7.0.20260504"