Skip to content

Commit

Permalink
Make check_expired_leases public
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopofar committed Nov 6, 2023
1 parent 458c2b3 commit e9d38a7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.0.4 - 2023-11-06

* change name of `check_expired_leases()` to make it a public method

## 0.0.3 - 2023-10-05

* add function to delete old completed tasks
Expand Down
11 changes: 6 additions & 5 deletions postgrestq/task_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,21 @@ def is_empty(self) -> bool:
bool
"""
self._check_expired_leases()
self.check_expired_leases()
return len(self) == 0

def _check_expired_leases(self) -> None:
"""Check for expired leases.
def check_expired_leases(self) -> None:
"""Check for expired leases and put the task back if needed.
This method goes through all tasks that are currently processed
and checks if their deadline expired. If not we assume the
worker died. We decrease the TTL and if TTL is still > 0 we
reschedule the task into the task queue or, if the TTL is
exhausted, we mark the task as completed by setting
`completed_at` column with current timestamp.
`completed_at` column with current timestamp and call the
expired task callback if it's set.
Note: lease check is only performed against the tasks in
Note: lease check is only performed against the tasks
that are processing.
"""
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 = "0.0.3"
version = "0.0.4"
description = "Postgres Based Task Queue"
authors = [
{name = "FlixTech", email = "[email protected]"},
Expand Down
4 changes: 2 additions & 2 deletions tests/test_task_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def test_expired_leases_race(
# save the original function so we can use it inside the mock
get_orig = task_queue.get_updated_expired_task

# simulate a race condition in _check_expired_leases where we can
# simulate a race condition in check_expired_leases where we can
# still see a task in the set of tasks but by the time we try to get
# it from the queue it has been completed, i.e. is None
def mock_get(task_id: UUID):
Expand All @@ -282,7 +282,7 @@ def mock_get(task_id: UUID):

monkeypatch.setattr(task_queue, "get_updated_expired_task", mock_get)
caplog.set_level(logging.INFO)
task_queue._check_expired_leases()
task_queue.check_expired_leases()
assert "marked completed while we checked for" in caplog.text


Expand Down

0 comments on commit e9d38a7

Please sign in to comment.