]> git.llucax.com Git - software/pymin.git/blob - pymin/procman.py
Convert Rule to Item for validation in firewall service handler.
[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', 'call',
10            'start', 'stop', 'kill', 'get', 'has', 'sigchild_handler')
11
12 class ProcessInfo:
13     def __init__(self, name, command, callback=None, persist=False,
14                        args=None, kw=None, max_errors=3):
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 kw is None: kw = dict()
21         self.kw = kw
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         self.running = False
31     def start(self):
32         assert self.process is None
33         self.restart()
34     def restart(self):
35         self.clear()
36         log.debug(u'ProcessInfo.restart(): executing %s', self.command)
37         self.process = subprocess.Popen(self.command, *self.args, **self.kw)
38         self.running = True
39     def stop(self):
40         assert self.process is not None
41         self.dont_run = True
42         if self.signal == signal.SIGTERM or self.signal == signal.SIGKILL:
43             # Allready stopped, kill it
44             self.kill(signal.SIGKILL)
45         else:
46             # Stop it
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)
53         self.signal = signum
54     def __repr__(self):
55         pid = None
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__)
61
62 class ProcessManager:
63
64     def __init__(self):
65         self.services = dict()
66         self.namemap = dict()
67         self.pidmap = dict()
68         log.debug(u'ProcessManager()')
69
70     def register(self, name, command, callback=None, persist=False,
71                        *args, **kw):
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,
75                                           args, kw)
76
77     def unregister(self, name):
78         log.debug(u'ProcessManager.unregister(%s)', name)
79         del self.services[name]
80
81     def _call(self, pi):
82         pi.start()
83         self.namemap[pi.name] = self.pidmap[pi.process.pid] = pi
84
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)
89         self._call(pi)
90
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])
95
96     def stop(self, name):
97         log.debug(u'ProcessManager.stop(%s)', name)
98         assert name in self.namemap
99         self.namemap[name].stop()
100
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()
107         else:
108             self.namemap[name].start()
109
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)
114
115     def sigchild_handler(self, signum, stack_frame=None):
116         log.debug(u'ProcessManager.sigchild_handler(%s)', signum)
117         try:
118             (pid, status) = os.waitpid(-1, os.WNOHANG)
119         except OSError, e:
120             log.debug(u'ProcessManager.sigchild_handler(): OSError')
121             if e.errno is errno.ECHILD:
122                 log.debug(u'ProcessManager.sigchild_handler(): OSError ECHILD')
123                 return
124             raise
125         log.debug(u'ProcessManager.sigchild_handler: pid=%s, status=%s',
126                       pid, status)
127         while pid:
128             if pid in self.pidmap:
129                 p = self.pidmap[pid]
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)
134                     p.callback(self, 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]
141                     del self.pidmap[pid]
142                     p.clear()
143                 else:
144                     log.debug(u'ProcessManager.sigchild_handler: persist')
145                     if p.process.returncode == 0:
146                         p.error_count = 0
147                         log.debug(u'ProcessManager.sigchild_handler: '
148                                 u'return OK, resetting error_count')
149                     else:
150                         p.error_count += 1
151                         log.debug(u'ProcessManager.sigchild_handler: return'
152                                 u'not 0, error_count + 1 = %s', p.error_count)
153                     del self.pidmap[pid]
154                     p.restart()
155                     self.pidmap[p.process.pid] = p
156             try:
157                 (pid, status) = os.waitpid(-1, os.WNOHANG)
158             except OSError, e:
159                 if e.errno == errno.ECHILD:
160                     return
161                 raise
162
163     def get(self, name):
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]
169         else: # is a pid
170             if name in self.pidmap:
171                 return self.pidmap[name]
172         raise KeyError, name
173
174     def has(self, name):
175         if isinstance(name, basestring): # is a name
176             if name in self.namemap:
177                 return True
178             if name in self.services:
179                 return True
180         else: # is a pid
181             if name in self.pidmap:
182                 return True
183         return False
184
185     def __getitem__(self, name):
186         return self.get(name)
187
188     def __contains__(self, name):
189         return self.has(name)
190
191
192 if __name__ == '__main__':
193     logging.basicConfig(
194         level   = logging.DEBUG,
195         format  = '%(asctime)s %(levelname)-8s %(message)s',
196         datefmt = '%H:%M:%S',
197     )
198
199
200 # Globals
201 manager = ProcessManager()
202 register = manager.register
203 unregister = manager.unregister
204 call = manager.call
205 start = manager.start
206 stop = manager.stop
207 kill = manager.kill
208 get = manager.get
209 has = manager.has
210 sigchild_handler = manager.sigchild_handler
211
212
213 if __name__ == '__main__':
214
215     import signal
216     import time
217
218     sig = None
219     count = 0
220
221     def SIGCHLD_handler(signum, stacktrace):
222         global sig
223         sig = signum
224         print 'SIGCHLD', signum
225
226     def notify(pm, pi):
227         global count
228         if pi.name == 'test-service':
229             print 'test-service count =', count
230             count += 1
231             if count > 4:
232                 print 'test-service not persistent anymore, start test2'
233                 pi.persist = False
234                 pm.start('test2')
235         print 'died:', pi.name, pi.command
236
237     register('test-service', ('sleep', '2'), notify, True)
238     register('test2', ('sleep', '3'), notify, False)
239
240     signal.signal(signal.SIGCHLD, SIGCHLD_handler)
241
242     call('test', ('sleep', '5'), notify)
243     start('test-service')
244
245     print "Esperando...", [pi.name for pi in manager.namemap.values()]
246     while manager.pidmap:
247         signal.pause()
248         if sig == signal.SIGCHLD:
249             sigchild_handler(sig)
250             sig = None
251         print "Esperando...", [pi.name for pi in manager.namemap.values()]
252