Tweakhancement: add jitter to IMAP polling schedule

This commit is contained in:
shamoon
2026-08-19 13:54:15 -07:00
parent a424dace43
commit f331d0b3ba
3 changed files with 18 additions and 2 deletions
+9
View File
@@ -1,6 +1,7 @@
import datetime
import logging
import os
import random
from pathlib import Path
from typing import Any
@@ -172,6 +173,14 @@ def parse_beat_schedule() -> dict:
# Don't add disabled tasks to the schedule
if value == "disable":
continue
if (
task["env_key"] == "PAPERLESS_EMAIL_TASK_CRON"
and task["env_key"] not in os.environ
):
# Spread default polling across the ten-minute interval.
offset = random.randrange(10)
minutes = ",".join(str(minute) for minute in range(offset, 60, 10))
value = f"{minutes} * * * *"
# I find https://crontab.guru/ super helpful
# crontab(5) format
# - five time-and-date fields
@@ -168,6 +168,7 @@ class TestParseHostingSettings:
def make_expected_schedule(
overrides: dict[str, dict[str, Any]] | None = None,
disabled: set[str] | None = None,
email_minute: str = "3,13,23,33,43,53",
) -> dict[str, Any]:
"""
Build the expected schedule with optional overrides and disabled tasks.
@@ -185,7 +186,7 @@ def make_expected_schedule(
schedule: dict[str, Any] = {
"Check all e-mail accounts": {
"task": "paperless_mail.tasks.process_mail_accounts",
"schedule": crontab(minute="*/10"),
"schedule": crontab(minute=email_minute),
"options": {
"expires": mail_expire,
"headers": {"trigger_source": "scheduled"},
@@ -266,6 +267,11 @@ class TestParseBeatSchedule:
("env", "expected"),
[
pytest.param({}, make_expected_schedule(), id="defaults"),
pytest.param(
{"PAPERLESS_EMAIL_TASK_CRON": "*/10 * * * *"},
make_expected_schedule(email_minute="*/10"),
id="email-explicit-default",
),
pytest.param(
{"PAPERLESS_EMAIL_TASK_CRON": "*/50 * * * mon"},
make_expected_schedule(
@@ -305,6 +311,7 @@ class TestParseBeatSchedule:
mocker: MockerFixture,
) -> None:
mocker.patch.dict(os.environ, env, clear=False)
mocker.patch("paperless.settings.custom.random.randrange", return_value=3)
schedule = parse_beat_schedule()
assert schedule == expected