Skip to content

Commit

Permalink
Merge pull request #11 from flix-tech/handle_duplicated_completed_task
Browse files Browse the repository at this point in the history
Handle duplicated completed task
  • Loading branch information
jacopofar authored Jul 2, 2024
2 parents 5106ed4 + 0dd9ee4 commit 0ccea7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.0 - 2024-07-01

* The `complete()` method now returns the count of updated tasks, 0 if it was already completed

## 1.0.1 - 2024-05-27

* Restrict the type yielded by the generator to never be None
Expand Down
15 changes: 13 additions & 2 deletions postgrestq/task_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def get_many(self, amount: int) -> Sequence[
conn.commit()
return ret

def complete(self, task_id: Optional[UUID]) -> None:
def complete(self, task_id: Optional[UUID]) -> int:
"""Mark a task as completed.
Marks a task as completed by setting completed_at column by
Expand All @@ -409,20 +409,31 @@ def complete(self, task_id: Optional[UUID]) -> None:
task_id : UUID | None
the task ID
Returns
-------
the number of updated rows: int
"""
logger.info(f"Marking task {task_id} as completed")
conn = self.conn
count = 0
with conn.cursor() as cur:
cur.execute(
sql.SQL(
"""
UPDATE {}
SET completed_at = current_timestamp
WHERE id = %s"""
WHERE id = %s
AND completed_at is NULL"""
).format(sql.Identifier(self._table_name)),
(task_id,),
)
count = cur.rowcount
if count == 0:
logger.info(f"Task {task_id} was already completed")

conn.commit()
return count

def is_empty(self) -> bool:
"""Check if the task queue is empty.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ log_cli_date_format = "%Y-%m-%d %H:%M:%S"

[project]
name = "postgres-tq"
version = "1.0.1"
version = "1.1.0"
description = "Postgres Based Task Queue"
authors = [
{name = "FlixTech", email = "[email protected]"},
Expand Down

0 comments on commit 0ccea7e

Please sign in to comment.