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
31 assert self.process is None
35 self.process = subprocess.Popen(self.command, *self.args, **self.kw)
38 assert self.process is not None
40 if self.signal == signal.SIGTERM or self.signal == signal.SIGKILL:
41 # Allready stopped, kill it
42 self.kill(signal.SIGKILL)
45 self.kill(signal.SIGTERM)
46 def kill(self, signum):
47 assert self.process is not None
48 os.kill(self.process.pid, signum)
52 if self.process is not None:
53 pid = self.process.pid
54 return 'ProcessInfo(name=%s, pid=%s command=%s, persist=%s, cb=%s)' % (
55 self.name, pid, self.command, self.persist,
56 self.callback.__name__)
61 self.services = dict()
65 def register(self, name, command, callback=None, persist=False,
67 self.services[name] = ProcessInfo(name, command, callback, persist,
70 def unregister(self, name):
71 del self.services[name]
75 self.namemap[pi.name] = self.pidmap[pi.process.pid] = pi
77 def call(self, name, command, callback=None, persist=False, *args, **kw):
78 pi = ProcessInfo(name, command, callback, persist, args, kw)
81 def start(self, name):
82 assert name not in self.namemap
83 self._call(self.services[name])
86 assert name in self.namemap
87 self.namemap[name].stop()
89 def kill(self, name, signum):
90 assert name in self.namemap
91 self.namemap[name].kill(name, stop)
93 def sigchild_handler(self, signum, stack_frame=None):
95 (pid, status) = os.waitpid(-1, os.WNOHANG)
97 if e.errno is errno.ECHILD:
101 if pid in self.pidmap:
103 p.process.returncode = status
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]
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()]