浏览代码

Generalize: logger -> recorder (an event process recorder will do no logging).

Chris McDonough 18 年之前
父节点
当前提交
5ff765b25d
共有 3 个文件被更改,包括 53 次插入53 次删除
  1. 23 23
      src/supervisor/process.py
  2. 2 2
      src/supervisor/tests/base.py
  3. 28 28
      src/supervisor/tests/test_process.py

+ 23 - 23
src/supervisor/process.py

@@ -61,8 +61,8 @@ class Subprocess:
     pipes = None # mapping of channel name to pipe fd number
     pipes = None # mapping of channel name to pipe fd number
     rpipes = None # mapping of pipe fd number to channel name
     rpipes = None # mapping of pipe fd number to channel name
     dispatchers = None # asnycore output dispatchers (keyed by fd)
     dispatchers = None # asnycore output dispatchers (keyed by fd)
-    stdout_logger = None # Logger instance representing stdout
-    stderr_logger = None # Logger instance representing stderr
+    stdout_recorder = None # object that records stdout output
+    stderr_recorder = None # object that records stderr output
     stdin_buffer = '' # buffer of characters to be sent to child's stdin
     stdin_buffer = '' # buffer of characters to be sent to child's stdin
     exitstatus = None # status attached to dead process by finsh()
     exitstatus = None # status attached to dead process by finsh()
     spawnerr = None # error message attached by spawn() if any
     spawnerr = None # error message attached by spawn() if any
@@ -76,9 +76,8 @@ class Subprocess:
         self.pipes = {}
         self.pipes = {}
         self.rpipes = {}
         self.rpipes = {}
         self.dispatchers = {}
         self.dispatchers = {}
-        self.loggers = {'stdout':None, 'stderr':None}
         if config.stdout_logfile:
         if config.stdout_logfile:
-            self.loggers['stdout'] = Logger(
+            self.stdout_recorder = LoggingRecorder(
                 options = config.options,
                 options = config.options,
                 procname = config.name,
                 procname = config.name,
                 channel = 'stdout',
                 channel = 'stdout',
@@ -87,7 +86,7 @@ class Subprocess:
                 logfile_maxbytes = config.stdout_logfile_maxbytes,
                 logfile_maxbytes = config.stdout_logfile_maxbytes,
                 capturefile = config.stdout_capturefile)
                 capturefile = config.stdout_capturefile)
         if config.stderr_logfile and not config.redirect_stderr:
         if config.stderr_logfile and not config.redirect_stderr:
-            self.loggers['stderr'] = Logger(
+            self.stderr_recorder = LoggingRecorder(
                 options = config.options,
                 options = config.options,
                 procname = config.name,
                 procname = config.name,
                 channel = 'stderr',
                 channel = 'stderr',
@@ -97,24 +96,26 @@ class Subprocess:
                 capturefile = config.stderr_capturefile)
                 capturefile = config.stderr_capturefile)
 
 
     def removelogs(self):
     def removelogs(self):
-        for logger in (self.loggers['stdout'], self.loggers['stderr']):
-            if logger is not None:
-                logger.removelogs()
+        for recorder in self.stdout_recorder, self.stderr_recorder:
+            if recorder is not None:
+                if hasatrr(recorder, 'removelogs'):
+                    recorder.removelogs()
 
 
     def reopenlogs(self):
     def reopenlogs(self):
-        for logger in (self.loggers['stdout'], self.loggers['stderr']):
-            if logger is not None:
-                logger.reopenlogs()
+        for recorder in self.stdout_recorder, self.stderr_recorder:
+            if recorder is not None:
+                if hasattr(recorder, 'reopenlogs'):
+                    recorder.reopenlogs()
 
 
-    def log_output(self):
-        for logger in (self.loggers['stdout'], self.loggers['stderr']):
-            if logger is not None:
-                logger.log_output()
+    def record_output(self):
+        for recorder in self.stdout_recorder, self.stderr_recorder:
+            if recorder is not None:
+                recorder.record_output()
 
 
     def drain_output_fd(self, fd):
     def drain_output_fd(self, fd):
         output = self.config.options.readfd(fd)
         output = self.config.options.readfd(fd)
-        name = self.rpipes[fd]
-        self.loggers[name].output_buffer += output
+        name = '%s_recorder' % self.rpipes[fd]
+        getattr(self, name).output_buffer += output
 
 
     def drain_input_fd(self, fd):
     def drain_input_fd(self, fd):
         if self.stdin_buffer:
         if self.stdin_buffer:
@@ -350,7 +351,7 @@ class Subprocess:
         """ The process was reaped and we need to report and manage its state
         """ The process was reaped and we need to report and manage its state
         """
         """
         self.drain()
         self.drain()
-        self.log_output()
+        self.record_output()
 
 
         es, msg = decode_wait_status(sts)
         es, msg = decode_wait_status(sts)
 
 
@@ -504,7 +505,7 @@ class ProcessGroup:
         now = time.time()
         now = time.time()
 
 
         for proc in self.processes.values():
         for proc in self.processes.values():
-            proc.log_output()
+            proc.record_output()
             state = proc.get_state()
             state = proc.get_state()
 
 
             # we need to transition processes between BACKOFF ->
             # we need to transition processes between BACKOFF ->
@@ -568,9 +569,8 @@ class ProcessGroup:
         for process in self.processes.values():
         for process in self.processes.values():
             dispatchers.update(process.dispatchers)
             dispatchers.update(process.dispatchers)
         return dispatchers
         return dispatchers
-        
 
 
-class Logger:
+class LoggingRecorder:
     options = None # reference to options.ServerOptions instance
     options = None # reference to options.ServerOptions instance
     procname = '' # process name which "owns" this logger
     procname = '' # process name which "owns" this logger
     channel = None # 'stdin' or 'stdout'
     channel = None # 'stdin' or 'stdout'
@@ -614,7 +614,7 @@ class Logger:
                 for handler in log.handlers:
                 for handler in log.handlers:
                     handler.reopen()
                     handler.reopen()
 
 
-    def log_output(self):
+    def record_output(self):
         if self.capturemode:
         if self.capturemode:
             token = ProcessCommunicationEvent.END_TOKEN
             token = ProcessCommunicationEvent.END_TOKEN
         else:
         else:
@@ -650,7 +650,7 @@ class Logger:
             self.options.logger.log(self.options.TRACE, msg)
             self.options.logger.log(self.options.TRACE, msg)
 
 
         if after:
         if after:
-            self.log_output()
+            self.record_output()
 
 
     def toggle_capturemode(self):
     def toggle_capturemode(self):
         self.capturemode = not self.capturemode
         self.capturemode = not self.capturemode

+ 2 - 2
src/supervisor/tests/base.py

@@ -353,7 +353,7 @@ class DummyProcess:
     def readable_fds(self):
     def readable_fds(self):
         return []
         return []
 
 
-    def log_output(self):
+    def record_output(self):
         self.stdout_logged += self.stdout_buffer
         self.stdout_logged += self.stdout_buffer
         self.stdout_buffer = ''
         self.stdout_buffer = ''
 
 
@@ -684,7 +684,7 @@ class PopulatedDummySupervisor(DummySupervisor):
         process = self.process_groups[group_name].processes[process_name]
         process = self.process_groups[group_name].processes[process_name]
         setattr(process, attr_name, val)
         setattr(process, attr_name, val)
 
 
-class DummyProcessLogger:
+class DummyRecorder:
     def __init__(self):
     def __init__(self):
         self.output_buffer = ''
         self.output_buffer = ''
 
 

+ 28 - 28
src/supervisor/tests/test_process.py

@@ -8,7 +8,7 @@ from supervisor.tests.base import DummyOptions
 from supervisor.tests.base import DummyPConfig
 from supervisor.tests.base import DummyPConfig
 from supervisor.tests.base import DummyProcess
 from supervisor.tests.base import DummyProcess
 from supervisor.tests.base import DummyPGroupConfig
 from supervisor.tests.base import DummyPGroupConfig
-from supervisor.tests.base import DummyProcessLogger
+from supervisor.tests.base import DummyRecorder
 
 
 class SubprocessTests(unittest.TestCase):
 class SubprocessTests(unittest.TestCase):
     def _getTargetClass(self):
     def _getTargetClass(self):
@@ -27,10 +27,10 @@ class SubprocessTests(unittest.TestCase):
         self.assertEqual(instance.config, config)
         self.assertEqual(instance.config, config)
         self.assertEqual(instance.config.options, options)
         self.assertEqual(instance.config.options, options)
         self.assertEqual(instance.laststart, 0)
         self.assertEqual(instance.laststart, 0)
-        self.assertEqual(instance.loggers['stdout'].childlog.args, (
+        self.assertEqual(instance.stdout_recorder.childlog.args, (
             ('/tmp/temp123.log', 20, '%(message)s'),
             ('/tmp/temp123.log', 20, '%(message)s'),
             {'rotating': False, 'backups': 0, 'maxbytes': 0}))
             {'rotating': False, 'backups': 0, 'maxbytes': 0}))
