130 self._insert(ev, snapshot) |
131 self._insert(ev, snapshot) |
131 |
132 |
132 |
133 |
133 |
134 |
134 class Scheduler(QueueThread): |
135 class Scheduler(QueueThread): |
135 # Default to precision of 60 seconds: the scheduler thread will never |
136 # Default to precision of 5 minutes: if there are finite-horizon events in |
136 # sleep longer than that, to get quickly back on track with the schedule |
137 # the queue, the scheduler thread will never sleep longer than that. |
137 # when the computer wakes up from sleep |
138 # This is to quickly get back on track with the schedule when the computer |
138 def __init__(self, precision=60): |
139 # wakes up from sleep, if the sleep monitor is not working or is not |
|
140 # implemented for the particular operating system. However, if there are |
|
141 # only infinite-horizon events in the queue (meaning, DreamTime-scheduled |
|
142 # events, and the system is sleeping or "sleeping"), the scheduler will |
|
143 # also sleep. Hopefully this will also help the system stay properly asleep. |
|
144 def __init__(self, precision=60*5): |
139 self.precision = precision |
145 self.precision = precision |
140 self._next_event_time = None |
146 self._next_event_time = None |
141 super().__init__(target = self._scheduler_thread, name = 'Scheduler') |
147 super().__init__(target = self._scheduler_thread, name = 'Scheduler') |
142 dreamtime.add_callback(self, self._sleepwake_callback) |
148 dreamtime.add_callback(self, self._sleepwake_callback) |
143 |
149 |
148 snapshot = dreamtime.Snapshot() |
154 snapshot = dreamtime.Snapshot() |
149 now = snapshot.monotonic() |
155 now = snapshot.monotonic() |
150 if not self._list: |
156 if not self._list: |
151 timeout = None |
157 timeout = None |
152 else: |
158 else: |
153 # Wait at most precision seconds, or until next event if it |
159 delta = self._list.when.horizon(snapshot)-now |
154 # comes earlier |
160 if delta==math.inf: |
155 delta = self._list.when.monotonic(snapshot)-now |
161 timeout=None |
156 timeout = min(self.precision, delta) |
162 else: |
|
163 timeout = min(self.precision, delta) |
157 |
164 |
158 if not timeout or timeout>0: |
165 if not timeout or timeout>0: |
159 logger.debug("Scheduler waiting %s seconds" % str(timeout)) |
166 logger.debug("Scheduler waiting %s seconds" % str(timeout)) |
160 self._cond.wait(timeout) |
167 self._cond.wait(timeout) |
161 snapshot = dreamtime.Snapshot() |
168 snapshot = dreamtime.Snapshot() |