1 # vim: set encoding=utf-8 et sw=4 sts=4 :
7 import logging ; log = logging.getLogger('pymin.procman')
9 __all__ = ('ProcessManager', 'manager', 'register', 'unregister', 'call',
10 'start', 'stop', 'kill', 'get', 'has', 'sigchild_handler')
13 def __init__(self, name, command, callback=None, persist=False,
14 args=None, kw=None, max_errors=3):
16 self.command = command
17 self.callback = callback
18 if args is None: args = list()
20 if kw is None: kw = dict()
22 self.persist = persist
23 self.max_errors = max_errors
32 assert self.process is None
36 log.debug(u'ProcessInfo.restart(): executing %s', self.command)
37 self.process = subprocess.Popen(self.command, *self.args, **self.kw)
40 assert self.process is not None
42 if self.signal == signal.SIGTERM or self.signal == signal.SIGKILL:
43 # Allready stopped, kill it
44 self.kill(signal.SIGKILL)
47 self.kill(signal.SIGTERM)
48 def kill(self, signum):
49 log.debug(u'ProcessInfo.kill(): killing pid %s with signal %s',
50 self.process.pid, signum)
51 assert self.process is not None
52 os.kill(self.process.pid, signum)
56 if self.process is not None:
57 pid = self.process.pid
58 return 'ProcessInfo(name=%s, pid=%s command=%s, persist=%s, cb=%s)' % (
59 self.name, pid, self.command, self.persist,
60 self.callback.__name__)
65 self.services = dict()
68 log.debug(u'ProcessManager()')
70 def register(self, name, command, callback=None, persist=False,
72 log.debug(u'ProcessManager.register(%s, %s, %s, %s, %s, %s)',
73 name, command, callback, persist, args, kw)
74 self.services[name] = ProcessInfo(name, command, callback, persist,
77 def unregister(self, name):
78 log.debug(u'ProcessManager.unregister(%s)', name)
79 del self.services[name]
83 self.namemap[pi.name] = self.pidmap[pi.process.pid] = pi
85 def call(self, name, command, callback=None, persist=False, *args, **kw):
86 log.debug(u'ProcessManager.call(%s, %s, %s, %s, %s, %s)',
87 name, command, callback, persist, args, kw)
88 pi = ProcessInfo(name, command, callback, persist, args, kw)
91 def start(self, name):
92 log.debug(u'ProcessManager.start(%s)', name)
93 assert name not in self.namemap
94 self._call(self.services[name])
97 log.debug(u'ProcessManager.stop(%s)', name)
98 assert name in self.namemap
99 self.namemap[name].stop()
101 def restart(self, name):
102 log.debug(u'ProcessManager.restart(%s)', name)
103 if name in self.namemap:
104 self.namemap[name].stop()
105 self.namemap[name].wait()
106 self.namemap[name].restart()
108 self.namemap[name].start()
110 def kill(self, name, signum):
111 log.debug(u'ProcessManager.kill(%s, %s)', name, signum)
112 assert name in self.namemap
113 self.namemap[name].kill(name, stop)
115 def sigchild_handler(self, signum, stack_frame=None):
116 log.debug(u'ProcessManager.sigchild_handler(%s)', signum)
118 (pid, status) = os.waitpid(-1, os.WNOHANG)
120 log.debug(u'ProcessManager.sigchild_handler(): OSError')
121 if e.errno is errno.ECHILD:
122 log.debug(u'ProcessManager.sigchild_handler(): OSError ECHILD')
125 log.debug(u'ProcessManager.sigchild_handler: pid=%s, status=%s',
128 if pid in self.pidmap:
130 p.process.returncode = status
131 if p.callback is not None:
132 log.debug(u'ProcessManager.sigchild_handler: '
133 u'calling %s(%s)', p.callback.__name__, p)
135 if p.dont_run or not p.persist or p.error_count >= p.max_errors:
136 log.debug(u"ProcessManager.sigchild_handler: can't "
137 u'persist, dont_run=%s, persist=%s, error_cout=%s, '
138 u'max_errors=%s', p.dont_run, p.persist,
139 p.error_count, p.max_errors)
140 del self.namemap[p.name]
144 log.debug(u'ProcessManager.sigchild_handler: persist')
145 if p.process.returncode == 0:
147 log.debug(u'ProcessManager.sigchild_handler: '
148 u'return OK, resetting error_count')
151 log.debug(u'ProcessManager.sigchild_handler: return'
152 u'not 0, error_count + 1 = %s', p.error_count)
155 self.pidmap[p.process.pid] = p
157 (pid, status) = os.waitpid(-1, os.WNOHANG)
159 if e.errno == errno.ECHILD:
164 if isinstance(name, basestring): # is a name
165 if name in self.namemap:
166 return self.namemap[name]
167 if name in self.services:
168 return self.services[name]
170 if name in self.pidmap:
171 return self.pidmap[name]
175 if isinstance(name, basestring): # is a name
176 if name in self.namemap:
178 if name in self.services:
181 if name in self.pidmap:
185 def __getitem__(self, name):
186 return self.get(name)
188 def __contains__(self, name):
189 return self.has(name)
192 if __name__ == '__main__':
194 level = logging.DEBUG,
195 format = '%(asctime)s %(levelname)-8s %(message)s',
196 datefmt = '%H:%M:%S',
201 manager = ProcessManager()
202 register = manager.register
203 unregister = manager.unregister
205 start = manager.start
210 sigchild_handler = manager.sigchild_handler
213 if __name__ == '__main__':
221 def SIGCHLD_handler(signum, stacktrace):
224 print 'SIGCHLD', signum
228 if pi.name == 'test-service':
229 print 'test-service count =', count
232 print 'test-service not persistent anymore, start test2'
235 print 'died:', pi.name, pi.command
237 register('test-service', ('sleep', '2'), notify, True)
238 register('test2', ('sleep', '3'), notify, False)
240 signal.signal(signal.SIGCHLD, SIGCHLD_handler)
242 call('test', ('sleep', '5'), notify)
243 start('test-service')
245 print "Esperando...", [pi.name for pi in manager.namemap.values()]
246 while manager.pidmap:
248 if sig == signal.SIGCHLD:
249 sigchild_handler(sig)
251 print "Esperando...", [pi.name for pi in manager.namemap.values()]