Skip to content

Commit dda61c6

Browse files
authored
Hotfix: Added debugpy support (#268)
* Hotfix: Added debugpy support * Docs
1 parent 5b532a3 commit dda61c6

File tree

16 files changed

+120
-16
lines changed

16 files changed

+120
-16
lines changed

docs/devguide/local-development-environment.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ To install the development dependencies, run the following command in your desir
4545
pip install -U pip ipython;\
4646
poetry install -E "all" --with test,dev,ci,docs
4747

48+
.. _vscode:
49+
4850
Debugging with VSCode
4951
=====================
5052

@@ -64,7 +66,7 @@ Create a ``.vscode`` directory with the following ``launch.json`` file that can
6466
"configurations": [
6567
{
6668
"name": "Python: Debug Tests",
67-
"type": "python",
69+
"type": "debugpy",
6870
"request": "launch",
6971
"program": "${file}",
7072
"purpose": [
@@ -84,6 +86,21 @@ Create a ``.vscode`` directory with the following ``launch.json`` file that can
8486
"stopOnEntry": false,
8587
"showReturnValue": true,
8688
"redirectOutput": true
89+
},
90+
{
91+
"name": "Attach to Celery Worker",
92+
"type": "debugpy",
93+
"request": "attach",
94+
"connect": {
95+
"host": "localhost",
96+
"port": 5678
97+
},
98+
"pathMappings": [
99+
{
100+
"localRoot": "${workspaceFolder}",
101+
"remoteRoot": "path-to-celery-source-in-container"
102+
}
103+
]
87104
}
88105
]
89106
}

docs/getting-started/next-steps.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,13 @@ And build our container using the standard `pytest-docker-tools <https://pypi.or
258258
259259
myworker_container = container(
260260
image="{myworker_image.id}",
261+
ports=MyWorkerContainer.ports(),
261262
environment=fxtr("default_worker_env"),
262263
network="{default_pytest_celery_network.name}",
263264
volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME},
264265
wrapper_class=MyWorkerContainer,
265266
timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT,
267+
command=MyWorkerContainer.command(),
266268
)
267269
268270
Pytest Integration

docs/userguide/celery-bug-report.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ We'll use the smoke tests worker to run the worker from the source code.
361361
from t.smoke.workers.dev import SmokeWorkerContainer
362362
363363
from pytest_celery import RABBITMQ_PORTS
364+
from pytest_celery import WORKER_DEBUGPY_PORTS
364365
from pytest_celery import CeleryBackendCluster
365366
from pytest_celery import CeleryBrokerCluster
366367
from pytest_celery import CeleryTestSetup
@@ -448,8 +449,14 @@ We'll use the smoke tests worker to run the worker from the source code.
448449
"--without-gossip",
449450
"--without-mingle",
450451
"--without-heartbeat",
452+
debugpy=False,
453+
wait_for_client=True,
451454
)
452455
456+
@classmethod
457+
def ports(cls) -> dict | None:
458+
return WORKER_DEBUGPY_PORTS
459+
453460
454461
@pytest.fixture
455462
def default_worker_container_cls() -> type[SmokeWorkerContainer]:

docs/userguide/examples/django.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ These will be used to configure the worker for the tests.
7272
ENV WORKER_NAME=$CELERY_WORKER_NAME
7373
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE
7474
75+
EXPOSE 5678
76+
7577
``/src`` is arbitrarily chosen as the working directory to install the Django project from.
7678

7779
.. code-block:: docker
@@ -133,6 +135,7 @@ and `container <https://github.com/Jc2k/pytest-docker-tools?tab=readme-ov-file#c
133135
134136
default_worker_container = container(
135137
image="{worker_image.id}",
138+
ports=fxtr("default_worker_ports"),
136139
environment=fxtr("default_worker_env"),
137140
network="{default_pytest_celery_network.name}",
138141
volumes={

docs/userguide/examples/myworker.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ These will be used to configure the worker for the tests.
7373
ENV WORKER_NAME=$CELERY_WORKER_NAME
7474
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE
7575
76+
EXPOSE 5678
77+
7678
``/src`` is arbitrarily chosen as the working directory to install Celery from.
7779

7880
.. code-block:: docker
@@ -136,6 +138,22 @@ to the container instance.
136138
def worker_queue(cls) -> str:
137139
return "myworker"
138140
141+
.. tip::
142+
143+
Add the following implementation to enable debugpy for the worker container.
144+
145+
.. code-block:: python
146+
147+
@classmethod
148+
def ports(cls) -> dict | None:
149+
return WORKER_DEBUGPY_PORTS
150+
151+
@classmethod
152+
def command(cls, *args: str, **kwargs: dict) -> list[str]:
153+
return super().command(*args, debugpy=True, wait_for_client=True, **kwargs)
154+
155+
The ``WORKER_DEBUGPY_PORTS`` can be imported from the plugin.
156+
139157
Next, we build our worker image using the `build <https://github.com/Jc2k/pytest-docker-tools?tab=readme-ov-file#images>`_
140158
and `container <https://github.com/Jc2k/pytest-docker-tools?tab=readme-ov-file#containers>`_ fixtures.
141159

@@ -156,11 +174,13 @@ These fixtures may be overridden if required.
156174
157175
myworker_container = container(
158176
image="{myworker_image.id}",
177+
ports=MyWorkerContainer.ports(),
159178
environment=fxtr("default_worker_env"),
160179
network="{default_pytest_celery_network.name}",
161180
volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME},
162181
wrapper_class=MyWorkerContainer,
163182
timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT,
183+
command=MyWorkerContainer.command(),
164184
)
165185
166186
Lastly, we wrap the container in a fixture to allow it to be injected into the test environment

docs/userguide/examples/range.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Our focus should be on the ``worker_containers`` list, which will contain the na
144144
cnt = f"worker_v{v.replace('.', '_')}_container"
145145
globals()[cnt] = container(
146146
image="{" + f"{img}.id" + "}",
147+
ports=fxtr("default_worker_ports"),
147148
environment=fxtr("default_worker_env"),
148149
network="{default_pytest_celery_network.name}",
149150
volumes={"{default_worker_volume.name}": DEFAULT_WORKER_VOLUME},

examples/django/tests/DjangoWorker.Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ENV LOG_LEVEL=$CELERY_LOG_LEVEL
1414
ENV WORKER_NAME=$CELERY_WORKER_NAME
1515
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE
1616

17+
EXPOSE 5678
18+
1719
# Install packages
1820
WORKDIR /src
1921

examples/django/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def worker_queue(cls) -> str:
4545

4646
default_worker_container = container(
4747
image="{worker_image.id}",
48+
ports=fxtr("default_worker_ports"),
4849
environment=fxtr("default_worker_env"),
4950
network="{default_pytest_celery_network.name}",
5051
volumes={

examples/myworker/tests/myworker/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ENV LOG_LEVEL=$CELERY_LOG_LEVEL
1414
ENV WORKER_NAME=$CELERY_WORKER_NAME
1515
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE
1616

17+
EXPOSE 5678
18+
1719
# Install packages
1820
WORKDIR /src
1921

examples/myworker/tests/myworker/myworker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ def worker_queue(cls) -> str:
4545

4646
myworker_container = container(
4747
image="{myworker_image.id}",
48+
ports=MyWorkerContainer.ports(),
4849
environment=fxtr("default_worker_env"),
4950
network="{default_pytest_celery_network.name}",
5051
volumes={"{default_worker_volume.name}": defaults.DEFAULT_WORKER_VOLUME},
5152
wrapper_class=MyWorkerContainer,
5253
timeout=defaults.DEFAULT_WORKER_CONTAINER_TIMEOUT,
54+
command=MyWorkerContainer.command(),
5355
)
5456

5557

0 commit comments

Comments
 (0)