-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1081 from openradx/cancel-job-feature
- Loading branch information
Showing
22 changed files
with
919 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Cancel a job | ||
|
||
We can cancel a job that has not yet been processed by a worker. We can also | ||
mark a job that is currently being processed for abortion, but this request | ||
has to be handled by the task itself. | ||
|
||
## Cancel a job (that is not being processed yet) | ||
|
||
```python | ||
# by using the sync method | ||
app.job_manager.cancel_job_by_id(33) | ||
# or by using the async method | ||
await app.job_manager.cancel_job_by_id_async(33) | ||
``` | ||
|
||
## Delete the cancelled job | ||
|
||
A cancelled job can also be deleted from the database. | ||
|
||
```python | ||
# by using the sync method | ||
app.job_manager.cancel_job_by_id(33, delete_job=True) | ||
# or by using the async method | ||
await app.job_manager.cancel_job_by_id_async(33, delete_job=True) | ||
``` | ||
|
||
## Mark a currently being processed job for abortion | ||
|
||
If a worker has not picked up the job yet, the below command behaves like the | ||
command without the `abort` option. But if a job is already in the middle of | ||
being processed, the `abort` option marks this job for abortion (see below | ||
how to handle this request). | ||
|
||
```python | ||
# by using the sync method | ||
app.job_manager.cancel_job_by_id(33, abort=True) | ||
# or by using the async method | ||
await app.job_manager.cancel_job_by_id_async(33, abort=True) | ||
``` | ||
|
||
## Handle a abortion request inside the task | ||
|
||
In our task, we can check (for example, periodically) if the task should be | ||
aborted. If we want to respect that request (we don't have to), we raise a | ||
`JobAborted` error. Any message passed to `JobAborted` (e.g. | ||
`raise JobAborted("custom message")`) will end up in the logs. | ||
|
||
```python | ||
@app.task(pass_context=True) | ||
def my_task(context): | ||
for i in range(100): | ||
if context.should_abort(): | ||
raise exceptions.JobAborted | ||
do_something_expensive() | ||
``` | ||
|
||
There is also an async API | ||
|
||
```python | ||
@app.task(pass_context=True) | ||
async def my_task(context): | ||
for i in range(100): | ||
if await context.should_abort_async(): | ||
raise exceptions.JobAborted | ||
do_something_expensive() | ||
``` | ||
|
||
:::{warning} | ||
`context.should_abort()` and `context.should_abort_async()` does poll the | ||
database and might flood the database. Ensure you do it only sometimes and | ||
not from too many parallel tasks. | ||
::: |
47 changes: 47 additions & 0 deletions
47
procrastinate/contrib/django/migrations/0028_add_cancel_states.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
from django.db import migrations, models | ||
|
||
from .. import migrations_utils | ||
|
||
|
||
class Migration(migrations.Migration): | ||
operations = [ | ||
migrations_utils.RunProcrastinateSQL(name="02.06.00_01_add_cancel_states.sql"), | ||
migrations.AlterField( | ||
"procrastinatejob", | ||
"status", | ||
models.CharField( | ||
choices=[ | ||
("todo", "todo"), | ||
("doing", "doing"), | ||
("succeeded", "succeeded"), | ||
("failed", "failed"), | ||
("cancelled", "cancelled"), | ||
("aborting", "aborting"), | ||
("aborted", "aborted"), | ||
], | ||
max_length=32, | ||
), | ||
), | ||
migrations.AlterField( | ||
"procrastinateevent", | ||
"type", | ||
models.CharField( | ||
choices=[ | ||
("deferred", "deferred"), | ||
("started", "started"), | ||
("deferred_for_retry", "deferred_for_retry"), | ||
("failed", "failed"), | ||
("succeeded", "succeeded"), | ||
("cancelled", "cancelled"), | ||
("abort_requested", "abort_requested"), | ||
("aborted", "aborted"), | ||
("scheduled", "scheduled"), | ||
], | ||
max_length=32, | ||
), | ||
), | ||
] | ||
name = "0028_add_cancel_states" | ||
dependencies = [("procrastinate", "0027_add_periodic_job_priority")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.