Skip to content

Commit

Permalink
Add notes on pre/post hook behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
j4mie committed Jun 25, 2024
1 parent c52941f commit 32b1ac9
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ JOBS = {
```

#### Pre & Post Task Hooks
You can also run pre task or post task hooks, which happen in the normal processing of your `Job` instances and are executed in the worker process.
You can also run pre task or post task hooks, which happen in the normal processing of your `Job` instances and are executed inside the worker process.

Both pre and post task hooks receive your `Job` instance as their only argument. Here's an example:

Expand All @@ -122,7 +122,7 @@ def my_pre_task_hook(job):
... # configure something before running your task
```

To ensure these hooks gets run, simply add a `pre_task_hook` or `post_task_hook` key (or both, if needed) to your job config like so:
To ensure these hooks are run, simply add a `pre_task_hook` or `post_task_hook` key (or both, if needed) to your job config like so:

```python
JOBS = {
Expand All @@ -134,6 +134,12 @@ JOBS = {
}
```

Notes:

* If the `pre_task_hook` fails (raises an exception), the task function is not run, and django-db-queue behaves as if the task function itself had failed: the failure hook is called, and the job is goes into the `FAILED` state.
* The `post_task_hook` is always run, even if the job fails. In this case, it runs after the `failure_hook`.
* If the `post_task_hook` raises an exception, this is logged but the the job is **not marked as failed** and the failure hook does not run. This is because the `post_task_hook` might need to perform cleanup that always happens after the task, no matter whether it succeeds or fails.


### Start the worker

Expand Down

0 comments on commit 32b1ac9

Please sign in to comment.