Browse Source

Added SIGHUP handler that restarts the worker. (This works now that we no longer have our own --detach). Closes #26

Ask Solem 15 years ago
parent
commit
4eed0eed29
1 changed files with 15 additions and 0 deletions
  1. 15 0
      celery/bin/celeryd.py

+ 15 - 0
celery/bin/celeryd.py

@@ -158,6 +158,10 @@ def run_worker(concurrency=conf.CELERYD_CONCURRENCY,
                                 logfile=logfile,
                                 embed_clockservice=run_clockservice,
                                 send_events=events)
+
+        # Install signal handler so SIGHUP restarts the worker.
+        install_worker_restart_handler(worker)
+
         from celery import signals
         signals.worker_init.send(sender=worker)
 
@@ -174,6 +178,17 @@ def run_worker(concurrency=conf.CELERYD_CONCURRENCY,
         raise
 
 
+def install_worker_restart_handler(worker):
+
+    def restart_worker_sig_handler(signum, frame):
+        """Signal handler restarting the current python program."""
+        worker.logger.warn("Restarting celeryd (%s)" % (
+            " ".join(sys.argv)))
+        worker.stop()
+        os.execv(sys.executable, [sys.executable] + sys.argv)
+
+    platform.install_signal_handler("SIGHUP", restart_worker_sig_handler)
+
 def parse_options(arguments):
     """Parse the available options to ``celeryd``."""
     parser = optparse.OptionParser(option_list=OPTION_LIST)