File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# Task
22
3- 基于 [ taskiq] ( https://github.com/taskiq-python/taskiq ) 的异步任务队列插件
3+ 基于 [ taskiq] ( https://github.com/taskiq-python/taskiq ) 的异步任务队列插件,支持 Worker 和定时任务调度器
44
5- ## 全局配置
5+ ## 插件类型
66
7- 插件自动读取项目原始配置,优先使用 ` TASKIQ_BROKER ` ,否则使用 ` CELERY_BROKER ` :
7+ - 应用级插件
8+
9+ ## 配置说明
10+
11+ 插件自动读取项目原始任务队列配置,优先使用 ` TASKIQ_BROKER ` ,否则使用 ` CELERY_BROKER `
812
913``` python
1014# Redis
@@ -23,34 +27,29 @@ CELERY_RABBITMQ_PASSWORD = 'guest'
2327CELERY_RABBITMQ_VHOST = ' /'
2428```
2529
26- ## 快速开始
27-
28- ### 1. 集成生命周期
29-
30- 在 ` backend/core/registrar.py ` 的 ` register_init ` 函数中添加:
30+ ## 使用方式
3131
32- ``` python
33- from backend.plugin.task.broker import taskiq_broker
32+ 1 . 安装并启用插件后,重启后端服务
33+ 2 . 自 fba v1.13.3 起,插件安装后会自动应用;旧版本需手动将 ` hooks.py ` 中的 lifespan 添加到
34+ ` backend/core/registrar.py::register_init ` 中
35+ 3 . 使用以下命令启动 Worker:
3436
37+ ``` bash
38+ taskiq worker backend.plugin.task.broker:taskiq_broker backend.plugin.task.tasks.beat -fsd -tp backend/plugin/task/** /tasks.py
39+ ```
3540
36- async def register_init (app ):
37- if not taskiq_broker.is_worker_process:
38- await taskiq_broker.startup()
39- yield
40- if not taskiq_broker.is_worker_process:
41- await taskiq_broker.shutdown()
42- ```
41+ 4. 如需定时任务,使用以下命令启动调度器:
4342
44- ### 2. 启动 Worker
43+ ` ` ` bash
44+ taskiq scheduler backend.plugin.task.scheduler:taskiq_scheduler backend.plugin.task.tasks.beat
45+ ` ` `
4546
46- ``` bash
47- taskiq worker backend.plugin.task.broker:taskiq_broker backend.plugin.task.tasks.beat -fsd -tp backend/plugin/task/** /tasks.py
48- ```
47+ # # 卸载说明
4948
50- ## 定时任务
49+ - 卸载插件前,先停止 Worker 和 Scheduler
50+ - 卸载插件后,清理业务中的任务调用和队列配置
5151
52- ### 启动定时任务调度器(可选)
52+ # # 联系方式
5353
54- ``` bash
55- taskiq scheduler backend.plugin.task.scheduler:taskiq_scheduler backend.plugin.task.tasks.beat
56- ```
54+ - 作者:` wu-clan`
55+ - 反馈方式:提交 Issue 或 PR
Original file line number Diff line number Diff line change 1+ from collections .abc import AsyncGenerator
2+ from contextlib import asynccontextmanager
3+
4+ from fastapi import FastAPI
5+
6+ from backend .plugin .task .broker import taskiq_broker
7+
8+
9+ @asynccontextmanager
10+ async def lifespan (_app : FastAPI ) -> AsyncGenerator [None ]:
11+ """Taskiq broker 生命周期"""
12+ if not taskiq_broker .is_worker_process :
13+ await taskiq_broker .startup ()
14+
15+ yield
16+
17+ if not taskiq_broker .is_worker_process :
18+ await taskiq_broker .shutdown ()
Original file line number Diff line number Diff line change 1- taskiq >= 0.11.7
2- taskiq-redis >= 1.0 .2
3- taskiq-aio-pika >= 0.4.1
1+ taskiq >= 0.12.2
2+ taskiq-redis >= 1.2 .2
3+ taskiq-aio-pika >= 0.6.0
You can’t perform that action at this time.
0 commit comments