Skip to content

Commit 2b77100

Browse files
docs: illustrate worker middleware and result storage flow
1 parent f08a54a commit 2b77100

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

51.1 KB
Loading

‎docs/assets/worker-middleware-flow.svg‎

Lines changed: 98 additions & 0 deletions
Loading

‎docs/extending-taskiq/middleware.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,30 @@ may use `self.broker` to do so.
3030

3131
[Taskiq-pipelines](https://github.com/taskiq-python/taskiq-pipelines) uses middlewares to
3232
call next tasks.
33+
34+
## Worker hook flow
35+
36+
Worker hooks run after the message has been decoded and its task has been found.
37+
The diagram shows one execution, assuming that the middleware hooks themselves
38+
complete normally:
39+
40+
![Worker middleware flow: pre_execute, task execution, optional on_error, post_execute, then result storage and post_save only if the result is saved successfully. NoResultError skips storage.](../assets/worker-middleware-flow.png)
41+
42+
*Worker middleware flow. [Editable SVG](../assets/worker-middleware-flow.svg).
43+
Diagram created with [svgdiagram.ai](https://svgdiagram.ai).*
44+
45+
1. `pre_execute` can modify the message before the task runs.
46+
2. Task execution and dependency cleanup produce the result. If an execution
47+
error was found, `on_error` runs before `post_execute`.
48+
3. `post_execute` runs for both successful and failed task results.
49+
4. A result containing `NoResultError` skips result storage and `post_save`.
50+
Other results, including ordinary task errors, are passed to the result backend.
51+
5. `post_save` runs only after the backend saves the result successfully. A save
52+
error skips `post_save`; it is logged, and can also be propagated by the receiver.
53+
54+
With multiple registered middlewares, `pre_execute` runs in registration order.
55+
`on_error`, `post_execute`, and `post_save` run in reverse registration order.
56+
These worker hooks are separate from the client-side `pre_send` and `post_send`
57+
hooks. Exceptions raised by middleware hooks can interrupt this flow, so handle
58+
them inside the middleware as described in the
59+
[architecture overview](../guide/architecture-overview.md#middlewares).

0 commit comments

Comments
 (0)