1 # vim: set encoding=utf-8 et sw=4 sts=4 :
9 def __init__(self, name, command, callback=None, persist=False,
10 args=None, kw=None, max_errors=3):
12 self.command = command
13 self.callback = callback
14 if args is None: args = list()
16 if kw is None: kw = dict()
18 self.persist = persist
19 self.max_errors = max_errors
26 self.last_return = None
29 assert self.process is None
33 self.process = subprocess.Popen(self.command, *self.args, **self.kw)
36 assert self.process is not None
38 if self.signal == signal.SIGTERM or self.signal == signal.SIGKILL:
39 # Allready stopped, kill it
40 self.kill(signal.SIGKILL)
43 self.kill(signal.SIGTERM)
44 def kill(self, signum):
45 assert self.process is not None
46 os.kill(pi.process.pid, signum)
50 if self.process is not None:
51 pid = self.process.pid
52 return 'ProcessInfo(name=%s, pid=%s command=%s, persist=%s, cb=%s)' % (
53 self.name, pid, self.command, self.persist,
54 self.callback.__name__)
59 self.services = dict()
63 def register(self, name, command, callback=None, persist=False,
65 self.services[name] = ProcessInfo(name, command, callback, persist,
68 def unregister(self, name):
69 del self.services[name]
73 self.namemap[pi.name] = self.pidmap[pi.process.pid] = pi
75 def call(self, name, command, callback=None, persist=False, *args, **kw):
76 pi = ProcessInfo(name, command, callback, persist, args, kw)
79 def start(self, name):
80 assert name not in self.namemap
81 self._call(self.services[name])
84 assert name in self.namemap
85 self.namemap[name].stop(name)
87 def kill(self, name, signum):
88 assert name in self.namemap
89 self.namemap[name].kill(name, stop)
91 def sigchild_handler(self, signum):
93 (pid, status) = os.waitpid(-1, os.WNOHANG)
95 if e.errno is e.ECHILD:
99 if pid in self.pidmap:
101 if p.callback is not None:
103 if p.dont_run or not p.persist or p.error_count >= p.max_errors:
104 del self.namemap[p.name]
108 if p.process.returncode == 0:
114 self.pidmap[p.process.pid] = p
116 (pid, status) = os.waitpid(-1, os.WNOHANG)
118 if e.errno == errno.ECHILD:
122 def __getitem__(self, name):
123 if isinstance(name, basestring): # is a name
124 if name in self.namemap:
125 return self.namemap[name]
126 if name in self.services:
127 return self.services[name]
129 if name in self.pidmap:
130 return self.pidmap[name]
131 return KeyError, name
133 def __contains__(self, name):
134 if isinstance(name, basestring): # is a name
135 if name in self.namemap:
137 if name in self.services:
140 if name in self.pidmap:
145 if __name__ == '__main__':
153 def sigchild_handler(signum, stacktrace):
156 print 'SIGCHLD', signum
160 if pi.name == 'test-service':
161 print 'test-service count =', count
164 print 'test-service not persistent anymore, start test2'
167 print 'died:', pi.name, pi.command
169 procman = ProcessManager()
171 procman.register('test-service', ('sleep', '2'), notify, True)
172 procman.register('test2', ('sleep', '3'), notify, False)
174 signal.signal(signal.SIGCHLD, sigchild_handler)
176 procman.call('test', ('sleep', '5'), notify)
177 procman.start('test-service')
179 while procman.pidmap:
181 if sig == signal.SIGCHLD:
183 procman.sigchild_handler(sig)
184 print "Esperando...", [pi.name for pi in procman.namemap.values()]