Ver Fonte

Extends 'pid' supervisorctl command to retreive child's pid

- Backward compatible with previous behaviour (without argument)
- Display one pid per line
- Support of "pid all" syntax
Grégory há 14 anos atrás
pai
commit
fea0db27f4

+ 21 - 3
src/supervisor/supervisorctl.py

@@ -282,7 +282,7 @@ class Controller(cmd.Cmd):
             return results[state]
         else:
             exp = line.split()[0]
-            if exp in ['start','stop','restart','clear','status','tail','fg']:
+            if exp in ['start','stop','restart','clear','status','tail','fg','pid']:
                 if not line.endswith(' ') and len(line.split()) == 1:
                     return [text + ' ', None][state]
                 if exp == 'fg':
@@ -604,8 +604,26 @@ class DefaultControllerPlugin(ControllerPluginBase):
 
     def do_pid(self, arg):
         supervisor = self.ctl.get_supervisor()
-        pid = supervisor.getPID()
-        self.ctl.output(str(pid))
+        if not self.ctl.upcheck():
+            return
+        names = arg.strip().split()
+        if not names:
+            pid = supervisor.getPID()
+            self.ctl.output(str(pid))
+        elif 'all' in names:
+            for info in supervisor.getAllProcessInfo():
+                self.ctl.output(str(info['pid']))
+        else:
+            for name in names:
+                try:
+                    info = supervisor.getProcessInfo(name)
+                except xmlrpclib.Fault, e:
+                    if e.faultCode == xmlrpc.Faults.BAD_NAME:
+                        self.ctl.output('No such process %s' % name)
+                    else:
+                        raise
+                    continue
+                self.ctl.output(str(info['pid']))
 
     def help_pid(self):
         self.ctl.output("pid\t\t\tGet the PID of supervisord.")    

+ 20 - 1
src/supervisor/tests/test_supervisorctl.py

@@ -832,7 +832,7 @@ class TestDefaultControllerPlugin(unittest.TestCase):
         plugin.do_update('')
         self.assertEqual(supervisor.processes, ['removed_group'])
 
-    def test_pid(self):
+    def test_pid_supervisord(self):
         plugin = self._makeOne()
         result = plugin.do_pid('')
         options = plugin.ctl.options
@@ -841,6 +841,25 @@ class TestDefaultControllerPlugin(unittest.TestCase):
         self.assertEqual(len(lines), 2)
         self.assertEqual(lines[0], str(options._server.supervisor.getPID()))
 
+    def test_pid_allprocesses(self):
+        plugin = self._makeOne()
+        result = plugin.do_pid('all')
+        self.assertEqual(result, None)
+        value = plugin.ctl.stdout.getvalue().strip()
+        self.assertEqual(value.split(), ['11', '12', '13'])
+
+    def test_pid_badname(self):
+        plugin = self._makeOne()
+        result = plugin.do_pid('BAD_NAME')
+        self.assertEqual(result, None)
+        value = plugin.ctl.stdout.getvalue().strip()
+        self.assertEqual(value, 'No such process BAD_NAME')
+
+    def test_pid_oneprocess(self):
+        plugin = self._makeOne()
+        result = plugin.do_pid('foo')
+        self.assertEqual(plugin.ctl.stdout.getvalue().strip(), '11')
+
     def test_maintail_toomanyargs(self):
         plugin = self._makeOne()
         result = plugin.do_maintail('foo bar')