Skip to content

Commit ba88814

Browse files
authored
Update debugging support for Django and Celery (#1048)
* feat: add debugpy debugging support for Django and Celery Use docker-compose override file pattern for enabling remote debugging with debugpy in VS Code. Supports debugging the main Django app and Celery workers independently or simultaneously. Changes: - Update docker-compose.yml to use /start-celeryworker script - Add docker-compose.override.yml.example to enable debuggers - Update start scripts to look for DEBUGGER environment variable - Add debugger config for main Django app in addition to Celery workers * chore: add pipefail option to celery worker start as well Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: ensure celeryworker doesn't reuse django ports * feat: allow auto reload for celery worker even with debugger * chore: comments & cleanup * chore: rename example file so that linting works * feat: allow debugger to work properly with Django * chore: remove redundant launch config * chore: don't auto reload celery worker * chore: make script volume mount options consistent
1 parent 6caa37b commit ba88814

File tree

8 files changed

+155
-12
lines changed

8 files changed

+155
-12
lines changed

.agents/AGENTS.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ Stop all services:
6666
docker compose down
6767
```
6868

69+
### Debugging with VS Code
70+
71+
Enable remote debugging with debugpy for Django and Celery services:
72+
73+
```bash
74+
# One-time setup: copy the override example file
75+
cp docker-compose.override-example.yml docker-compose.override.yml
76+
77+
# Start services (debugpy will be enabled automatically)
78+
docker compose up
79+
80+
# In VS Code, attach debugger:
81+
# - "Attach: Django" for web server debugging (port 5678)
82+
# - "Attach: Celeryworker" for task debugging (port 5679)
83+
# - "Attach: Django + Celery" for simultaneous debugging
84+
```
85+
86+
**How it works:**
87+
- The `docker-compose.override.yml` file sets `DEBUGGER=1` environment variable
88+
- Start scripts (`compose/local/django/start` and `compose/local/django/celery/worker/start`) detect this and launch with debugpy
89+
- VS Code launch configurations in `.vscode/launch.json` connect to the exposed ports
90+
- The override file is git-ignored for local customization
91+
92+
**Disable debugging:**
93+
```bash
94+
rm docker-compose.override.yml
95+
docker compose restart django celeryworker
96+
```
97+
6998
### Backend (Django)
7099

71100
Run tests:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,8 @@ sandbox/
277277
# Other
278278
flower
279279

280+
# Docker Compose override for local debugging (copy from .example file)
281+
docker-compose.override.yml
282+
280283
# Temporary files in current work session
281284
.agents/todo

.vscode/launch.json

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
22
"version": "0.2.0",
3+
"compounds": [
4+
{
5+
"name": "Attach: Django + Celery",
6+
"configurations": ["Attach: Django", "Attach: Celeryworker"],
7+
"stopAll": true
8+
}
9+
],
310
"configurations": [
411
{
5-
"name": "Python Debugger: Remote Attach",
12+
"name": "Attach: Django",
613
"type": "debugpy",
714
"request": "attach",
815
"connect": {
@@ -12,9 +19,26 @@
1219
"pathMappings": [
1320
{
1421
"localRoot": "${workspaceFolder}",
15-
"remoteRoot": "."
22+
"remoteRoot": "/app"
23+
}
24+
],
25+
"justMyCode": true
26+
},
27+
{
28+
"name": "Attach: Celeryworker",
29+
"type": "debugpy",
30+
"request": "attach",
31+
"connect": {
32+
"host": "localhost",
33+
"port": 5679
34+
},
35+
"pathMappings": [
36+
{
37+
"localRoot": "${workspaceFolder}",
38+
"remoteRoot": "/app"
1639
}
17-
]
40+
],
41+
"justMyCode": true
1842
}
1943
]
2044
}

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,50 @@ The local environment uses a local PostgreSQL database in a Docker container.
258258
### Load fixtures with test data
259259

260260
docker compose run --rm django python manage.py migrate
261+
262+
## Debugging with VS Code
263+
264+
Antenna supports remote debugging with debugpy for both Django and Celery services.
265+
266+
### Setup
267+
268+
1. Copy or link the override example file:
269+
```bash
270+
cp docker-compose.override-example.yml docker-compose.override.yml
271+
# OR
272+
ln -s docker-compose.override-example.yml docker-compose.override.yml
273+
```
274+
275+
2. Start services normally:
276+
```bash
277+
docker compose up
278+
```
279+
280+
3. In VS Code, open the Debug panel (Ctrl+Shift+D) and select one of:
281+
- **Attach: Django** - Debug the Django web server (port 5678)
282+
- **Attach: Celeryworker** - Debug the Celery worker (port 5679)
283+
- **Attach: Django + Celery** - Debug both simultaneously
284+
285+
4. Click the green play button or press F5 to attach the debugger
286+
287+
### Setting Breakpoints
288+
289+
- Set breakpoints in your Python code by clicking in the left margin of the editor
290+
- When the code executes, the debugger will pause at your breakpoints
291+
- Use the Debug Console to inspect variables and execute expressions
292+
293+
### Troubleshooting
294+
295+
- **Connection refused**: Make sure you copied `docker-compose.override-example.yml` to `docker-compose.override.yml`
296+
- **Debugger not stopping**: Verify breakpoints are set in code that actually executes
297+
- **Port conflicts**: Check that ports 5678 and 5679 aren't already in use on your host machine
298+
- **Auto-reload**: Note that auto-reloading is disabled when debugging. You will need to manually restart the services to see code changes.
299+
300+
### Disabling Debug Mode
301+
302+
To disable debugging and return to normal operation:
303+
304+
```bash
305+
rm docker-compose.override.yml
306+
docker compose restart django celeryworker
307+
```

compose/local/django/celery/worker/start

100644100755
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#!/bin/bash
22

33
set -o errexit
4+
set -o pipefail
45
set -o nounset
56

6-
7-
exec watchfiles --filter python celery.__main__.main --args '-A config.celery_app worker -l INFO'
7+
# Launch VS Code debug server if DEBUGGER environment variable is set to 1
8+
# Note that auto reloading is disabled when debugging, manual restart required for code changes.
9+
if [ "${DEBUGGER:-0}" = "1" ]; then
10+
# exec watchfiles --filter python 'python -m debugpy --listen 0.0.0.0:5679 -m celery -A config.celery_app worker -l INFO'
11+
exec python -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5679 -m celery -A config.celery_app worker -l INFO
12+
else
13+
exec watchfiles --filter python celery.__main__.main --args '-A config.celery_app worker -l INFO'
14+
fi

compose/local/django/start

100644100755
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ set -o errexit
44
set -o pipefail
55
set -o nounset
66

7-
87
python manage.py migrate
9-
exec uvicorn config.asgi:application --host 0.0.0.0 --reload --reload-include '*.html'
8+
9+
# Launch VS Code debug server if DEBUGGER environment variable is set to 1
10+
# Note that the --reload flag is not compatible with debugpy, so manually restart the server when code changes
11+
if [ "${DEBUGGER:-0}" = "1" ]; then
12+
exec python -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 -m uvicorn config.asgi:application --host 0.0.0.0
13+
else
14+
exec uvicorn config.asgi:application --host 0.0.0.0 --reload --reload-include '*.html'
15+
fi
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Docker Compose Override File for Local Development
2+
#
3+
# This file can be used to enable debugging and other local development features (db volume location, etc.)
4+
# without affecting the committed docker-compose.yml file.
5+
#
6+
# Setup Instructions:
7+
# 1. Copy this file: cp docker-compose.override-example.yml docker-compose.override.yml
8+
# 2. Start services: docker compose up
9+
# 3. Attach debugger from VS Code using the launch configurations in .vscode/launch.json
10+
#
11+
# The docker-compose.override.yml file is git-ignored for local customization.
12+
13+
services:
14+
django:
15+
environment:
16+
- DEBUGGER=1
17+
ports:
18+
- "5678:5678"
19+
volumes:
20+
# allow modifying the start script without rebuilding the image
21+
- ./compose/local/django/start:/start
22+
23+
celeryworker:
24+
environment:
25+
- DEBUGGER=1
26+
ports:
27+
- "5679:5679"
28+
volumes:
29+
# allow modifying the start script without rebuilding the image
30+
- ./compose/local/django/celery/worker/start:/start-celeryworker

docker-compose.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,8 @@ services:
9090
<<: *django
9191
image: ami_local_celeryworker
9292
scale: 1
93-
# For remote debugging with debugpy, should get overridden for production
94-
# Also make sure to install debugpy in your requirements/local.txt
95-
ports:
96-
- "5678:5678"
97-
command: python -m debugpy --listen 0.0.0.0:5678 -m celery -A config.celery_app worker -l INFO
93+
ports: []
94+
command: /start-celeryworker
9895

9996
celerybeat:
10097
<<: *django

0 commit comments

Comments
 (0)