borgend/scheduler.py

changeset 102
0d43cd568f3c
parent 101
3068b0de12ee
child 103
32f2154ef25e
equal deleted inserted replaced
101:3068b0de12ee 102:0d43cd568f3c
6 # sleep periods 6 # sleep periods
7 # 7 #
8 8
9 import time 9 import time
10 import logging 10 import logging
11 import math
11 from threading import Condition, Thread 12 from threading import Condition, Thread
12 13
13 from . import dreamtime 14 from . import dreamtime
14 15
15 logger=logging.getLogger(__name__) 16 logger=logging.getLogger(__name__)
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()

mercurial