104 lines
2.1 KiB
Markdown
104 lines
2.1 KiB
Markdown
# Celery 定时任务项目 + Flower监控
|
||
|
||
## 项目结构
|
||
- `celery_app.py`: Celery应用配置
|
||
- `tasks.py`: 定义任务函数
|
||
- `scheduler.py`: 定时任务调度配置
|
||
- `run_worker.py`: 启动Worker的脚本
|
||
- `run_beat.py`: 启动Beat调度器的脚本
|
||
- `run_flower.py`: 启动Flower监控的脚本
|
||
- `flower_config.py`: Flower监控配置
|
||
- `start_all.py`: 一键启动所有服务
|
||
- `test_task.py`: 测试脚本
|
||
|
||
## 安装依赖
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
## 启动Redis
|
||
确保Redis服务已启动,默认监听localhost:6379
|
||
|
||
## 运行项目
|
||
|
||
### 方式一:一键启动(推荐)
|
||
```bash
|
||
python start_all.py
|
||
```
|
||
|
||
### 方式二:分别启动
|
||
1. 启动Celery Worker
|
||
```bash
|
||
python run_worker.py
|
||
```
|
||
|
||
2. 启动Celery Beat调度器
|
||
```bash
|
||
python run_beat.py
|
||
```
|
||
|
||
3. 启动Flower监控
|
||
```bash
|
||
python run_flower.py
|
||
```
|
||
|
||
## Flower监控
|
||
访问 http://localhost:5555 查看监控面板,包含:
|
||
- 📊 实时任务状态
|
||
- 📈 Worker状态和性能
|
||
- ⏰ 定时任务执行历史
|
||
- 🔍 任务执行详情
|
||
- 📋 队列监控
|
||
|
||
## 定时任务配置
|
||
任务会在每天上午9点准时执行,执行时会打印日志信息。
|
||
|
||
## 测试任务
|
||
|
||
### 1. 测试API接口
|
||
```bash
|
||
python test_api.py
|
||
```
|
||
|
||
### 2. 测试重试机制
|
||
```bash
|
||
# 正常重试测试
|
||
python test_retry.py
|
||
|
||
# 模拟失败场景测试
|
||
python simulate_failure.py
|
||
```
|
||
|
||
### 3. 测试HTTP方法
|
||
```bash
|
||
# 测试不同HTTP方法的API调用
|
||
python test_methods.py
|
||
|
||
# 使用呼叫API任务
|
||
from call_api_task import execute_call_api_task
|
||
result = execute_call_api_task.delay() # 调用所有配置的API
|
||
```
|
||
|
||
### 4. 测试Body配置
|
||
```bash
|
||
# 测试不同API配置的body参数
|
||
python test_body_config.py
|
||
|
||
# 使用呼叫API任务(自动遍历所有配置)
|
||
from call_api_task import execute_call_api_task
|
||
result = execute_call_api_task.delay() # 自动调用所有配置的API
|
||
```
|
||
|
||
### 3. 手动触发定时任务
|
||
```python
|
||
from tasks import daily_morning_task
|
||
result = daily_morning_task.delay()
|
||
print(result.get())
|
||
```
|
||
|
||
## 监控功能
|
||
- 实时查看任务执行状态
|
||
- 监控Worker性能指标
|
||
- 查看任务执行历史和日志
|
||
- 管理定时任务
|
||
- 监控队列状态 |