borgend/scheduler.py

changeset 102
0d43cd568f3c
parent 101
3068b0de12ee
child 103
32f2154ef25e
--- a/borgend/scheduler.py	Sun Feb 04 01:27:38 2018 +0000
+++ b/borgend/scheduler.py	Sun Feb 04 01:36:59 2018 +0000
@@ -8,6 +8,7 @@
 
 import time
 import logging
+import math
 from threading import Condition, Thread
 
 from . import dreamtime
@@ -132,10 +133,15 @@
 
 
 class Scheduler(QueueThread):
-    # Default to precision of 60 seconds: the scheduler thread will never
-    # sleep longer than that, to get quickly back on track with the schedule
-    # when the computer wakes up from sleep
-    def __init__(self, precision=60):
+    # Default to precision of 5 minutes: if there are finite-horizon events in
+    # the queue, the scheduler thread will never sleep longer than that.
+    # This is to quickly get back on track with the schedule when the computer
+    # wakes up from sleep, if the sleep monitor is not working or is not
+    # implemented for the particular operating system. However, if there are
+    # only infinite-horizon events in the queue (meaning, DreamTime-scheduled
+    # events, and the system is sleeping or "sleeping"), the scheduler will
+    # also sleep. Hopefully this will also help the system stay properly asleep.
+    def __init__(self, precision=60*5):
         self.precision = precision
         self._next_event_time = None
         super().__init__(target = self._scheduler_thread, name = 'Scheduler')
@@ -150,10 +156,11 @@
                 if not self._list:
                     timeout = None
                 else:
-                    # Wait at most precision seconds, or until next event if it
-                    # comes earlier
-                    delta = self._list.when.monotonic(snapshot)-now
-                    timeout = min(self.precision, delta)
+                    delta = self._list.when.horizon(snapshot)-now
+                    if delta==math.inf:
+                        timeout=None
+                    else:
+                        timeout = min(self.precision, delta)
 
                 if not timeout or timeout>0:
                     logger.debug("Scheduler waiting %s seconds" % str(timeout))

mercurial