Browse Source

Adds SIGUSR2 as a breakpoint signal if CELERY_RDBSIG is set

Ask Solem 14 years ago
parent
commit
d7e5e7a7dc
4 changed files with 41 additions and 5 deletions
  1. 12 0
      celery/apps/worker.py
  2. 5 3
      celery/contrib/rdb.py
  3. 1 2
      celery/worker/__init__.py
  4. 23 0
      docs/tutorials/debugging.rst

+ 12 - 0
celery/apps/worker.py

@@ -242,6 +242,7 @@ class Worker(object):
         install_worker_term_handler(worker)
         install_worker_int_handler(worker)
         install_cry_handler(worker.logger)
+        install_rdb_handler()
         signals.worker_init.send(sender=worker)
 
     def osx_proxy_detection_workaround(self):
@@ -325,6 +326,17 @@ def install_cry_handler(logger):
         platforms.install_signal_handler("SIGUSR1", cry_handler)
 
 
+def install_rdb_handler():  # pragma: no cover
+
+    def rdb_handler(signum, frame):
+        """Signal handler setting a rdb breakpoint at the current frame."""
+        from celery.contrib import rdb
+        rdb.set_trace(frame)
+
+    if os.environ.get("CELERY_RDBSIG"):
+        platforms.install_signal_handler("SIGUSR2", rdb_handler)
+
+
 def install_HUP_not_supported_handler(worker):
 
     def warn_on_HUP_handler(signum, frame):

+ 5 - 3
celery/contrib/rdb.py

@@ -150,6 +150,8 @@ def debugger():
     return rdb
 
 
-def set_trace():
-    """Set breakpoint at current location."""
-    return debugger().set_trace(_frame().f_back)
+def set_trace(frame=None):
+    """Set breakpoint at current location, or a specified frame"""
+    if frame is None:
+        frame = _frame().f_back
+    return debugger().set_trace(frame)

+ 1 - 2
celery/worker/__init__.py

@@ -25,8 +25,7 @@ WORKER_SIGRESET = frozenset(["SIGTERM",
                              "SIGHUP",
                              "SIGTTIN",
                              "SIGTTOU",
-                             "SIGUSR1",
-                             "SIGUSR2"])
+                             "SIGUSR1"])
 
 #: List of signals to ignore when a child process starts.
 WORKER_SIGIGNORE = frozenset(["SIGINT"])

+ 23 - 0
docs/tutorials/debugging.rst

@@ -4,6 +4,9 @@
  Debugging Tasks Remotely (using pdb)
 ======================================
 
+Basics
+======
+
 :mod:`celery.contrib.rdb` is an extended version of :mod:`pdb` that
 enables remote debugging of processes that does not have terminal
 access.
@@ -76,3 +79,23 @@ The result of our vandalism can be seen in the worker logs::
         in 61.481s: 'hello from rdb'
 
 .. _`Python Debugger Manual`: http://docs.python.org/library/pdb.html
+
+
+Tips
+====
+
+
+Enabling the breakpoint signal
+------------------------------
+
+If the environment variable :envvar:`CELERY_RDBSIG` is set, the worker
+will open up an rdb instance whenever the `SIGUSR2` signal is sent.
+This is the case for both main and worker processes.
+
+For example starting the worker with::
+
+    CELERY_RDBSIG=1 celeryd -l info
+
+You can start an rdb session for any of the worker processes by executing::
+
+    kill -USR2 <pid>