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 restart(self, name):
90 logging.debug(u'ProcessManager.restart(%s)', name)
91 if name in self.namemap:
92 self.namemap[name].stop()
93 self.namemap[name].wait()
94 self.namemap[name].restart()
96 self.namemap[name].start()
98 def kill(self, name, signum):
99 assert name in self.namemap
100 self.namemap[name].kill(name, stop)
102 def sigchild_handler(self, signum, stack_frame=None):
104 (pid, status) = os.waitpid(-1, os.WNOHANG)
106 if e.errno is errno.ECHILD:
110 if pid in self.pidmap:
112 p.process.returncode = status
113 if p.callback is not None:
115 if p.dont_run or not p.persist or p.error_count >= p.max_errors:
116 del self.namemap[p.name]
120 if p.process.returncode == 0:
126 self.pidmap[p.process.pid] = p
128 (pid, status) = os.waitpid(-1, os.WNOHANG)
130 if e.errno == errno.ECHILD:
135 if isinstance(name, basestring): # is a name
136 if name in self.namemap:
137 return self.namemap[name]
138 if name in self.services:
139 return self.services[name]
141 if name in self.pidmap:
142 return self.pidmap[name]
146 if isinstance(name, basestring): # is a name
147 if name in self.namemap:
149 if name in self.services:
152 if name in self.pidmap:
156 def __getitem__(self, name):
157 return self.get(name)
159 def __contains__(self, name):
160 return self.has(name)
163 manager = ProcessManager()
164 register = manager.register
165 unregister = manager.unregister
167 start = manager.start
172 sigchild_handler = manager.sigchild_handler
175 if __name__ == '__main__':
183 def SIGCHLD_handler(signum, stacktrace):
186 print 'SIGCHLD', signum
190 if pi.name == 'test-service':
191 print 'test-service count =', count
194 print 'test-service not persistent anymore, start test2'
197 print 'died:', pi.name, pi.command
199 register('test-service', ('sleep', '2'), notify, True)
200 register('test2', ('sleep', '3'), notify, False)
202 signal.signal(signal.SIGCHLD, SIGCHLD_handler)
204 call('test', ('sleep', '5'), notify)
205 start('test-service')
207 print "Esperando...", [pi.name for pi in manager.namemap.values()]
208 while manager.pidmap:
210 if sig == signal.SIGCHLD:
212 sigchild_handler(sig)
213 print "Esperando...", [pi.name for pi in manager.namemap.values()]