| 1 |
.. _guide-debugging: |
| 2 |
|
| 3 |
====================================== |
| 4 |
Debugging |
| 5 |
====================================== |
| 6 |
|
| 7 |
.. _tut-remote_debug: |
| 8 |
|
| 9 |
Debugging Tasks Remotely (using pdb) |
| 10 |
==================================== |
| 11 |
|
| 12 |
Basics |
| 13 |
------ |
| 14 |
|
| 15 |
:mod:`celery.contrib.rdb` is an extended version of :mod:`pdb` that |
| 16 |
enables remote debugging of processes that doesn't have terminal |
| 17 |
access. |
| 18 |
|
| 19 |
Example usage: |
| 20 |
|
| 21 |
.. code-block:: python |
| 22 |
|
| 23 |
from celery import task |
| 24 |
from celery.contrib import rdb |
| 25 |
|
| 26 |
@task() |
| 27 |
def add(x, y): |
| 28 |
result = x + y |
| 29 |
rdb.set_trace() # <- set break-point |
| 30 |
return result |
| 31 |
|
| 32 |
|
| 33 |
:func:`~celery.contrib.rdb.set_trace` sets a break-point at the current |
| 34 |
location and creates a socket you can telnet into to remotely debug |
| 35 |
your task. |
| 36 |
|
| 37 |
The debugger may be started by multiple processes at the same time, |
| 38 |
so rather than using a fixed port the debugger will search for an |
| 39 |
available port, starting from the base port (6900 by default). |
| 40 |
The base port can be changed using the environment variable |
| 41 |
:envvar:`CELERY_RDB_PORT`. |
| 42 |
|
| 43 |
By default the debugger will only be available from the local host, |
| 44 |
to enable access from the outside you have to set the environment |
| 45 |
variable :envvar:`CELERY_RDB_HOST`. |
| 46 |
|
| 47 |
When the worker encounters your break-point it'll log the following |
| 48 |
information: |
| 49 |
|
| 50 |
.. code-block:: text |
| 51 |
|
| 52 |
[INFO/MainProcess] Received task: |
| 53 |
tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8] |
| 54 |
[WARNING/PoolWorker-1] Remote Debugger:6900: |
| 55 |
Please telnet 127.0.0.1 6900. Type `exit` in session to continue. |
| 56 |
[2011-01-18 14:25:44,119: WARNING/PoolWorker-1] Remote Debugger:6900: |
| 57 |
Waiting for client... |
| 58 |
|
| 59 |
If you telnet the port specified you'll be presented |
| 60 |
with a `pdb` shell: |
| 61 |
|
| 62 |
.. code-block:: console |
| 63 |
|
| 64 |
$ telnet localhost 6900 |
| 65 |
Connected to localhost. |
| 66 |
Escape character is '^]'. |
| 67 |
> /opt/devel/demoapp/tasks.py(128)add() |
| 68 |
-> return result |
| 69 |
(Pdb) |
| 70 |
|
| 71 |
Enter ``help`` to get a list of available commands, |
| 72 |
It may be a good idea to read the `Python Debugger Manual`_ if |
| 73 |
you have never used `pdb` before. |
| 74 |
|
| 75 |
To demonstrate, we'll read the value of the ``result`` variable, |
| 76 |
change it and continue execution of the task: |
| 77 |
|
| 78 |
.. code-block:: text |
| 79 |
|
| 80 |
(Pdb) result |
| 81 |
4 |
| 82 |
(Pdb) result = 'hello from rdb' |
| 83 |
(Pdb) continue |
| 84 |
Connection closed by foreign host. |
| 85 |
|
| 86 |
The result of our vandalism can be seen in the worker logs: |
| 87 |
|
| 88 |
.. code-block:: text |
| 89 |
|
| 90 |
[2011-01-18 14:35:36,599: INFO/MainProcess] Task |
| 91 |
tasks.add[d7261c71-4962-47e5-b342-2448bedd20e8] succeeded |
| 92 |
in 61.481s: 'hello from rdb' |
| 93 |
|
| 94 |
.. _`Python Debugger Manual`: http://docs.python.org/library/pdb.html |
| 95 |
|
| 96 |
|
| 97 |
Tips |
| 98 |
---- |
| 99 |
|
| 100 |
.. _breakpoint_signal: |
| 101 |
|
| 102 |
Enabling the break-point signal |
| 103 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 104 |
|
| 105 |
If the environment variable :envvar:`CELERY_RDBSIG` is set, the worker |
| 106 |
will open up an rdb instance whenever the `SIGUSR2` signal is sent. |
| 107 |
This is the case for both main and worker processes. |
| 108 |
|
| 109 |
For example starting the worker with: |
| 110 |
|
| 111 |
.. code-block:: console |
| 112 |
|
| 113 |
$ CELERY_RDBSIG=1 celery worker -l INFO |
| 114 |
|
| 115 |
You can start an rdb session for any of the worker processes by executing: |
| 116 |
|
| 117 |
.. code-block:: console |
| 118 |
|
| 119 |
$ kill -USR2 <pid> |