From 32b1ac9a353cc1277c5d29d87d6f6fd192008c95 Mon Sep 17 00:00:00 2001 From: Jamie Matthews Date: Tue, 25 Jun 2024 10:53:31 +0100 Subject: [PATCH] Add notes on pre/post hook behaviour --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cb77aef..c4e698a 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 = { @@ -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