Skip to content

Commit 59e5848

Browse files
author
b_alekseev
committed
Added seconds support in cron_offset
1 parent 2620fc3 commit 59e5848

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

taskiq/cli/scheduler/run.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,18 @@ def get_task_delay(task: ScheduledTask) -> Optional[int]:
8585
if task.cron is not None:
8686
# If user specified cron offset we apply it.
8787
# If it's timedelta, we simply add the delta to current time.
88+
additional_seconds = 0
8889
if task.cron_offset and isinstance(task.cron_offset, timedelta):
8990
now += task.cron_offset
91+
additional_seconds += task.cron_offset.seconds + (
92+
1 if task.cron_offset.microseconds else 0
93+
)
9094
# If timezone was specified as string we convert it timzone
9195
# offset and then apply.
9296
elif task.cron_offset and isinstance(task.cron_offset, str):
9397
now = now.astimezone(pytz.timezone(task.cron_offset))
9498
if is_now(task.cron, now):
95-
return 0
99+
return 0 + additional_seconds
96100
return None
97101
if task.time is not None:
98102
task_time = to_tz_aware(task.time)

tests/cli/scheduler/test_task_delays.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,35 @@ def test_time_delay_with_milliseconds() -> None:
150150
),
151151
)
152152
assert delay is not None and delay == 16
153+
154+
155+
@freeze_time("2025-05-22 12:00:00")
156+
def test_cron_offset_delay_with_seconds() -> None:
157+
seconds_offset = 2
158+
delay = get_task_delay(
159+
ScheduledTask(
160+
task_name="",
161+
labels={},
162+
args=[],
163+
kwargs={},
164+
cron="* 12 * * *",
165+
cron_offset=datetime.timedelta(seconds=seconds_offset),
166+
),
167+
)
168+
assert delay is not None and delay == seconds_offset
169+
170+
171+
@freeze_time("2025-05-22 12:00:00")
172+
def test_cron_offset_delay_with_seconds_and_milliseconds() -> None:
173+
seconds_offset = 2
174+
delay = get_task_delay(
175+
ScheduledTask(
176+
task_name="",
177+
labels={},
178+
args=[],
179+
kwargs={},
180+
cron="* 12 * * *",
181+
cron_offset=datetime.timedelta(seconds=seconds_offset, milliseconds=1),
182+
),
183+
)
184+
assert delay is not None and delay == seconds_offset + 1

0 commit comments

Comments
 (0)