You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,6 +112,35 @@ JOBS = {
112
112
}
113
113
```
114
114
115
+
#### Pre & Post Task Hooks
116
+
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.
117
+
118
+
Both pre and post task hooks receive your `Job` instance as their only argument. Here's an example:
119
+
120
+
```python
121
+
defmy_pre_task_hook(job):
122
+
...# configure something before running your task
123
+
```
124
+
125
+
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:
* 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.
140
+
* The `post_task_hook` is always run, even if the job fails. In this case, it runs after the `failure_hook`.
141
+
* 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.
0 commit comments