Files
romm/backend/tasks/cron_config.py
Georges-Antoine Assi 640e8eb96b 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>
2026-09-30 16:32:49 -04:00

43 lines
1.6 KiB
Python

"""The schedule `rq cron` runs, registered when that process starts.
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
from logger.logger import log
from tasks.registry import SCHEDULED_TASKS, enqueue_scheduled_scan
from tasks.tasks import TaskType, run_task_by_name
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
# Every entry runs the same function, so without an explicit name they all
# share one cron identity and one job history.
cron.register(
enqueue_scheduled_scan if is_scan else run_task_by_name,
task.queue_name,
name=name,
kwargs={"name": name},
cron=task.cron_string,
job_timeout=TASK_TIMEOUT if is_scan else task.timeout,
# A spent dispatch carries the scan's own name, and RQ drops a job whose
# result_ttl is 0, so one rescan is not listed as two runs.
result_ttl=0 if is_scan else task.result_ttl,
meta=task.job_meta(name),
)
log.info(f"Scheduled '{name}' at '{task.cron_string}'")