]> git.llucax.com Git - software/pymin.git/blob - pymin/procman.py
c8a8574ddbc7d0da726fb36e6a1d75e3d744c404
[software/pymin.git] / pymin / procman.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 import os
4 import errno
5 import signal
6 import subprocess
7 import logging ; log = logging.getLogger('pymin.procman')
8
9 __all__ = ('ProcessManager', 'manager', 'register', 'unregister', 'once',
10            'start', 'stop', 'restart', 'kill', 'get', 'has', 'sigchild_handler')
11
12 class ProcessInfo:
13     def __init__(self, name, command, callback=None, persist=False,
14                  max_errors=3, args=None, kwargs=None):
15         self._name = name
16         self.command = command
17         self.callback = callback
18         if args is None: args = list()
19         self.args = args
20         if kwargs is None: kwargs = dict()
21         self.kwargs = kwargs
22         self.persist = persist
23         self.max_errors = max_errors
24         self.clear()
25     def clear(self):
26         self._dont_run = False
27         self._signal = None
28         self._process = None
29         self.error_count = 0
30     def start(self):
31         assert self.process is None
32         self.restart()
33     def restart(self):
34         self.clear()
35         log.debug(u'ProcessInfo.restart(): executing %s', self.command)
36         self._process = subprocess.Popen(self.command,
37                                          *self.args, **self.kwargs)
38     def stop(self):
39         assert self.process is not None
40         self._dont_run = True
41         if self._signal == signal.SIGTERM or self._signal == signal.SIGKILL:
42             # Allready stopped, kill it
43             self.kill(signal.SIGKILL)
44         else:
45             # Stop it
46             self.kill(signal.SIGTERM)
47     def kill(self, signum):
48         log.debug(u'ProcessInfo.kill(): killing pid %s with signal %s',
49                       self.process.pid, signum)
50         assert self.process is not None
51         os.kill(self.process.pid, signum)
52         self._signal = signum
53     @property
54     def running(self):
55         return self.process is not None and self.process.poll() is None
56     @property
57     def name(self):
58         return self._name
59     @property
60     def process(self):
61         return self._process
62     def __repr__(self):
63         pid = None
64         if self.process is not None:
65             pid = self.process.pid
66         return 'ProcessInfo(name=%s, pid=%s command=%s, persist=%s, cb=%s)' % (
67                     self.name, pid, self.command, self.persist,
68                     self.callback.__name__)
69
70 class ProcessManager:
71
72     def __init__(self):
73         self.services = dict()
74         self.namemap = dict()
75         self.pidmap = dict()
76         log.debug(u'ProcessManager()')
77
78     def register(self, name, command, callback=None, persist=False,
79                 max_errors=3, *args, **kwargs):
80         log.debug(u'ProcessManager.register(%s, %s, %s, %s, %s, %s, %s)',
81                   name, command, callback, persist, max_errors, args, kwargs)
82         assert not self.has(name)
83         pi = ProcessInfo(name, command, callback, persist, max_errors,
84                         args, kwargs)
85         self.services[name] = pi
86         return pi
87
88     def unregister(self, name):
89         log.debug(u'ProcessManager.unregister(%s)', name)
90         pi = self.services[name]
91         del self.services[name]
92         return pi
93
94     def _call(self, pi):
95         pi.start()
96         self.namemap[pi.name] = self.pidmap[pi.process.pid] = pi
97
98     def once(self, name, command, callback=None, persist=False,
99                 max_errors=3, *args, **kwargs):
100         log.debug(u'ProcessManager.once(%s, %s, %s, %s, %s, %s, %s)',
101                   name, command, callback, persist, max_errors, args, kwargs)
102         assert not self.has(name)
103         pi = ProcessInfo(name, command, callback, persist, max_errors,
104                          args, kwargs)
105         self._call(pi)
106         return pi
107
108     def start(self, name):
109         log.debug(u'ProcessManager.start(%s)', name)
110         assert name not in self.namemap
111         self._call(self.services[name])
112
113     def stop(self, name):
114         log.debug(u'ProcessManager.stop(%s)', name)
115         assert name in self.namemap
116         self.namemap[name].stop()
117
118     def restart(self, name):
119         log.debug(u'ProcessManager.restart(%s)', name)
120         # we have to check first in namemap in case is an unregistered
121         # process (added with once())
122         if name in self.namemap:
123             pi = self.namemap[name]
124             # the process will change its PID, so we delete it while we know it
125             del self.pidmap[pi.process.pid]
126             pi.stop()
127             pi.process.wait()
128             pi.restart()
129             # add the new PID
130             self.pidmap[pi.process.pid] = pi
131         else:
132             self.start(name)
133
134     def kill(self, name, signum):
135         log.debug(u'ProcessManager.kill(%s, %s)', name, signum)
136         assert name in self.namemap
137         self.namemap[name].kill(name, stop)
138
139     def sigchild_handler(self, signum, stack_frame=None):
140         log.debug(u'ProcessManager.sigchild_handler(%s)', signum)
141         try:
142             (pid, status) = os.waitpid(-1, os.WNOHANG)
143         except OSError, e:
144             log.debug(u'ProcessManager.sigchild_handler(): OSError')
145             if e.errno is errno.ECHILD:
146                 log.debug(u'ProcessManager.sigchild_handler(): OSError ECHILD')
147                 return
148             raise
149         log.debug(u'ProcessManager.sigchild_handler: pid=%s, status=%s',
150                       pid, status)
151         while pid:
152             if pid in self.pidmap:
153                 p = self.pidmap[pid]
154                 p.process.returncode = status
155                 if p.callback is not None:
156                     log.debug(u'ProcessManager.sigchild_handler: '
157                                   u'calling %s(%s)', p.callback.__name__, p)
158                     p.callback(self, p)
159                 if p._dont_run or not p.persist or p.error_count >= p.max_errors:
160                     log.debug(u"ProcessManager.sigchild_handler: can't "
161                             u'persist, dont_run=%s, persist=%s, error_cout=%s, '
162                             u'max_errors=%s', p._dont_run, p.persist,
163                             p.error_count, p.max_errors)
164                     del self.namemap[p.name]
165                     del self.pidmap[pid]
166                     p.clear()
167                 else:
168                     log.debug(u'ProcessManager.sigchild_handler: persist')
169                     if p.process.returncode == 0:
170                         p.error_count = 0
171                         log.debug(u'ProcessManager.sigchild_handler: '
172                                 u'return OK, resetting error_count')
173                     else:
174                         p.error_count += 1
175                         log.debug(u'ProcessManager.sigchild_handler: return'
176                                 u'not 0, error_count + 1 = %s', p.error_count)
177                     del self.pidmap[pid]
178                     p.restart()
179                     self.pidmap[p.process.pid] = p
180             try:
181                 (pid, status) = os.waitpid(-1, os.WNOHANG)
182             except OSError, e:
183                 if e.errno == errno.ECHILD:
184                     return
185                 raise
186
187     def get(self, name):
188         if isinstance(name, basestring): # is a name
189             if name in self.namemap:
190                 return self.namemap[name]
191             if name in self.services:
192                 return self.services[name]
193         else: # is a pid
194             if name in self.pidmap:
195                 return self.pidmap[name]
196         raise KeyError, name
197     # Syntax sugar for self[name]
198     __getitem__ = get
199
200     def has(self, name):
201         if isinstance(name, basestring): # is a name
202             if name in self.namemap:
203                 return True
204             if name in self.services:
205                 return True
206         else: # is a pid
207             if name in self.pidmap:
208                 return True
209         return False
210     # Syntax sugar for name in self
211     __contains__ = has
212
213
214 if __name__ == '__main__':
215     logging.basicConfig(
216         level   = logging.DEBUG,
217         format  = '%(asctime)s %(levelname)-8s %(message)s',
218         datefmt = '%H:%M:%S',
219     )
220
221
222 # Globals
223 manager = ProcessManager()
224 register = manager.register
225 unregister = manager.unregister
226 once = manager.once
227 start = manager.start
228 stop = manager.stop
229 restart = manager.restart
230 kill = manager.kill
231 get = manager.get
232 has = manager.has
233 sigchild_handler = manager.sigchild_handler
234
235
236 if __name__ == '__main__':
237
238     import signal
239     import time
240
241     sig = None
242     count = 0
243
244     def SIGCHLD_handler(signum, stacktrace):
245         global sig
246         sig = signum
247         print 'SIGCHLD', signum
248
249     def notify(pm, pi):
250         global count
251         if pi.name == 'test-service':
252             print 'test-service count =', count
253             count += 1
254             if count > 4:
255                 print 'set test-service non-persistent, start test-service-2'
256                 pi.persist = False
257                 assert 'test-service-2' not in manager.namemap
258                 pm.start('test-service-2')
259                 assert 'test-service-2' in manager.namemap
260                 assert get('test-service-2').running
261         print 'died:', pi.name, pi.command
262
263     register('test-service', ('sleep', '2'), notify, True)
264     assert 'test-service' in manager.services
265     assert 'test-service' not in manager.namemap
266     assert not get('test-service').running
267     assert manager['test-service'] == get('test-service')
268     assert has('test-service')
269     assert 'test-service' in manager
270
271     register('test-service-2', ('sleep', '3'), notify, False)
272     assert 'test-service-2' in manager.services
273     assert 'test-service-2' not in manager.namemap
274     assert not get('test-service-2').running
275
276     signal.signal(signal.SIGCHLD, SIGCHLD_handler)
277
278     once('test-once', ('sleep', '5'), notify)
279     assert 'test-once' not in manager.services
280     assert 'test-once' in manager.namemap
281     assert get('test-once').running
282     assert get('test-once').process.pid
283     pid = get('test-once').process.pid
284     restart('test-once')
285     assert pid != get('test-once').process.pid
286     assert pid != manager.pidmap[get('test-once').process.pid].process.pid
287
288     start('test-service')
289     assert 'test-service' in manager.namemap
290     assert get('test-service').running
291     assert get('test-service').process.pid
292     pid = get('test-service').process.pid
293     restart('test-service')
294     assert pid != get('test-service').process.pid
295     assert pid != manager.pidmap[get('test-service').process.pid].process.pid
296
297     print "Known processes:", manager.services.keys()
298     print "Waiting...", manager.namemap.keys()
299     print "------------------------------------------------------------------"
300     while manager.pidmap:
301         signal.pause()
302         if sig == signal.SIGCHLD:
303             sigchild_handler(sig)
304             sig = None
305         print "Known processes:", manager.services.keys()
306         print "Waiting...", manager.namemap.keys()
307         print "------------------------------------------------------------------"
308     assert 'test-service' not in manager.namemap
309     assert 'test-service-2' not in manager.namemap
310     assert 'test-once' not in manager.services
311     assert 'test-once' not in manager.namemap
312
313     restart('test-service')
314     assert get('test-service').process.pid
315     assert manager.pidmap[get('test-service').process.pid].process.pid
316
317     once('test-wait', ('sleep', '2'))
318     print 'test-wait running?', get('test-wait').running
319     assert get('test-wait').running
320     print 'Waiting test-wait to return...'
321     ret = get('test-wait').process.wait()
322     print 'Done! returned:', ret
323