]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/procman.py
Add an example of regular attributes usage with a ValidatedClass
[software/pymin.git] / pymin / procman.py
index 0d67b9b1209fff69b6bf318243a5523b9e73ff4d..269117b7b202196a99871ff8b042e179a4a8827e 100644 (file)
@@ -68,7 +68,7 @@ class ProcessInfo:
             pid = self.process.pid
         return 'ProcessInfo(name=%s, pid=%s command=%s, persist=%s, cb=%s)' % (
                     self.name, pid, self.command, self.persist,
-                    self.callback.__name__)
+                    self.callback and self.callback.__name__ or None)
 
 class ProcessManager:
 
@@ -78,19 +78,27 @@ class ProcessManager:
         self.pidmap = dict()
         log.debug(u'ProcessManager()')
 
-    def register(self, name, command, callback=None, persist=False,
+    def register(self, name, command=None, callback=None, persist=False,
                 max_errors=3, *args, **kwargs):
         log.debug(u'ProcessManager.register(%s, %s, %s, %s, %s, %s, %s)',
                   name, command, callback, persist, max_errors, args, kwargs)
+        if not isinstance(name, ProcessInfo):
+            pi = ProcessInfo(name, command, callback, persist, max_errors,
+                             args, kwargs)
+        else:
+            pi = name
+            name = pi.name
         assert not self.has(name)
-        pi = ProcessInfo(name, command, callback, persist, max_errors,
-                        args, kwargs)
         self.services[name] = pi
         return pi
 
     def unregister(self, name):
         log.debug(u'ProcessManager.unregister(%s)', name)
-        pi = self.services[name]
+        if isinstance(name, ProcessInfo):
+            pi = name
+            name = pi.name
+        else:
+            pi = self.services[name]
         del self.services[name]
         return pi
 
@@ -98,28 +106,42 @@ class ProcessManager:
         pi.start()
         self.namemap[pi.name] = self.pidmap[pi.process.pid] = pi
 
-    def once(self, name, command, callback=None, persist=False,
+    def once(self, name, command=None, callback=None, persist=False,
                 max_errors=3, *args, **kwargs):
         log.debug(u'ProcessManager.once(%s, %s, %s, %s, %s, %s, %s)',
                   name, command, callback, persist, max_errors, args, kwargs)
+        if not isinstance(name, ProcessInfo):
+            pi = ProcessInfo(name, command, callback, persist, max_errors,
+                             args, kwargs)
+        else:
+            pi = name
+            name = pi.name
         assert not self.has(name)
-        pi = ProcessInfo(name, command, callback, persist, max_errors,
-                         args, kwargs)
         self._call(pi)
         return pi
 
     def start(self, name):
         log.debug(u'ProcessManager.start(%s)', name)
-        assert name not in self.namemap
-        self._call(self.services[name])
+        if isinstance(name, ProcessInfo):
+            name = name.name
+        if name not in self.namemap:
+            self._call(self.services[name])
+            return True
+        return False
 
     def stop(self, name):
         log.debug(u'ProcessManager.stop(%s)', name)
-        assert name in self.namemap
-        self.namemap[name].stop()
+        if isinstance(name, ProcessInfo):
+            name = name.name
+        if name in self.namemap:
+            self.namemap[name].stop()
+            return True
+        return False
 
     def restart(self, name):
         log.debug(u'ProcessManager.restart(%s)', name)
+        if isinstance(name, ProcessInfo):
+            name = name.name
         # we have to check first in namemap in case is an unregistered
         # process (added with once())
         if name in self.namemap:
@@ -131,13 +153,19 @@ class ProcessManager:
             pi.restart()
             # add the new PID
             self.pidmap[pi.process.pid] = pi
+            return True
         else:
             self.start(name)
+            return False
 
     def kill(self, name, signum):
         log.debug(u'ProcessManager.kill(%s, %s)', name, signum)
-        assert name in self.namemap
-        self.namemap[name].kill(name, stop)
+        if isinstance(name, ProcessInfo):
+            name = name.name
+        if name in self.namemap:
+            self.namemap[name].kill(name, stop)
+            return True
+        return False
 
     def sigchild_handler(self, signum, stack_frame=None):
         log.debug(u'ProcessManager.sigchild_handler(%s)', signum)