Skip to content

fix(tasks) Fix crontab schedules triggering with no last_run #97557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sentry/taskworker/scheduler/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def monitor_value(self) -> str:
def is_due(self, last_run: datetime | None = None) -> bool:
"""Check if the schedule is due to run again based on last_run."""
if last_run is None:
return True
last_run = timezone.now() - timedelta(minutes=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this hardcoded to one minute?

remaining = self.remaining_seconds(last_run)
return remaining <= 0

Expand All @@ -135,7 +135,7 @@ def remaining_seconds(self, last_run: datetime | None = None) -> int:
Use the current time to find the next schedule time
"""
if last_run is None:
return 0
last_run = timezone.now() - timedelta(minutes=1)

# This could result in missed beats, or increased load on redis.
last_run = last_run.replace(second=0, microsecond=0)
Expand Down
16 changes: 7 additions & 9 deletions tests/sentry/taskworker/scheduler/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,42 +377,40 @@ def test_schedulerunner_tick_fast_and_slow(
assert sleep_time == 30

called = extract_sent_tasks(mock_send)
assert called == ["second", "valid"]
assert called == ["valid"]

run_storage.delete("test:valid")
with freeze_time("2025-01-24 14:25:30"):
sleep_time = schedule_set.tick()
assert sleep_time == 30

called = extract_sent_tasks(mock_send)
assert called == ["second", "valid", "valid"]
assert called == ["valid", "valid"]

run_storage.delete("test:valid")
run_storage.delete("test:second")
with freeze_time("2025-01-24 14:26:01"):
with freeze_time("2025-01-24 14:26:00"):
sleep_time = schedule_set.tick()
assert sleep_time == 30

called = extract_sent_tasks(mock_send)
assert called == ["second", "valid", "valid", "second", "valid"]
assert called == ["valid", "valid", "second", "valid"]

run_storage.delete("test:valid")
with freeze_time("2025-01-24 14:26:31"):
with freeze_time("2025-01-24 14:26:30"):
sleep_time = schedule_set.tick()
assert sleep_time == 30

called = extract_sent_tasks(mock_send)
assert called == ["second", "valid", "valid", "second", "valid", "valid"]
assert called == ["valid", "valid", "second", "valid", "valid"]

run_storage.delete("test:valid")
with freeze_time("2025-01-24 14:27:01"):
with freeze_time("2025-01-24 14:27:00"):
sleep_time = schedule_set.tick()
assert sleep_time == 30

assert run_storage.read("test:valid")
called = extract_sent_tasks(mock_send)
assert called == [
"second",
"valid",
"valid",
"second",
Expand Down
16 changes: 15 additions & 1 deletion tests/sentry/taskworker/scheduler/test_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def test_crontabschedule_invalid() -> None:
def test_crontabschedule_is_due() -> None:
schedule = CrontabSchedule("test", crontab(minute="*/5"))

# no last_run and not time to spawn
with freeze_time("2025-01-24 14:23:00"):
now = timezone.now()
assert not schedule.is_due(None)
assert not schedule.is_due(now)

with freeze_time("2025-01-24 14:25:00"):
now = timezone.now()
assert schedule.is_due(None)
Expand Down Expand Up @@ -109,7 +115,15 @@ def test_crontabschedule_is_due() -> None:
def test_crontabschedule_remaining_seconds() -> None:
schedule = CrontabSchedule("test", crontab(minute="*/5"))

assert schedule.remaining_seconds(None) == 0
# no last_run, but due in one minute
with freeze_time("2025-01-24 14:24:00"):
assert not schedule.is_due(None)
assert schedule.remaining_seconds(None) == 60

# no last_run, but due now
with freeze_time("2025-01-24 14:25:00"):
assert schedule.is_due(None)
assert schedule.remaining_seconds(None) == 0

# last run was late (14:21), next spawn is at 14:25
with freeze_time("2025-01-24 14:25:00"):
Expand Down
Loading