Skip to content

Add seconds and milliseconds support in cron_offset #467

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
6 changes: 5 additions & 1 deletion taskiq/cli/scheduler/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ def get_task_delay(task: ScheduledTask) -> Optional[int]:
if task.cron is not None:
# If user specified cron offset we apply it.
# If it's timedelta, we simply add the delta to current time.
additional_seconds = 0
if task.cron_offset and isinstance(task.cron_offset, timedelta):
now += task.cron_offset
additional_seconds += task.cron_offset.seconds + (
1 if task.cron_offset.microseconds else 0
)
# If timezone was specified as string we convert it timzone
# offset and then apply.
elif task.cron_offset and isinstance(task.cron_offset, str):
now = now.astimezone(pytz.timezone(task.cron_offset))
if is_now(task.cron, now):
return 0
return 0 + additional_seconds
return None
if task.time is not None:
task_time = to_tz_aware(task.time)
Expand Down
32 changes: 32 additions & 0 deletions tests/cli/scheduler/test_task_delays.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,35 @@ def test_time_delay_with_milliseconds() -> None:
),
)
assert delay is not None and delay == 16


@freeze_time("2025-05-22 12:00:00")
def test_cron_offset_delay_with_seconds() -> None:
seconds_offset = 2
delay = get_task_delay(
ScheduledTask(
task_name="",
labels={},
args=[],
kwargs={},
cron="* 12 * * *",
cron_offset=datetime.timedelta(seconds=seconds_offset),
),
)
assert delay is not None and delay == seconds_offset


@freeze_time("2025-05-22 12:00:00")
def test_cron_offset_delay_with_seconds_and_milliseconds() -> None:
seconds_offset = 2
delay = get_task_delay(
ScheduledTask(
task_name="",
labels={},
args=[],
kwargs={},
cron="* 12 * * *",
cron_offset=datetime.timedelta(seconds=seconds_offset, milliseconds=1),
),
)
assert delay is not None and delay == seconds_offset + 1