ソースを参照

Added the ability to retrieve supervisord's own pid through
supervisor.getPID() on the XML-RPC interface or a new
"pid" command on supervisorctl.

Mike Naberezny 17 年 前
コミット
5393635387

+ 6 - 0
CHANGES.txt

@@ -1,3 +1,9 @@
+Next Release
+
+  - Added the ability to retrieve supervisord's own pid through
+    supervisor.getPID() on the XML-RPC interface or a new
+    "pid" command on supervisorctl.
+
 3.0a5
 
   - Supervisorctl now supports persistent readline history.  To

+ 0 - 4
TODO.txt

@@ -61,7 +61,3 @@
 - Unit tests for log rotation.
 
 - Command-line arg tests.
-
-- Provide a way to get the supervisord pid from supervisorctl/web interface.
-
-

+ 3 - 0
src/supervisor/options.py

@@ -1191,6 +1191,9 @@ class ServerOptions(Options):
                 path = p.split(os.pathsep)
         return path
 
+    def get_pid(self):
+        return os.getpid()
+
     def check_execv_args(self, filename, argv, st):
         if st is None:
             raise NotFound("can't find command %r" % filename)

+ 8 - 0
src/supervisor/rpcinterface.py

@@ -90,6 +90,14 @@ class SupervisorNamespaceRPCInterface:
             }
         return data
 
+    def getPID(self):
+        """ Return the PID of supervisord
+        
+        @return int PID
+        """
+        self._update('getPID')
+        return self.supervisord.options.get_pid()
+
     def readLog(self, offset, length):
         """ Read length bytes from the main log starting at offset
 

+ 8 - 0
src/supervisor/supervisorctl.py

@@ -372,6 +372,14 @@ class Controller(cmd.Cmd):
         self._output("status <name> <name>\tGet status on multiple named "
                      "processes.")
 
+    def do_pid(self, arg):
+        supervisor = self._get_supervisor()
+        pid = supervisor.getPID()
+        self._output(str(pid))
+
+    def help_pid(self):
+        self._output("pid\t\t\tGet the PID of supervisord.")    
+
     def _startresult(self, result):
         name = result['name']
         code = result['status']

+ 7 - 0
src/supervisor/tests/base.py

@@ -132,6 +132,10 @@ class DummyOptions:
     def get_path(self):
         return ["/bin", "/usr/bin", "/usr/local/bin"]
 
+    def get_pid(self):
+        import os
+        return os.getpid()
+        
     def check_execv_args(self, filename, argv, st):
         if filename == '/bad/filename':
             from supervisor.options import NotFound
@@ -633,6 +637,9 @@ class DummySupervisorRPCNamespace:
 
     getVersion = getAPIVersion # deprecated
 
+    def getPID(self):
+        return 42
+
     def readProcessStdoutLog(self, name, offset, length):
         from supervisor import xmlrpc
         import xmlrpclib

+ 4 - 0
src/supervisor/tests/test_options.py

@@ -278,6 +278,10 @@ class ServerOptionsTests(unittest.TestCase):
         else:
             raise AssertionError("Didn't raise")
 
+    def test_get_pid(self):
+        instance = self._makeOne()
+        self.assertEqual(os.getpid(), instance.get_pid())
+
     def test_check_execv_args_cant_find_command(self):
         instance = self._makeOne()
         from supervisor.options import NotFound

+ 7 - 0
src/supervisor/tests/test_rpcinterfaces.py

@@ -117,6 +117,13 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         self.assertEqual(stateinfo['statename'], statename)
         self.assertEqual(interface.update_text, 'getState')
 
+    def test_getPID(self):
+        options = DummyOptions()
+        supervisord = DummySupervisor(options)
+        interface = self._makeOne(supervisord)
+        self.assertEqual(interface.getPID(), options.get_pid())
+        self.assertEqual(interface.update_text, 'getPID')
+
     def test_readLog_aliased_to_deprecated_readMainLog(self):
         supervisord = DummySupervisor()
         interface = self._makeOne(supervisord)

+ 10 - 0
src/supervisor/tests/test_supervisorctl.py

@@ -71,6 +71,16 @@ class ControllerTests(unittest.TestCase):
         self.assertEqual(result, None)
         self.assertEqual(controller.cmdqueue, [' version'])
         self.assertEqual(controller.stdout.getvalue(), '3000\n')
+        
+    def test_pid(self):
+        options = DummyClientOptions()
+        controller = self._makeOne(options)
+        controller.stdout = StringIO()
+        result = controller.do_pid('')
+        self.assertEqual(result, None)
+        lines = controller.stdout.getvalue().split('\n')
+        self.assertEqual(len(lines), 2)
+        self.assertEqual(lines[0], str(options._server.supervisor.getPID()))
 
     def test_tail_toofewargs(self):
         options = DummyClientOptions()