From 296f0a74381ef1455c49113d97fe4ef11a0c58ab Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Wed, 7 Aug 2024 09:59:47 -0800 Subject: [PATCH 1/3] feat: add cancelled and aborted jobs to delete_old_jobs --- procrastinate/builtin_tasks.py | 16 ++++++++++++++-- procrastinate/manager.py | 24 +++++++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/procrastinate/builtin_tasks.py b/procrastinate/builtin_tasks.py index f419558f9..c31afd5ef 100644 --- a/procrastinate/builtin_tasks.py +++ b/procrastinate/builtin_tasks.py @@ -12,6 +12,8 @@ async def remove_old_jobs( max_hours: int, queue: str | None = None, remove_error: bool | None = False, + remove_cancelled: bool | None = False, + remove_aborted: bool | None = False, ) -> None: """ This task cleans your database by removing old jobs. Note that jobs and linked @@ -24,11 +26,21 @@ async def remove_old_jobs( queue : The name of the queue in which jobs will be deleted. If not specified, the task will delete jobs from all queues. - remove_error : + remove_error : ``Optional[bool]`` By default only successful jobs will be removed. When this parameter is True failed jobs will also be deleted. + remove_cancelled : ``Optional[bool]`` + By default only successful jobs will be removed. When this parameter is True + cancelled jobs will also be deleted. + remove_aborted : ``Optional[bool]`` + By default only successful jobs will be removed. When this parameter is True + aborted jobs will also be deleted. """ assert context.app await context.app.job_manager.delete_old_jobs( - nb_hours=max_hours, queue=queue, include_error=remove_error + nb_hours=max_hours, + queue=queue, + include_error=remove_error, + include_cancelled=remove_cancelled, + include_aborted=remove_aborted, ) diff --git a/procrastinate/manager.py b/procrastinate/manager.py index 9657e4528..129b50962 100644 --- a/procrastinate/manager.py +++ b/procrastinate/manager.py @@ -181,9 +181,13 @@ async def delete_old_jobs( nb_hours: int, queue: str | None = None, include_error: bool | None = False, + include_cancelled: bool | None = False, + include_aborted: bool | None = False, ) -> None: """ - Delete jobs that have reached a final state (``succeeded`` or ``failed``). + Delete jobs that have reached a final state (``succeeded``, ``failed``, + ``cancelled``, or ``aborted``). By default, only considers jobs that have + succeeded. Parameters ---------- @@ -192,14 +196,20 @@ async def delete_old_jobs( queue : ``Optional[str]`` Filter by job queue name include_error : ``Optional[bool]`` - If ``True``, only succeeded jobs will be considered. If ``False``, both - succeeded and failed jobs will be considered, ``False`` by default + If ``True``, also consider errored jobs. ``False`` by default + include_cancelled : ``Optional[bool]`` + If ``True``, also consider cancelled jobs. ``False`` by default. + include_aborted : ``Optional[bool]`` + If ``True``, also consider aborted jobs. ``False`` by default. """ # We only consider finished jobs by default - if not include_error: - statuses = [jobs.Status.SUCCEEDED.value] - else: - statuses = [jobs.Status.SUCCEEDED.value, jobs.Status.FAILED.value] + statuses = [jobs.Status.SUCCEEDED.value] + if include_error: + statuses.append(jobs.Status.FAILED.value) + if include_cancelled: + statuses.append(jobs.Status.CANCELLED.value) + if include_aborted: + statuses.append(jobs.Status.ABORTED.value) await self.connector.execute_query_async( query=sql.queries["delete_old_jobs"], From 1b5a651e802a6627e5c623a9ca5ca736163e1b11 Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Wed, 7 Aug 2024 10:04:01 -0800 Subject: [PATCH 2/3] test: add cancelled and aborted jobs to remove_old_jobs --- tests/unit/test_builtin_tasks.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_builtin_tasks.py b/tests/unit/test_builtin_tasks.py index 7c577b478..8849f9878 100644 --- a/tests/unit/test_builtin_tasks.py +++ b/tests/unit/test_builtin_tasks.py @@ -5,11 +5,20 @@ async def test_remove_old_jobs(app): await builtin_tasks.remove_old_jobs( - job_context.JobContext(app=app), max_hours=2, queue="queue_a", remove_error=True + job_context.JobContext(app=app), + max_hours=2, + queue="queue_a", + remove_error=True, + remove_cancelled=True, + remove_aborted=True, ) assert app.connector.queries == [ ( "delete_old_jobs", - {"nb_hours": 2, "queue": "queue_a", "statuses": ["succeeded", "failed"]}, + { + "nb_hours": 2, + "queue": "queue_a", + "statuses": ["succeeded", "failed", "cancelled", "aborted"], + }, ) ] From 9970d03385c91806fa41bd55c14e085f6ff63ea3 Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Wed, 7 Aug 2024 10:04:43 -0800 Subject: [PATCH 3/3] docs: add removing cancelled and aborted jobs to example --- docs/howto/production/delete_finished_jobs.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/howto/production/delete_finished_jobs.md b/docs/howto/production/delete_finished_jobs.md index e47ecbfb8..14a7502a7 100644 --- a/docs/howto/production/delete_finished_jobs.md +++ b/docs/howto/production/delete_finished_jobs.md @@ -88,6 +88,8 @@ async def remove_old_jobs(context, timestamp): context, max_hours=72, remove_error=True, + remove_cancelled=True, + remove_aborted=True, ) ```