Kaynağa Gözat

celeryd: Do not install HUP handler if running from a terminal.

This fixes the problem where celeryd is launched in the background
when closing the terminal.
Ask Solem 14 yıl önce
ebeveyn
işleme
7a7c44e393
2 değiştirilmiş dosya ile 17 ekleme ve 4 silme
  1. 6 2
      celery/bin/celeryd.py
  2. 11 2
      celery/tests/test_bin/test_celeryd.py

+ 6 - 2
celery/bin/celeryd.py

@@ -188,6 +188,7 @@ class Worker(object):
         self.max_tasks_per_child = max_tasks_per_child
         self.db = db
         self.queues = queues or []
+        self._isatty = sys.stdout.isatty()
 
         if isinstance(self.queues, basestring):
             self.queues = self.queues.split(",")
@@ -304,10 +305,13 @@ class Worker(object):
                                 task_soft_time_limit=self.task_soft_time_limit)
 
         # Install signal handler so SIGHUP restarts the worker.
-        install_worker_restart_handler(worker)
+        if not self._isatty:
+            # only install HUP handler if detached from terminal,
+            # so closing the terminal window doesn't restart celeryd
+            # into the background.
+            install_worker_restart_handler(worker)
         install_worker_term_handler(worker)
         install_worker_int_handler(worker)
-
         signals.worker_init.send(sender=worker)
         worker.start()
 

+ 11 - 2
celery/tests/test_bin/test_celeryd.py

@@ -60,7 +60,6 @@ class test_Worker(unittest.TestCase):
         worker = self.Worker(loglevel="INFO")
         self.assertEqual(worker.loglevel, logging.INFO)
 
-    @disable_stdouts
     def test_run_worker(self):
         handlers = {}
 
@@ -70,9 +69,19 @@ class test_Worker(unittest.TestCase):
         p = platform.install_signal_handler
         platform.install_signal_handler = i
         try:
-            self.Worker().run_worker()
+            w = self.Worker()
+            w._isatty = False
+            w.run_worker()
             for sig in "SIGINT", "SIGHUP", "SIGTERM":
                 self.assertIn(sig, handlers)
+
+            handlers.clear()
+            w = self.Worker()
+            w._isatty = True
+            w.run_worker()
+            for sig in "SIGINT", "SIGTERM":
+                self.assertIn(sig, handlers)
+            self.assertNotIn("SIGHUP", handlers)
         finally:
             platform.install_signal_handler = p