1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 __doc__='''Schedule
16
17 Walk through the maintenance schedule.
18
19 $Id$
20 '''
21 import time
22 import logging
23 import transaction
24 from twisted.internet import reactor
25
27
29 "start executing the schedule"
30 self.dmd = dmd
31 self.maintenance = []
32 self.options = options
33 self.log = logging.getLogger("zen.Schedule")
34 self.workList = []
35 self.timer = None
36
37
39 "Set options in a borrowed parser"
40
41
45
46
51
52
54 "Synch with the database"
55 self.dmd._p_jar.sync()
56
71
77
79 work = [(mw.nextEvent(now), mw) for mw in workList]
80 work.sort()
81
82 while len(work):
83 t, mw = work[0]
84 if t: break
85 if mw.enabled:
86 self.log.debug("Never going to run Maintenance "
87 "Window %s for %s again",
88 mw.getId(), mw.target().getId())
89 if mw.started:
90 mw.end()
91 work.pop(0)
92 return work
93
96
98 "Execute all the maintanance windows at the proper time"
99
100 if self.timer and not self.timer.called:
101 self.timer.cancel()
102
103
104 now = self.now()
105 work = self.makeWorkList(now, self.workList)
106 self.workList = [mw for t, mw in work]
107
108
109 for next, mw in work:
110 if next <= now:
111 how = {True:'stopping', False:'starting'}[bool(mw.started)]
112 self.log.debug("Maintenance window "
113 "%s %s for %s",
114 how, mw.getId(), mw.target().getId())
115 self.executeMaintenanceWindow(mw, next)
116 else:
117 break
118
119 work = self.makeWorkList(now, self.workList)
120 if work:
121 wait = max(0, work[0][0] - now)
122 self.log.debug("Waiting %f seconds", wait)
123 self.timer = self.callLater(wait)
124 self.commit()
125
128
131
132 - def executeMaintenanceWindow(self, mw, timestamp):
133 mw.execute(timestamp)
134
135 if __name__ == "__main__":
153
154 import Globals
155 from Products.ZenUtils.ZCmdBase import ZCmdBase
156
157 cmd = ZCmdBase()
159 s = MySchedule(Options(), cmd.dmd)
160
161 end = s.currentTime + 60*60*24*30
162 while s.currentTime < end:
163 s.run()
164