1 # vim: set encoding=utf-8 et sw=4 sts=4 :
8 __ALL__ = ('ProcessManager', 'manager', 'register', 'unregister', 'call',
9 'start', 'stop', 'kill', 'get', 'has', 'sigchild_handler')
12 def __init__(self, name, command, callback=None, persist=False,
13 args=None, kw=None, max_errors=3):
15 self.command = command
16 self.callback = callback
17 if args is None: args = list()
19 if kw is None: kw = dict()
21 self.persist = persist
22 self.max_errors = max_errors
29 self.last_return = None
32 assert self.process is None
36 self.process = subprocess.Popen(self.command, *self.args, **self.kw)
39 assert self.process is not None
41 if self.signal == signal.SIGTERM or self.signal == signal.SIGKILL:
42 # Allready stopped, kill it
43 self.kill(signal.SIGKILL)
46 self.kill(signal.SIGTERM)
47 def kill(self, signum):
48 assert self.process is not None
49 os.kill(pi.process.pid, signum)
53 if self.process is not None:
54 pid = self.process.pid
55 return 'ProcessInfo(name=%s, pid=%s command=%s, persist=%s, cb=%s)' % (
56 self.name, pid, self.command, self.persist,
57 self.callback.__name__)
62 self.services = dict()
66 def register(self, name, command, callback=None, persist=False,
68 self.services[name] = ProcessInfo(name, command, callback, persist,
71 def unregister(self, name):
72 del self.services[name]
76 self.namemap[pi.name] = self.pidmap[pi.process.pid] = pi
78 def call(self, name, command, callback=None, persist=False, *args, **kw):
79 pi = ProcessInfo(name, command, callback, persist, args, kw)
82 def start(self, name):
83 assert name not in self.namemap
84 self._call(self.services[name])
87 assert name in self.namemap
88 self.namemap[name].stop(name)
90 def kill(self, name, signum):
91 assert name in self.namemap
92 self.namemap[name].kill(name, stop)
94 def sigchild_handler(self, signum, stack_frame=None):
96 (pid, status) = os.waitpid(-1, os.WNOHANG)
98 if e.errno is errno.ECHILD:
102 if pid in self.pidmap:
104 if p.callback is not None:
106 if p.dont_run or not p.persist or p.error_count >= p.max_errors:
107 del self.namemap[p.name]
111 if p.process.returncode == 0:
117 self.pidmap[p.process.pid] = p
119 (pid, status) = os.waitpid(-1, os.WNOHANG)
121 if e.errno == errno.ECHILD:
126 if isinstance(name, basestring): # is a name
127 if name in self.namemap:
128 return self.namemap[name]
129 if name in self.services:
130 return self.services[name]
132 if name in self.pidmap:
133 return self.pidmap[name]
134 return KeyError, name
137 if isinstance(name, basestring): # is a name
138 if name in self.namemap:
140 if name in self.services:
143 if name in self.pidmap:
147 def __getitem__(self, name):
148 return self.get(name)
150 def __contains__(self, name):
151 return self.has(name)
154 manager = ProcessManager()
155 register = manager.register
156 unregister = manager.unregister
158 start = manager.start
163 sigchild_handler = manager.sigchild_handler
166 if __name__ == '__main__':
174 def SIGCHLD_handler(signum, stacktrace):
177 print 'SIGCHLD', signum
181 if pi.name == 'test-service':
182 print 'test-service count =', count
185 print 'test-service not persistent anymore, start test2'
188 print 'died:', pi.name, pi.command
190 register('test-service', ('sleep', '2'), notify, True)
191 register('test2', ('sleep', '3'), notify, False)
193 signal.signal(signal.SIGCHLD, SIGCHLD_handler)
195 call('test', ('sleep', '5'), notify)
196 start('test-service')
198 print "Esperando...", [pi.name for pi in manager.namemap.values()]
199 while manager.pidmap:
201 if sig == signal.SIGCHLD:
203 sigchild_handler(sig)
204 print "Esperando...", [pi.name for pi in manager.namemap.values()]