Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle duplicated completed task #11

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading