X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/6253fd538884266caab60f3c30a7c1b08a56881b..2f68311579c5a7179c2e6d2202034a1e7d8fff2a:/pymin/procman.py diff --git a/pymin/procman.py b/pymin/procman.py index 9816997..f6a393d 100644 --- a/pymin/procman.py +++ b/pymin/procman.py @@ -7,7 +7,7 @@ import subprocess import logging ; log = logging.getLogger('pymin.procman') __all__ = ('ProcessManager', 'manager', 'register', 'unregister', 'call', - 'start', 'stop', 'kill', 'get', 'has', 'sigchild_handler') + 'start', 'stop', 'restart', 'kill', 'get', 'has', 'sigchild_handler') class ProcessInfo: def __init__(self, name, command, callback=None, persist=False, @@ -102,12 +102,15 @@ class ProcessManager: def restart(self, name): log.debug(u'ProcessManager.restart(%s)', name) + # we have to check first in namemap in case is an unregistered + # process (added with call()) if name in self.namemap: - self.namemap[name].stop() - self.namemap[name].wait() - self.namemap[name].restart() + pi = self.namemap[name] + pi.stop() + pi.process.wait() + pi.restart() else: - self.namemap[name].start() + self.services[name].start() def kill(self, name, signum): log.debug(u'ProcessManager.kill(%s, %s)', name, signum) @@ -206,6 +209,7 @@ unregister = manager.unregister call = manager.call start = manager.start stop = manager.stop +restart = manager.restart kill = manager.kill get = manager.get has = manager.has @@ -231,24 +235,49 @@ if __name__ == '__main__': print 'test-service count =', count count += 1 if count > 4: - print 'test-service not persistent anymore, start test2' + print 'set test-service non-persistent, start test-service-2' pi.persist = False - pm.start('test2') + assert 'test-service-2' not in manager.namemap + pm.start('test-service-2') + assert 'test-service-2' in manager.namemap print 'died:', pi.name, pi.command register('test-service', ('sleep', '2'), notify, True) - register('test2', ('sleep', '3'), notify, False) + assert 'test-service' in manager.services + assert 'test-service' not in manager.namemap + + register('test-service-2', ('sleep', '3'), notify, False) + assert 'test-service-2' in manager.services + assert 'test-service-2' not in manager.namemap signal.signal(signal.SIGCHLD, SIGCHLD_handler) - call('test', ('sleep', '5'), notify) + call('test-once', ('sleep', '5'), notify) + assert 'test-once' not in manager.services + assert 'test-once' in manager.namemap + start('test-service') + assert 'test-service' in manager.namemap - print "Esperando...", [pi.name for pi in manager.namemap.values()] + print "Known processes:", manager.services.keys() + print "Waiting...", manager.namemap.keys() + print "------------------------------------------------------------------" while manager.pidmap: signal.pause() if sig == signal.SIGCHLD: sigchild_handler(sig) sig = None - print "Esperando...", [pi.name for pi in manager.namemap.values()] + print "Known processes:", manager.services.keys() + print "Waiting...", manager.namemap.keys() + print "------------------------------------------------------------------" + assert 'test-service' not in manager.namemap + assert 'test-service-2' not in manager.namemap + assert 'test-once' not in manager.services + assert 'test-once' not in manager.namemap + + call('test-wait', ('sleep', '2')) + print 'test-wait returned?', get('test-wait').process.poll() + print 'Waiting test-wait to return...' + ret = get('test-wait').process.wait() + print 'Done! returned:', ret