Skip to content

Commit 988cb76

Browse files
committed
return updated rowcount when completing a task
1 parent 5106ed4 commit 988cb76

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

postgrestq/task_queue.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def get_many(self, amount: int) -> Sequence[
394394
conn.commit()
395395
return ret
396396

397-
def complete(self, task_id: Optional[UUID]) -> None:
397+
def complete(self, task_id: Optional[UUID]) -> int:
398398
"""Mark a task as completed.
399399
400400
Marks a task as completed by setting completed_at column by
@@ -409,20 +409,31 @@ def complete(self, task_id: Optional[UUID]) -> None:
409409
task_id : UUID | None
410410
the task ID
411411
412+
Returns
413+
-------
414+
the number of updated rows: int
415+
412416
"""
413417
logger.info(f"Marking task {task_id} as completed")
414418
conn = self.conn
419+
count = 0
415420
with conn.cursor() as cur:
416421
cur.execute(
417422
sql.SQL(
418423
"""
419424
UPDATE {}
420425
SET completed_at = current_timestamp
421-
WHERE id = %s"""
426+
WHERE id = %s
427+
AND completed_at is NULL"""
422428
).format(sql.Identifier(self._table_name)),
423429
(task_id,),
424430
)
431+
count = cur.rowcount
432+
if count == 0:
433+
logger.info(f"Task {task_id} was already completed")
434+
425435
conn.commit()
436+
return count
426437

427438
def is_empty(self) -> bool:
428439
"""Check if the task queue is empty.

0 commit comments

Comments
 (0)