Skip to content

Commit

Permalink
Merge pull request #1146 from katlyn/feat/clean-cancelled-aborted-jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim authored Aug 8, 2024
2 parents af6131b + 9970d03 commit 821db68
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/howto/production/delete_finished_jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ async def remove_old_jobs(context, timestamp):
context,
max_hours=72,
remove_error=True,
remove_cancelled=True,
remove_aborted=True,
)
```

Expand Down
16 changes: 14 additions & 2 deletions procrastinate/builtin_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
24 changes: 17 additions & 7 deletions procrastinate/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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"],
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/test_builtin_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
)
]

0 comments on commit 821db68

Please sign in to comment.