From 6b3bee66c167a45f06a3867f57b5ce8c082c8e75 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Wed, 5 Oct 2022 09:00:32 -0400 Subject: [PATCH] adding some additional celery debugging options to readme --- README.md | 52 ++++++++++++++++++++++++++++++++ compose/local/fastapi/Dockerfile | 24 +++++++-------- docker-compose.yml | 8 ++--- project/users/tasks.py | 1 + 4 files changed, 69 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 757c5ef..389f0c0 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,55 @@ Use the flower dashboard at: `0.0.0.0:5557` `CELERY_TASK_ALWAYS_EAGER: bool = True` will synchronously execute the celery tasks. This allows us to use things like `breakpoint()` to enter a debugger within the execution context of the task. Since the app currently uses the fastapi application config for celery config, add `CELERY_TASK_ALWAYS_EAGER=True` to the config class if needed. You don't need to run the worker, message broker, or result backend processes to debug your code. It will process the task within + +### Using rdb in Docker Compose environment + +Celery ships with [an extension to pdb called rdb](https://docs.celeryq.dev/en/stable/userguide/debugging.html) that will allow for remote debugging using a tool like `telnet`. + +To use this with your dockerized app you must have `telnet` installed in your container or export the debug port on the container by setting `CELERY_RDB_PORT` in config and adjusting the compose file accordingly. By default the remote debugging is only available at localhost. It is possible to access this remote session on another host using `CELERY_RDB_HOST` also. + +Set the breakpoint like you would with pdb: + +``` +def foo(): + from celery.contrib import rdb + rdb.set_trace() +``` + +Run your dockerized environment + +``` +docker compose -d up +docker compose -f logs +``` + +Trigger the execution path to the break point: + +``` +docker compose exec web bash +python +>> from main import app +>> from project.users.tasks import divide +>> divide.delay(1,2) +``` + +You will see a debug session with connection details displayed in the logs. Now you just need to connect +to the remote session. Since this is running in a docker container that doesn't have the port exposed you +will need to exec into the container and use telenet. + +``` +docker compose exec celery_work bash +telenet 127.0.0.1 +>>(Pdb) +>>(Pdb) help +``` + +It is also possible to set a [signal to enter a remote debugger](https://docs.celeryq.dev/en/stable/userguide/debugging.html#enabling-the-break-point-signal) using by sending a `SIGUSR2`. + +``` +CELERY_RDBSIG=1 celery worker -l INFO +``` + +``` +kill -USR2 +``` diff --git a/compose/local/fastapi/Dockerfile b/compose/local/fastapi/Dockerfile index f8e0886..57db092 100644 --- a/compose/local/fastapi/Dockerfile +++ b/compose/local/fastapi/Dockerfile @@ -22,21 +22,21 @@ COPY ./compose/local/fastapi/entrypoint /entrypoint RUN sed -i 's/\r$//g' /entrypoint RUN chmod +x /entrypoint -COPY ./compose/local/fastapi/start /start-web -RUN sed -i 's/\r$//g' /start-web -RUN chmod +x /start-web +COPY ./compose/local/fastapi/start.sh /start-web.sh +RUN sed -i 's/\r$//g' /start-web.sh +RUN chmod +x /start-web.sh -COPY ./compose/local/fastapi/celery/worker/start /start-celeryworker -RUN sed -i 's/\r$//g' /start-celeryworker -RUN chmod +x /start-celeryworker +COPY ./compose/local/fastapi/celery/worker/start.sh /start-celeryworker.sh +RUN sed -i 's/\r$//g' /start-celeryworker.sh +RUN chmod +x /start-celeryworker.sh -COPY ./compose/local/fastapi/celery/beat/start /start-celerybeat -RUN sed -i 's/\r$//g' /start-celerybeat -RUN chmod +x /start-celerybeat +COPY ./compose/local/fastapi/celery/beat/start.sh /start-celerybeat.sh +RUN sed -i 's/\r$//g' /start-celerybeat.sh +RUN chmod +x /start-celerybeat.sh -COPY ./compose/local/fastapi/celery/flower/start /start-flower -RUN sed -i 's/\r$//g' /start-flower -RUN chmod +x /start-flower +COPY ./compose/local/fastapi/celery/flower/start.sh /start-flower.sh +RUN sed -i 's/\r$//g' /start-flower.sh +RUN chmod +x /start-flower.sh WORKDIR /app diff --git a/docker-compose.yml b/docker-compose.yml index de67f15..6739866 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: dockerfile: ./compose/local/fastapi/Dockerfile image: fastapi_celery_example_web # '/start' is the shell script used to run the service - command: /start-web + command: /start-web.sh # this volume is used to map the files and folders on the host to the container # so if we change code on the host, code in the docker container will also be changed volumes: @@ -37,7 +37,7 @@ services: context: . dockerfile: ./compose/local/fastapi/Dockerfile image: fastapi_celery_example_celery_worker - command: /start-celeryworker + command: /start-celeryworker.sh volumes: - .:/app env_file: @@ -51,7 +51,7 @@ services: context: . dockerfile: ./compose/local/fastapi/Dockerfile image: fastapi_celery_example_celery_beat - command: /start-celerybeat + command: /start-celerybeat.sh volumes: - .:/app env_file: @@ -65,7 +65,7 @@ services: context: . dockerfile: ./compose/local/fastapi/Dockerfile image: fastapi_celery_example_celery_flower - command: /start-flower + command: /start-flower.sh volumes: - .:/app env_file: diff --git a/project/users/tasks.py b/project/users/tasks.py index c1925fb..24b25d7 100644 --- a/project/users/tasks.py +++ b/project/users/tasks.py @@ -9,6 +9,7 @@ from celery import shared_task @shared_task def divide(x, y): + import time time.sleep(5)