-        self.assertEqual(instance.loggers['stderr'].childlog.args, (
+        self.assertEqual(instance.stderr_recorder.childlog.args, (
             ('/tmp/temp456.log', 20, '%(message)s'),
             ('/tmp/temp456.log', 20, '%(message)s'),
             {'rotating': False, 'backups': 0, 'maxbytes': 0}))
             {'rotating': False, 'backups': 0, 'maxbytes': 0}))
         self.assertEqual(instance.pid, 0)
         self.assertEqual(instance.pid, 0)
@@ -42,18 +42,18 @@ class SubprocessTests(unittest.TestCase):
         self.assertEqual(instance.backoff, 0)
         self.assertEqual(instance.backoff, 0)
         self.assertEqual(instance.pipes, {})
         self.assertEqual(instance.pipes, {})
         self.assertEqual(instance.spawnerr, None)
         self.assertEqual(instance.spawnerr, None)
-        self.assertEqual(instance.loggers['stdout'].output_buffer, '')
-        self.assertEqual(instance.loggers['stderr'].output_buffer, '')
+        self.assertEqual(instance.stdout_recorder.output_buffer, '')
+        self.assertEqual(instance.stderr_recorder.output_buffer, '')
 
 
-    def test_log_output_no_loggers(self):
+    def test_record_output_no_recorders(self):
         options = DummyOptions()
         options = DummyOptions()
         config = DummyPConfig(options, 'notthere', '/notthere',
         config = DummyPConfig(options, 'notthere', '/notthere',
                               stdout_logfile=None,
                               stdout_logfile=None,
                               stderr_logfile=None)
                               stderr_logfile=None)
         instance = self._makeOne(config)
         instance = self._makeOne(config)
-        self.assertEqual(instance.loggers['stdout'], None)
-        self.assertEqual(instance.loggers['stderr'], None)
-        instance.log_output()
+        self.assertEqual(instance.stdout_recorder, None)
+        self.assertEqual(instance.stderr_recorder, None)
+        instance.record_output()
         self.assertEqual(options.logger.data, [])
         self.assertEqual(options.logger.data, [])
 
 
     def test_drain(self):
     def test_drain(self):
@@ -71,8 +71,8 @@ class SubprocessTests(unittest.TestCase):
         instance.stdin_buffer = 'foo'
         instance.stdin_buffer = 'foo'
         options.readfd_result = 'abc'
         options.readfd_result = 'abc'
         instance.drain()
         instance.drain()
-        self.assertEqual(instance.loggers['stdout'].output_buffer, 'abc')
-        self.assertEqual(instance.loggers['stderr'].output_buffer, 'abc')
+        self.assertEqual(instance.stdout_recorder.output_buffer, 'abc')
+        self.assertEqual(instance.stderr_recorder.output_buffer, 'abc')
         self.assertEqual(options.written[3], 'foo')
         self.assertEqual(options.written[3], 'foo')
         
         
     def test_get_execv_args_abs_missing(self):
     def test_get_execv_args_abs_missing(self):
@@ -569,9 +569,9 @@ class SubprocessTests(unittest.TestCase):
 
 
         try:
         try:
             instance = self._makeOne(config)
             instance = self._makeOne(config)
-            instance.loggers['stdout'].output_buffer = ansi
-            instance.log_output()
-            [ x.flush() for x in instance.loggers['stdout'].childlog.handlers ]
+            instance.stdout_recorder.output_buffer = ansi
+            instance.record_output()
+            [ x.flush() for x in instance.stdout_recorder.childlog.handlers ]
             self.assertEqual(
             self.assertEqual(
                 open(instance.config.stdout_logfile, 'r').read(), noansi)
                 open(instance.config.stdout_logfile, 'r').read(), noansi)
         finally:
         finally:
@@ -583,9 +583,9 @@ class SubprocessTests(unittest.TestCase):
         try:
         try:
             options.strip_ansi = False
             options.strip_ansi = False
             instance = self._makeOne(config)
             instance = self._makeOne(config)
-            instance.loggers['stdout'].output_buffer = ansi
-            instance.log_output()
-            [ x.flush() for x in instance.loggers['stdout'].childlog.handlers ]
+            instance.stdout_recorder.output_buffer = ansi
+            instance.record_output()
+            [ x.flush() for x in instance.stdout_recorder.childlog.handlers ]
             self.assertEqual(
             self.assertEqual(
                 open(instance.config.stdout_logfile, 'r').read(), ansi)
                 open(instance.config.stdout_logfile, 'r').read(), ansi)
         finally:
         finally:
