]>
Commit | Line | Data |
---|---|---|
940227cb MT |
1 | #!/usr/bin/python |
2 | ||
3 | import logging | |
4 | import time | |
5 | import tornado.ioloop | |
6 | ||
7 | import webapp.backend as backend | |
8 | ||
9 | class Daemon(object): | |
10 | def __init__(self): | |
11 | self._managers = [] | |
12 | ||
13 | self.ioloop.set_blocking_log_threshold(900) | |
14 | ||
15 | @property | |
16 | def ioloop(self): | |
17 | return tornado.ioloop.IOLoop.instance() | |
18 | ||
19 | def add(self, manager_cls): | |
20 | manager = manager_cls(self) | |
21 | self._managers.append(manager) | |
22 | ||
23 | def run(self): | |
24 | """ | |
25 | Main loop. | |
26 | """ | |
27 | for manager in self._managers: | |
28 | manager.pc.start() | |
29 | ||
30 | self.ioloop.start() | |
31 | ||
32 | def shutdown(self): | |
33 | self.ioloop.stop() | |
34 | ||
35 | ||
36 | class Manager(object): | |
37 | def __init__(self, daemon): | |
38 | self.daemon = daemon | |
39 | ||
40 | self.pc = tornado.ioloop.PeriodicCallback(self, self.timeout * 1000) | |
41 | ||
42 | logging.info("%s was initialized." % self.__class__.__name__) | |
43 | ||
44 | self() | |
45 | ||
46 | def __call__(self): | |
47 | logging.info("%s main method was called." % self.__class__.__name__) | |
48 | ||
49 | self.do() | |
50 | ||
51 | # Update callback_time. | |
52 | self.pc.callback_time = self.timeout * 1000 | |
53 | logging.debug("Next call will be in %.2f seconds." % \ | |
54 | (self.pc.callback_time / 1000)) | |
55 | ||
56 | @property | |
57 | def timeout(self): | |
58 | """ | |
59 | Return a new callback timeout in seconds. | |
60 | """ | |
61 | raise NotImplementedError | |
62 | ||
63 | def do(self): | |
64 | raise NotImplementedError | |
65 | ||
66 | ||
67 | ||
68 | class MirrorManager(Manager): | |
69 | @property | |
70 | def mirrors(self): | |
71 | return backend.Mirrors() | |
72 | ||
73 | @property | |
74 | def timeout(self): | |
75 | return backend.Config().get_int("mirror_check_interval") | |
76 | ||
77 | def do(self): | |
78 | # Check status of all mirror servers. | |
79 | self.mirrors.check_all() | |
80 | ||
81 | ||
82 | if __name__ == "__main__": | |
83 | d = Daemon() | |
84 | d.add(MirrorManager) | |
85 | ||
86 | d.run() |