106 lines
2.2 KiB
Markdown
106 lines
2.2 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 generic_api_task import generic_api_call
|
||
result = generic_api_call.delay('get_api') # 调用GET API
|
||
result = generic_api_call.delay('put_api', {'key': 'value'}) # 调用PUT API
|
||
```
|
||
|
||
### 4. 测试Body配置
|
||
```bash
|
||
# 测试不同API配置的body参数
|
||
python test_body_config.py
|
||
|
||
# 使用自定义body参数
|
||
from generic_api_task import generic_api_call
|
||
custom_body = {'title': '自定义标题', 'content': '自定义内容'}
|
||
result = generic_api_call.delay('main_api', custom_body)
|
||
```
|
||
|
||
### 3. 手动触发定时任务
|
||
```python
|
||
from tasks import daily_morning_task
|
||
result = daily_morning_task.delay()
|
||
print(result.get())
|
||
```
|
||
|
||
## 监控功能
|
||
- 实时查看任务执行状态
|
||
- 监控Worker性能指标
|
||
- 查看任务执行历史和日志
|
||
- 管理定时任务
|
||
- 监控队列状态 |