@@ -600,11 +600,11 @@ class SubprocessTests(unittest.TestCase):
         instance = self._makeOne(config)
         instance = self._makeOne(config)
         instance.rpipes[0] = 'stdout'
         instance.rpipes[0] = 'stdout'
         instance.pipes['stdout'] = 0
         instance.pipes['stdout'] = 0
-        logger = DummyProcessLogger()
-        instance.loggers['stdout'] = logger
+        recorder = DummyRecorder()
+        instance.stdout_recorder = recorder
         options.readfd_result = 'hello'
         options.readfd_result = 'hello'
         instance.drain_output_fd(0)
         instance.drain_output_fd(0)
-        self.assertEqual(logger.output_buffer, 'hello')
+        self.assertEqual(recorder.output_buffer, 'hello')
 
 
     def test_drain_input_fd(self):
     def test_drain_input_fd(self):
         options = DummyOptions()
         options = DummyOptions()
@@ -852,10 +852,10 @@ class ProcessGroupTests(unittest.TestCase):
         self.assertEqual(result, {4:None, 5:None})
         self.assertEqual(result, {4:None, 5:None})
         
         
 
 
-class LoggerTests(unittest.TestCase):
+class LoggingRecorderTests(unittest.TestCase):
     def _getTargetClass(self):
     def _getTargetClass(self):
-        from supervisor.process import Logger
-        return Logger
+        from supervisor.process import LoggingRecorder
+        return LoggingRecorder
 
 
     def _makeOne(self, options, procname, channel, logfile, logfile_backups,
     def _makeOne(self, options, procname, channel, logfile, logfile_backups,
                  logfile_maxbytes, capturefile):
                  logfile_maxbytes, capturefile):
@@ -895,13 +895,13 @@ class LoggerTests(unittest.TestCase):
         instance.reopenlogs()
         instance.reopenlogs()
         self.assertEqual(instance.childlog.handlers[0].reopened, True)
         self.assertEqual(instance.childlog.handlers[0].reopened, True)
 
 
-    def test_log_output(self):
+    def test_record_output(self):
         # stdout/stderr goes to the process log and the main log
         # stdout/stderr goes to the process log and the main log
         options = DummyOptions()
         options = DummyOptions()
         instance = self._makeOne(options, 'whatever', 'stdout',
         instance = self._makeOne(options, 'whatever', 'stdout',
                                  '/tmp/log', None, 100, '/tmp/capture')
                                  '/tmp/log', None, 100, '/tmp/capture')
         instance.output_buffer = 'stdout string longer than a token'
         instance.output_buffer = 'stdout string longer than a token'
-        instance.log_output()
+        instance.record_output()
         self.assertEqual(instance.childlog.data,
         self.assertEqual(instance.childlog.data,
                          ['stdout string longer than a token'])
                          ['stdout string longer than a token'])
         self.assertEqual(options.logger.data[0], 5)
         self.assertEqual(options.logger.data[0], 5)
@@ -939,14 +939,14 @@ class LoggerTests(unittest.TestCase):
 
 
         try:
         try:
             instance.output_buffer = first
             instance.output_buffer = first
-            instance.log_output()
+            instance.record_output()
             [ x.flush() for x in instance.childlog.handlers]
             [ x.flush() for x in instance.childlog.handlers]
             self.assertEqual(open(logfile, 'r').read(), letters)
             self.assertEqual(open(logfile, 'r').read(), letters)
             self.assertEqual(instance.output_buffer, first[len(letters):])
             self.assertEqual(instance.output_buffer, first[len(letters):])
             self.assertEqual(len(events), 0)
             self.assertEqual(len(events), 0)
 
 
             instance.output_buffer += second
             instance.output_buffer += second
-            instance.log_output()
+            instance.record_output()
             self.assertEqual(len(events), 0)
             self.assertEqual(len(events), 0)
             [ x.flush() for x in instance.childlog.handlers]
             [ x.flush() for x in instance.childlog.handlers]
             self.assertEqual(open(logfile, 'r').read(), letters)
             self.assertEqual(open(logfile, 'r').read(), letters)
@@ -954,7 +954,7 @@ class LoggerTests(unittest.TestCase):
             self.assertEqual(len(events), 0)
             self.assertEqual(len(events), 0)
 
 
             instance.output_buffer += third
             instance.output_buffer += third
-            instance.log_output()
+            instance.record_output()
             [ x.flush() for x in instance.childlog.handlers]
             [ x.flush() for x in instance.childlog.handlers]
             self.assertEqual(open(logfile, 'r').read(), letters *2)
             self.assertEqual(open(logfile, 'r').read(), letters *2)
             self.assertEqual(len(events), 1)
             self.assertEqual(len(events), 1)