Skip to content

Commit e405ce5

Browse files
committed
always use DB clock when adding a task
1 parent 0b08e21 commit e405ce5

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

postgrestq/task_queue.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,14 @@ def add(
156156
job dies.
157157
can_start_at : datetime
158158
The earliest time the task can be started.
159-
If None, set current time. A task will not be started before this
160-
time.
159+
If None, set current time. For consistency the time is
160+
from the database clock. A task will not be started before
161+
this time.
161162
Returns
162163
-------
163164
task_id :
164165
The random UUID that was generated for this task
165166
"""
166-
if can_start_at is None:
167-
can_start_at = datetime.now(UTC)
168167
# make sure the timeout is an actual number, otherwise we'll run
169168
# into problems later when we calculate the actual deadline
170169
lease_timeout = float(lease_timeout)
@@ -186,7 +185,7 @@ def add(
186185
lease_timeout,
187186
can_start_at
188187
)
189-
VALUES (%s, %s, %s, %s, %s, %s)
188+
VALUES (%s, %s, %s, %s, %s, COALESCE(%s, current_timestamp))
190189
"""
191190
).format(sql.Identifier(self._table_name)),
192191
(
@@ -222,16 +221,15 @@ def add_many(
222221
job dies.
223222
can_start_at : datetime
224223
The earliest time the task can be started.
225-
If None, set current time. A task will not be started before this
224+
If None, set current time. For consistency the time is
225+
from the database clock. A task will not be started before this
226226
time.
227227
Returns
228228
-------
229229
task_ids :
230230
List of random UUIDs that were generated for this task.
231231
The order is the same of the given tasks
232232
"""
233-
if can_start_at is None:
234-
can_start_at = datetime.now(UTC)
235233
# make sure the timeout is an actual number, otherwise we'll run
236234
# into problems later when we calculate the actual deadline
237235
lease_timeout = float(lease_timeout)
@@ -253,7 +251,9 @@ def add_many(
253251
lease_timeout,
254252
can_start_at
255253
)
256-
VALUES (%s, %s, %s, %s, %s, %s)
254+
VALUES (
255+
%s, %s, %s, %s, %s, COALESCE(%s, current_timestamp)
256+
)
257257
"""
258258
).format(sql.Identifier(self._table_name)),
259259
(

0 commit comments

Comments
 (0)