adjust touch target on mobile Feature page
[people/shoehn/ipfire.org.git] / manager.py
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                 self.backend = backend.Backend("webapp.conf")
16
17         @property
18         def ioloop(self):
19                 return tornado.ioloop.IOLoop.instance()
20
21         def add(self, manager_cls):
22                 manager = manager_cls(self)
23                 self._managers.append(manager)
24
25         def run(self):
26                 """
27                         Main loop.
28                 """
29                 for manager in self._managers:
30                         manager.pc.start()
31
32                 self.ioloop.start()
33
34         def shutdown(self):
35                 self.ioloop.stop()
36
37
38 class Manager(object):
39         def __init__(self, daemon):
40                 self.daemon = daemon
41
42                 self.pc = tornado.ioloop.PeriodicCallback(self, self.timeout * 1000)
43                 
44                 logging.info("%s was initialized." % self.__class__.__name__)
45
46                 self()
47
48         def __call__(self):
49                 logging.info("%s main method was called." % self.__class__.__name__)
50
51                 self.do()
52
53                 # Update callback_time.
54                 self.pc.callback_time = self.timeout * 1000
55                 logging.debug("Next call will be in %.2f seconds." % \
56                         (self.pc.callback_time / 1000))
57
58         @property
59         def backend(self):
60                 return self.daemon.backend
61
62         @property
63         def settings(self):
64                 return self.backend.settings
65
66         @property
67         def timeout(self):
68                 """
69                         Return a new callback timeout in seconds.
70                 """
71                 raise NotImplementedError
72
73         def do(self):
74                 raise NotImplementedError
75
76
77
78 class MirrorManager(Manager):
79         @property
80         def mirrors(self):
81                 return self.backend.mirrors
82
83         @property
84         def timeout(self):
85                 return self.backend.settings.get_int("mirror_check_interval", 3600)
86
87         def do(self):
88                 # Check status of all mirror servers.
89                 self.mirrors.check_all()
90
91
92 class ReleaseFilesManager(Manager):
93         @property
94         def releases(self):
95                 return self.backend.releases
96
97         @property
98         def timeout(self):
99                 return self.settings.get_int("releasefiles_check_interval", 3600)
100
101         def do(self):
102                 for release in self.releases._get_all():
103                         release.scan_files()
104
105
106 if __name__ == "__main__":
107         d = Daemon()
108         d.add(MirrorManager)
109         d.add(ReleaseFilesManager)
110
111         d.run()