本地啟動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