diff --git a/CHANGELOG.md b/CHANGELOG.md index 03ad910..a47fce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/postgrestq/task_queue.py b/postgrestq/task_queue.py index 33d2fea..e7f32e2 100644 --- a/postgrestq/task_queue.py +++ b/postgrestq/task_queue.py @@ -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. """ diff --git a/pyproject.toml b/pyproject.toml index e10d726..a199381 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = "open-source@flixbus.com"}, diff --git a/tests/test_task_queue.py b/tests/test_task_queue.py index c8847a9..1a19c19 100644 --- a/tests/test_task_queue.py +++ b/tests/test_task_queue.py @@ -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): @@ -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