本地启动Flower来监控Dify的Celery任务队列
说明:
- 首次发表日期:2024-08-29
- 参考Dify官方文档:
- https://github.com/langgenius/dify/blob/main/docker/README.md#how-to-deploy-middleware-for-developing-dify
- Celery参考:
- https://docs.celeryq.dev/en/stable/ (官方文档)
- https://www.cloudamqp.com/blog/python-celery-and-rabbitmq.html (关于
--without-heartbeat --without-gossip --without-mingle
)
启动Middleware服务
git clone https://github.com/langgenius/dify.git
cd dify
cd docker
cp middleware.env.example middleware.env
docker compose -f docker-compose.middleware.yaml --profile weaviate -p dify up -d
准备SECRET_KEY:
cd ../api
cp .env.example .env
# Generate a `SECRET_KEY` in the `.env` file.
sed -i "/^SECRET_KEY=/c\SECRET_KEY=$(openssl rand -base64 42)" .env
创建Python环境并安装依赖:
python3 -m venv venv
source venv/bin/activate
pip install poetry
poetry run which python
poetry shell
poetry install
poetry add flower
运行迁移命令使得数据库保持最新:
poetry run python -m flask db upgrade
注释掉api/app.py中的monkey.patch_all(),注释后头部代码如下:
import os
if os.environ.get("DEBUG", "false").lower() != "true":
from gevent import monkey
# monkey.patch_all()
import grpc.experimental.gevent
grpc.experimental.gevent.init_gevent()
如果不关闭monkey patching,flower的页面将无法正常刷新出来,见我在flower库上提出的issue: https://github.com/mher/flower/issues/1390
启动Flask API服务:
poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
启动Web服务:
cd ../web
npm install
npm run dev
启动Celery任务队列:
# source venv/bin/activate
poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
启动Flower:
# source venv/bin/activate
poetry run python -m celery -A app.celery flower --port=5555 --address='0.0.0.0' --debug --enable_events