Fix weekly scheduling crash: schedule library uses full weekday names, not 3-letter abbreviations
This commit is contained in:
@@ -175,13 +175,16 @@ def schedule_job():
|
||||
schedule.every().day.at(f"{hour:02d}:{minute:02d}").do(update_blocklist)
|
||||
logging.info(f"Scheduled daily update at {hour:02d}:{minute:02d}")
|
||||
elif BLOCKLIST_CRON_TYPE == "weekly":
|
||||
valid_days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
|
||||
day_names = {
|
||||
"mon": "monday", "tue": "tuesday", "wed": "wednesday", "thu": "thursday",
|
||||
"fri": "friday", "sat": "saturday", "sun": "sunday",
|
||||
}
|
||||
day = BLOCKLIST_CRON_DAY[:3]
|
||||
if day not in valid_days:
|
||||
logging.error(f"Invalid BLOCKLIST_CRON_DAY '{BLOCKLIST_CRON_DAY}', must be one of {valid_days}. Defaulting to Monday.")
|
||||
if day not in day_names:
|
||||
logging.error(f"Invalid BLOCKLIST_CRON_DAY '{BLOCKLIST_CRON_DAY}', must be one of {list(day_names)}. Defaulting to Monday.")
|
||||
day = "mon"
|
||||
getattr(schedule.every(), day).at(f"{hour:02d}:{minute:02d}").do(update_blocklist)
|
||||
logging.info(f"Scheduled weekly update on {day.capitalize()} at {hour:02d}:{minute:02d}")
|
||||
getattr(schedule.every(), day_names[day]).at(f"{hour:02d}:{minute:02d}").do(update_blocklist)
|
||||
logging.info(f"Scheduled weekly update on {day_names[day].capitalize()} at {hour:02d}:{minute:02d}")
|
||||
else:
|
||||
logging.error(f"Invalid BLOCKLIST_CRON_TYPE '{BLOCKLIST_CRON_TYPE}', must be 'daily' or 'weekly'. Defaulting to daily.")
|
||||
schedule.every().day.at(f"{hour:02d}:{minute:02d}").do(update_blocklist)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import pytest
|
||||
import schedule as schedule_lib
|
||||
|
||||
import blocklist_scheduler as bs
|
||||
|
||||
@@ -39,3 +40,63 @@ def test_backup_first_start_raises_if_adguard_yaml_missing(tmp_path, monkeypatch
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
bs.backup_first_start()
|
||||
|
||||
|
||||
# --- schedule_job (schedule) ---
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clear_schedule():
|
||||
yield
|
||||
schedule_lib.clear()
|
||||
|
||||
|
||||
def test_schedule_job_daily(monkeypatch):
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TYPE", "daily")
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TIME", "06:00")
|
||||
|
||||
bs.schedule_job()
|
||||
|
||||
assert len(schedule_lib.jobs) == 1
|
||||
job = schedule_lib.jobs[0]
|
||||
assert job.unit == "days"
|
||||
assert str(job.at_time) == "06:00:00"
|
||||
assert job.job_func.func is bs.update_blocklist
|
||||
|
||||
|
||||
def test_schedule_job_weekly_valid_day(monkeypatch):
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TYPE", "weekly")
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TIME", "18:30")
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_DAY", "wed")
|
||||
|
||||
bs.schedule_job()
|
||||
|
||||
job = schedule_lib.jobs[0]
|
||||
assert job.unit == "weeks"
|
||||
assert job.start_day == "wednesday"
|
||||
assert str(job.at_time) == "18:30:00"
|
||||
|
||||
|
||||
def test_schedule_job_weekly_invalid_day_defaults_to_monday(monkeypatch):
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TYPE", "weekly")
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_DAY", "xxx")
|
||||
|
||||
bs.schedule_job()
|
||||
|
||||
assert schedule_lib.jobs[0].start_day == "monday"
|
||||
|
||||
|
||||
def test_schedule_job_invalid_time_defaults_to_six_am(monkeypatch):
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TYPE", "daily")
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TIME", "not-a-time")
|
||||
|
||||
bs.schedule_job()
|
||||
|
||||
assert str(schedule_lib.jobs[0].at_time) == "06:00:00"
|
||||
|
||||
|
||||
def test_schedule_job_invalid_type_defaults_to_daily(monkeypatch):
|
||||
monkeypatch.setattr(bs, "BLOCKLIST_CRON_TYPE", "bogus")
|
||||
|
||||
bs.schedule_job()
|
||||
|
||||
assert schedule_lib.jobs[0].unit == "days"
|
||||
|
||||
Reference in New Issue
Block a user