Skip to content

Commit

Permalink
Merge pull request #9 from flix-tech/return-task-id
Browse files Browse the repository at this point in the history
Return the task id when adding it
  • Loading branch information
jacopofar authored Feb 13, 2024
2 parents aee122a + 81edf02 commit ecbf074
Show file tree
Hide file tree
Showing 3 changed files with 14 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

## Unreleased

* adding a task now returns its UUID, previously nothign was returned

## 0.0.5 - 2023-11-06

* Upgraded dependencies, now this library requires Python 3.9 (previously was 3.8)
Expand Down
7 changes: 6 additions & 1 deletion postgrestq/task_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __len__(self) -> int:

def add(
self, task: Dict[str, Any], lease_timeout: float, ttl: int = 3
) -> None:
) -> str:
"""Add a task to the task queue.
Parameters
Expand All @@ -129,6 +129,10 @@ def add(
Number of (re-)tries, including the initial one, in case the
job dies.
Returns
-------
task_id :
The random UUID that was generated for this task
"""
# make sure the timeout is an actual number, otherwise we'll run
# into problems later when we calculate the actual deadline
Expand Down Expand Up @@ -156,6 +160,7 @@ def add(
(id_, self._queue_name, serialized_task, ttl, lease_timeout),
)
self.conn.commit()
return id_

def get(self) -> Tuple[Optional[Dict[str, Any]], Optional[UUID]]:
"""Get a task from the task queue (non-blocking).
Expand Down
6 changes: 4 additions & 2 deletions tests/test_task_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ def task_queue():
def test_add(task_queue: TaskQueue):
# add two tasks and get them back in correct order
TASKS = [{"foo": 1}, {"bar": 2}]
task_ids = set()
for task in TASKS:
task_queue.add(task, LEASE_TIMEOUT)

tid = task_queue.add(task, LEASE_TIMEOUT)
task_ids.add(tid)
assert len(task_ids) == 2
task, _ = task_queue.get()
assert task == TASKS[0]
task, _ = task_queue.get()
Expand Down

0 comments on commit ecbf074

Please sign in to comment.