Use failure, creation, and task hooks
masterHooks allow you to execute code at different stages of a job's lifecycle. They are configured in the JOBS dictionary in your settings.
Hook Types
| Hook Type | Config Key | Execution Context | Arguments | Behavior |
|---|---|---|---|---|
| Failure | failure_hook | Worker process | (job, exception) | Runs when an unhandled exception occurs. |
| Creation | creation_hook | Process that created the job | (job) | Runs immediately after Job.objects.create() call. |
| Pre-Task | pre_task_hook | Worker process | (job) | Runs before the task. If it fails, the job is marked FAILED. |
| Post-Task | post_task_hook | Worker process | (job) | Runs after the task. Always runs, even on failure. If it fails, the job is not marked as failed. |
Implementation Example
# settings.py
JOBS = {
"my_job": {
"tasks": ["project.common.jobs.my_task"],
"failure_hook": "project.common.jobs.my_task_failure_hook",
"creation_hook": "project.common.jobs.my_task_creation_hook",
"pre_task_hook": "project.common.jobs.my_pre_task_hook",
"post_task_hook": "project.common.jobs.my_post_task_hook",
},
}