diff --git a/README.md b/README.md index 82e0679..c3afb70 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,18 @@ # FastAPI and Celery Learning distributed task queues by doing. Since it's a greenfield project this also uses a newer async web framework. + +## Debugging Celery + +### Check results in redis result backend + +``` +docker-compose exec redis sh +redis-cli +KEYS celery* +MGET celery-task-meta- +``` + +### Checking results in Flower + +`0.0.0.0: diff --git a/compose/local/fastapi/celery/beat/start b/compose/local/fastapi/celery/beat/start index 30dc735..aeaeace 100644 --- a/compose/local/fastapi/celery/beat/start +++ b/compose/local/fastapi/celery/beat/start @@ -4,4 +4,4 @@ set -o errexit set -o nounset rm -f './celerybeat.pid' -celery -A main.celery beat -l info +exec python -m celery -A main.celery beat -l info diff --git a/compose/local/fastapi/celery/flower/start b/compose/local/fastapi/celery/flower/start index c8c97e4..2a56886 100644 --- a/compose/local/fastapi/celery/flower/start +++ b/compose/local/fastapi/celery/flower/start @@ -13,6 +13,6 @@ until worker_ready; do done >&2 echo 'Celery workers is available' -celery flower \ +exec celery flower \ --app=main.celery \ --broker="${CELERY_BROKER_URL}" diff --git a/compose/local/fastapi/celery/worker/start b/compose/local/fastapi/celery/worker/start index 9dad2ed..fed8d71 100644 --- a/compose/local/fastapi/celery/worker/start +++ b/compose/local/fastapi/celery/worker/start @@ -3,4 +3,11 @@ set -o errexit set -o nounset -celery -A main.celery worker --loglevel=info +# Use watchfiles during development +CELERY_WORKER_WATCH=${CELERY_WORKER_WATCH:-} + +if [ ! -z $CELERY_WORKER_WATCH ]; then + watchfiles --filter python 'celery -A main.celery worker --loglevel=info' +else + exec python -m celery -A main.celery worker --loglevel=info +fi diff --git a/project/users/tasks.py b/project/users/tasks.py index 60e7ecf..c1925fb 100644 --- a/project/users/tasks.py +++ b/project/users/tasks.py @@ -13,3 +13,11 @@ def divide(x, y): time.sleep(5) return x / y + + +@shared_task +def add(x, y): + import time + + time.sleep(5) + return x + y diff --git a/requirements.in b/requirements.in index 023e58b..41cc19d 100644 --- a/requirements.in +++ b/requirements.in @@ -6,3 +6,4 @@ psycopg2-binary==2.9.3 redis==4.3.4 SQLAlchemy==1.4.40 uvicorn[standard]==0.18.2 +watchfiles==0.17.0 diff --git a/requirements.txt b/requirements.txt index 5ca8432..c50b60c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -107,7 +107,9 @@ vine==5.0.0 # celery # kombu watchfiles==0.17.0 - # via uvicorn + # via + # -r requirements.in + # uvicorn wcwidth==0.2.5 # via prompt-toolkit websockets==10.3