Skip to content
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

Fix task delay for specific time with milliseconds #404

Merged
merged 1 commit into from
Feb 4, 2025
Merged
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
7 changes: 5 additions & 2 deletions taskiq/cli/scheduler/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ def get_task_delay(task: ScheduledTask) -> Optional[int]:
return 0
return None
if task.time is not None:
task_time = to_tz_aware(task.time).replace(microsecond=0)
task_time = to_tz_aware(task.time)
if task_time <= now:
return 0
one_min_ahead = (now + timedelta(minutes=1)).replace(second=1, microsecond=0)
if task_time <= one_min_ahead:
return int((task_time - now).total_seconds())
delay = task_time - now
if delay.microseconds:
return int(delay.total_seconds()) + 1
return int(delay.total_seconds())
return None


Expand Down
18 changes: 18 additions & 0 deletions tests/cli/scheduler/test_task_delays.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,21 @@ def test_time_delay() -> None:
),
)
assert delay is not None and delay == 15


@freeze_time("2023-01-14 12:00:00.05")
def test_time_delay_with_milliseconds() -> None:
time = datetime.datetime.now(tz=pytz.UTC) + datetime.timedelta(
seconds=15,
milliseconds=150,
)
delay = get_task_delay(
ScheduledTask(
task_name="",
labels={},
args=[],
kwargs={},
time=time,
),
)
assert delay is not None and delay == 16