Skip to content

Commit 0ccea7e

Browse files
authored
Merge pull request #11 from flix-tech/handle_duplicated_completed_task
Handle duplicated completed task
2 parents 5106ed4 + 0dd9ee4 commit 0ccea7e

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.0 - 2024-07-01
4+
5+
* The `complete()` method now returns the count of updated tasks, 0 if it was already completed
6+
37
## 1.0.1 - 2024-05-27
48

59
* Restrict the type yielded by the generator to never be None

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.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ log_cli_date_format = "%Y-%m-%d %H:%M:%S"
1818

1919
[project]
2020
name = "postgres-tq"
21-
version = "1.0.1"
21+
version = "1.1.0"
2222
description = "Postgres Based Task Queue"
2323
authors = [
2424
{name = "FlixTech", email = "[email protected]"},

0 commit comments

Comments
 (0)