@@ -18,91 +18,12 @@ Tasks Page | Task Details Page
1818
1919### Usage
2020
21- 1 ) Add this middleware to your project :
21+ 1 ) Import and connect the middleware to the broker :
2222
23- ``` python
24- from typing import Any
25- from urllib.parse import urljoin
26- from datetime import datetime, UTC
27-
28- import httpx
29- from taskiq import TaskiqMiddleware, TaskiqResult, TaskiqMessage
30-
31- class TaskiqAdminMiddleware (TaskiqMiddleware ):
32- def __init__ (
33- self ,
34- url : str ,
35- api_token : str ,
36- taskiq_broker_name : str | None = None ,
37- ):
38- super ().__init__ ()
39- self .url = url
40- self .api_token = api_token
41- self .__ta_broker_name = taskiq_broker_name
42-
43- async def post_send (self , message ):
44- now = datetime.now(UTC ).replace(tzinfo = None ).isoformat()
45- async with httpx.AsyncClient() as client:
46- await client.post(
47- headers = {" access-token" : self .api_token},
48- url = urljoin(self .url, f " /api/tasks/ { message.task_id} /queued " ),
49- json = {
50- " args" : message.args,
51- " kwargs" : message.kwargs,
52- " taskName" : message.task_name,
53- " worker" : self .__ta_broker_name,
54- " queuedAt" : now,
55- },
56- )
57- return super ().post_send(message)
58-
59- async def pre_execute (self , message : TaskiqMessage):
60- """ """
61- now = datetime.now(UTC ).replace(tzinfo = None ).isoformat()
62- async with httpx.AsyncClient() as client:
63- await client.post(
64- headers = {" access-token" : self .api_token},
65- url = urljoin(self .url, f " /api/tasks/ { message.task_id} /started " ),
66- json = {
67- " startedAt" : now,
68- " args" : message.args,
69- " kwargs" : message.kwargs,
70- " taskName" : message.task_name,
71- " worker" : self .__ta_broker_name,
72- },
73- )
74- return super ().pre_execute(message)
75-
76- async def post_execute (
77- self ,
78- message : TaskiqMessage,
79- result : TaskiqResult[Any],
80- ):
81- """ """
82- now = datetime.now(UTC ).replace(tzinfo = None ).isoformat()
83- async with httpx.AsyncClient() as client:
84- await client.post(
85- headers = {" access-token" : self .api_token},
86- url = urljoin(
87- self .url,
88- f " /api/tasks/ { message.task_id} /executed " ,
89- ),
90- json = {
91- " finishedAt" : now,
92- " error" : result.error
93- if result.error is None
94- else repr (result.error),
95- " executionTime" : result.execution_time,
96- " returnValue" : {" return_value" : result.return_value},
97- },
98- )
99- return super ().post_execute(message, result)
100- ```
101-
102- 2 ) Connect the middleware to your broker:
103-
10423``` python
10524...
25+ from taskiq.middlewares.taskiq_admin_middleware import TaskiqAdminMiddleware
26+
10627broker = (
10728 RedisStreamBroker(
10829 url = redis_url,
@@ -120,9 +41,9 @@ broker = (
12041...
12142```
12243
123- 3 ) Pull the image from GitHub Container Registry: ` docker pull ghcr.io/taskiq-python/taskiq-admin:latest `
44+ 2 ) Pull the image from GitHub Container Registry: ` docker pull ghcr.io/taskiq-python/taskiq-admin:latest `
12445
125- 4 ) Replace ` TASKIQ_ADMIN_API_TOKEN ` with any secret enough string and run:
46+ 3 ) Replace ` TASKIQ_ADMIN_API_TOKEN ` with any secret enough string and run:
12647``` bash
12748docker run -d --rm \
12849 -p " 3000:3000" \
@@ -132,7 +53,7 @@ docker run -d --rm \
13253 " ghcr.io/taskiq-python/taskiq-admin:latest"
13354```
13455
135- 5 ) Go to ` http://localhost:3000/tasks `
56+ 4 ) Go to ` http://localhost:3000/tasks `
13657
13758### Docker Compose Example
13859
0 commit comments