|
| 1 | +"""Tests for Cron dependency (cron-style scheduled tasks).""" |
| 2 | + |
| 3 | +from datetime import datetime, timedelta, timezone |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from croniter import croniter |
| 7 | + |
| 8 | +from docket import Docket, Worker |
| 9 | +from docket.dependencies import Cron |
| 10 | + |
| 11 | + |
| 12 | +async def test_cron_task_reschedules_itself(docket: Docket, worker: Worker): |
| 13 | + """Cron tasks automatically reschedule after each execution.""" |
| 14 | + runs = 0 |
| 15 | + |
| 16 | + async def my_cron_task(cron: Cron = Cron("0 9 * * *", automatic=False)): |
| 17 | + nonlocal runs |
| 18 | + runs += 1 |
| 19 | + |
| 20 | + # Patch croniter.get_next to return a time 10ms in the future |
| 21 | + with patch.object( |
| 22 | + croniter, |
| 23 | + "get_next", |
| 24 | + return_value=datetime.now(timezone.utc) + timedelta(milliseconds=10), |
| 25 | + ): |
| 26 | + execution = await docket.add(my_cron_task)() |
| 27 | + await worker.run_at_most({execution.key: 3}) |
| 28 | + |
| 29 | + assert runs == 3 |
| 30 | + |
| 31 | + |
| 32 | +async def test_cron_tasks_are_automatically_scheduled(docket: Docket, worker: Worker): |
| 33 | + """Cron tasks with automatic=True are scheduled at worker startup.""" |
| 34 | + calls = 0 |
| 35 | + |
| 36 | + async def my_automatic_cron( |
| 37 | + cron: Cron = Cron("0 0 * * *"), |
| 38 | + ): # automatic=True is default |
| 39 | + nonlocal calls |
| 40 | + calls += 1 |
| 41 | + |
| 42 | + docket.register(my_automatic_cron) |
| 43 | + |
| 44 | + with patch.object( |
| 45 | + croniter, |
| 46 | + "get_next", |
| 47 | + return_value=datetime.now(timezone.utc) + timedelta(milliseconds=10), |
| 48 | + ): |
| 49 | + await worker.run_at_most({"my_automatic_cron": 2}) |
| 50 | + |
| 51 | + assert calls == 2 |
| 52 | + |
| 53 | + |
| 54 | +async def test_cron_tasks_continue_after_errors(docket: Docket, worker: Worker): |
| 55 | + """Cron tasks keep rescheduling even when they raise exceptions.""" |
| 56 | + calls = 0 |
| 57 | + |
| 58 | + async def flaky_cron_task(cron: Cron = Cron("0 * * * *", automatic=False)): |
| 59 | + nonlocal calls |
| 60 | + calls += 1 |
| 61 | + raise ValueError("Task failed!") |
| 62 | + |
| 63 | + with patch.object( |
| 64 | + croniter, |
| 65 | + "get_next", |
| 66 | + return_value=datetime.now(timezone.utc) + timedelta(milliseconds=10), |
| 67 | + ): |
| 68 | + execution = await docket.add(flaky_cron_task)() |
| 69 | + await worker.run_at_most({execution.key: 3}) |
| 70 | + |
| 71 | + assert calls == 3 |
| 72 | + |
| 73 | + |
| 74 | +async def test_cron_tasks_can_cancel_themselves(docket: Docket, worker: Worker): |
| 75 | + """A cron task can stop rescheduling by calling cron.cancel().""" |
| 76 | + calls = 0 |
| 77 | + |
| 78 | + async def limited_cron_task(cron: Cron = Cron("0 * * * *", automatic=False)): |
| 79 | + nonlocal calls |
| 80 | + calls += 1 |
| 81 | + if calls >= 3: |
| 82 | + cron.cancel() |
| 83 | + |
| 84 | + with patch.object( |
| 85 | + croniter, |
| 86 | + "get_next", |
| 87 | + return_value=datetime.now(timezone.utc) + timedelta(milliseconds=10), |
| 88 | + ): |
| 89 | + await docket.add(limited_cron_task)() |
| 90 | + await worker.run_until_finished() |
| 91 | + |
| 92 | + assert calls == 3 |
| 93 | + |
| 94 | + |
| 95 | +async def test_cron_supports_vixie_keywords(docket: Docket, worker: Worker): |
| 96 | + """Cron supports Vixie cron keywords like @daily, @weekly, @hourly.""" |
| 97 | + runs = 0 |
| 98 | + |
| 99 | + # @daily is equivalent to "0 0 * * *" (midnight every day) |
| 100 | + async def daily_task(cron: Cron = Cron("@daily", automatic=False)): |
| 101 | + nonlocal runs |
| 102 | + runs += 1 |
| 103 | + |
| 104 | + with patch.object( |
| 105 | + croniter, |
| 106 | + "get_next", |
| 107 | + return_value=datetime.now(timezone.utc) + timedelta(milliseconds=10), |
| 108 | + ): |
| 109 | + execution = await docket.add(daily_task)() |
| 110 | + await worker.run_at_most({execution.key: 1}) |
| 111 | + |
| 112 | + assert runs == 1 |
| 113 | + |
| 114 | + |
| 115 | +async def test_automatic_cron_waits_for_scheduled_time(docket: Docket, worker: Worker): |
| 116 | + """Automatic cron tasks wait for their next scheduled time instead of running immediately. |
| 117 | +
|
| 118 | + Unlike Perpetual tasks which run immediately at worker startup, Cron tasks |
| 119 | + schedule themselves for the next matching cron time. This ensures a Monday 9 AM |
| 120 | + cron doesn't accidentally run on a Wednesday startup. |
| 121 | + """ |
| 122 | + calls: list[datetime] = [] |
| 123 | + |
| 124 | + async def scheduled_task(cron: Cron = Cron("0 9 * * 1")): # Mondays at 9 AM |
| 125 | + calls.append(datetime.now(timezone.utc)) |
| 126 | + |
| 127 | + docket.register(scheduled_task) |
| 128 | + |
| 129 | + # Schedule for 100ms in the future (simulating next Monday 9 AM) |
| 130 | + future_time = datetime.now(timezone.utc) + timedelta(milliseconds=100) |
| 131 | + with patch.object(croniter, "get_next", return_value=future_time): |
| 132 | + await worker.run_at_most({"scheduled_task": 1}) |
| 133 | + |
| 134 | + assert len(calls) == 1 |
| 135 | + # The task ran at or after the scheduled time, not immediately |
| 136 | + assert calls[0] >= future_time - timedelta(milliseconds=50) |
0 commit comments