borgend/scheduler.py

Sun, 04 Feb 2018 01:36:59 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 04 Feb 2018 01:36:59 +0000
changeset 102
0d43cd568f3c
parent 101
3068b0de12ee
child 103
32f2154ef25e
permissions
-rw-r--r--

Changed scheduler busylooping method, and decreased precision to 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.

49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
89
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
2 # Borgend by Tuomo Valkonen, 2018
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #
89
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
4 # This file is the scheduler: it provides e a way for other threads to
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
5 # wait until a given time; which may be "dreamtime" that discounts system
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
6 # sleep periods
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 #
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 import time
86
2fe66644c50d Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 80
diff changeset
10 import logging
102
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
11 import math
80
a409242121d5 Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 79
diff changeset
12 from threading import Condition, Thread
a409242121d5 Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 79
diff changeset
13
a409242121d5 Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 79
diff changeset
14 from . import dreamtime
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15
86
2fe66644c50d Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 80
diff changeset
16 logger=logging.getLogger(__name__)
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
18 class QueuedEvent:
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
19 def __init__(self, cond, name=None):
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 self.next=None
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 self.prev=None
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 self.name=name
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
23 self.cond=cond
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
24 self.linked=False
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
26 @staticmethod
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
27 def snapshot():
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
28 return None
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
29
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
30 def is_before(self, other, snapshot=None):
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
31 raise NotImplementedError
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
33 def __lt__(self, other):
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
34 return self.is_before(other, self.snapshot())
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
35
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
36 def insert_after(self, ev, snapshot=None):
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
37 if not snapshot:
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
38 snapshot=self.snapshot()
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
39 if not self.next or ev.is_before(self.next, snapshot):
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 self.insert_immediately_after(ev)
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 else:
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
42 self.next.insert_after(ev, snapshot)
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 def insert_immediately_after(self, ev):
69
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
45 assert(ev.next is None and ev.prev is None)
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 ev.prev=self
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 ev.next=self.next
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 self.next=ev
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49
69
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
50 def insert_immediately_before(self, ev):
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
51 assert(ev.next is None and ev.prev is None)
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
52 ev.next=self
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
53 ev.prev=self.prev
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
54 self.prev=ev
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
55
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 def unlink(self):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 n=self.next
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 p=self.prev
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 if n:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 n.prev=p
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 if p:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 p.next=n
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
63 self.next=None
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
64 self.prev=None
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
66 class ScheduledEvent(QueuedEvent):
78
83b43987e61e Renamed the "sleep" module "dreamtime"
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
67 #@accepts(ScheduledEvent, dreamtime.Time, threading.Cond, str)
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
68 def __init__(self, when, cond, name=None):
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
69 super().__init__(cond, name=name)
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
70 self.when=when
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
71
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
72 @staticmethod
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
73 def snapshot():
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
74 return dreamtime.Snapshot()
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
75
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
76 def is_before(self, other, snapshot=None):
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
77 if not snapshot:
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
78 snapshot=self.snapshot()
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
79 return self.when.horizon(snapshot) < other.when.horizon(snapshot)
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
80
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 class TerminableThread(Thread):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 def __init__(self, *args, **kwargs):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 super().__init__(*args, **kwargs)
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 self._terminate=False
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 self._cond=Condition()
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 def terminate(self):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 with self._cond:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89 _terminate=True
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 self._cond.notify()
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
92 class QueueThread(TerminableThread):
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
93 def __init__(self, *args, **kwargs):
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
94 super().__init__(*args, **kwargs)
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
95 self.daemon = True
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
96 self._list = None
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
98 def _insert(self, ev, snapshot=None):
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
99 assert(not ev.linked)
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
100 if not self._list:
75
2a44b9649212 UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
101 #logger.debug("Insert first")
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
102 self._list=ev
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
103 elif ev<self._list:
75
2a44b9649212 UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
104 #logger.debug("Insert beginning")
69
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 59
diff changeset
105 self._list.insert_immediately_before(ev)
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
106 self._list=ev
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
107 else:
75
2a44b9649212 UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
108 #logger.debug("Insert after")
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
109 if not snapshot:
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
110 snapshot=ev.snapshot()
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
111 self._list.insert_after(ev, snapshot)
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
112 ev.linked=True
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
113
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
114 def _unlink(self, ev):
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
115 assert(ev.linked)
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
116 if ev==self._list:
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
117 self._list=ev.next
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
118 ev.unlink()
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
119 ev.linked=False
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
120
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
121 def _resort(self, snapshot=None):
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
122 oldlist=self._list
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
123 self._list=None
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
124 if oldlist and not snapshot:
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
125 snapshot=oldlist.snapshot()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
126 while oldlist:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
127 ev=oldlist
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
128 oldlist=oldlist.next
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
129 ev.unlink()
91
f53aa2007a84 Oops; _resort needs to mark ev.linked=False to avoid assertion errors in _insert.
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
130 ev.linked=False
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
131 self._insert(ev, snapshot)
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
132
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
133
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
134
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
135 class Scheduler(QueueThread):
102
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
136 # Default to precision of 5 minutes: if there are finite-horizon events in
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
137 # the queue, the scheduler thread will never sleep longer than that.
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
138 # This is to quickly get back on track with the schedule when the computer
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
139 # wakes up from sleep, if the sleep monitor is not working or is not
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
140 # implemented for the particular operating system. However, if there are
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
141 # only infinite-horizon events in the queue (meaning, DreamTime-scheduled
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
142 # events, and the system is sleeping or "sleeping"), the scheduler will
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
143 # also sleep. Hopefully this will also help the system stay properly asleep.
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
144 def __init__(self, precision=60*5):
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 self.precision = precision
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
146 self._next_event_time = None
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
147 super().__init__(target = self._scheduler_thread, name = 'Scheduler')
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
148 dreamtime.add_callback(self, self._sleepwake_callback)
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
149
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
150 def _scheduler_thread(self):
75
2a44b9649212 UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
151 logger.debug("Scheduler thread started")
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152 with self._cond:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 while not self._terminate:
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
154 snapshot = dreamtime.Snapshot()
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
155 now = snapshot.monotonic()
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
156 if not self._list:
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 timeout = None
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158 else:
102
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
159 delta = self._list.when.horizon(snapshot)-now
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
160 if delta==math.inf:
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
161 timeout=None
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
162 else:
0d43cd568f3c Changed scheduler busylooping method, and decreased precision to 5 minutes:
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
163 timeout = min(self.precision, delta)
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 if not timeout or timeout>0:
86
2fe66644c50d Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 80
diff changeset
166 logger.debug("Scheduler waiting %s seconds" % str(timeout))
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 self._cond.wait(timeout)
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
168 snapshot = dreamtime.Snapshot()
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
169 now = snapshot.monotonic()
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170
75
2a44b9649212 UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
171 logger.debug("Scheduler timed out")
2a44b9649212 UI refresh fix; added debug messages
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
172
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
173 while self._list and self._list.when.horizon(snapshot) <= now:
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
174 ev=self._list
59
8d0a815022cc Oops, accidentally calling the wrong function (+log message clarification)
Tuomo Valkonen <tuomov@iki.fi>
parents: 55
diff changeset
175 logger.debug("Scheduler activating %s" % (ev.name or "(unknown)"))
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
176 # We are only allowed to remove ev from list when ev.cond allows
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
177 self._unlink(ev)
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
178 # We need to release the lock on self._cond before acquire
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
179 # one ev.cond to avoid race conditions with self._wait
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
180 self._cond.release()
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
181 with ev.cond:
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
182 ev.cond.notify_all()
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
183 self._cond.acquire()
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
184
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
185
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
186 def _sleepwake_callback(self, woke):
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 91
diff changeset
187 logger.debug("Rescheduling events after sleep/wakeup")
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
188 with self._cond:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
189 self._resort()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
190
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
191 # It is required to have acquired the lock on ev.cond on entry
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
192 def _wait(self, ev):
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
193 with self._cond:
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
194 self._insert(ev)
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
195 self._cond.notify()
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
196
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
197 # This will release the lock on cond, allowing the scheduler
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
198 # thread to notify us if we are ready to be released
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
199 ev.cond.wait()
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
200
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
201 # If we were woken up by some other event, not the scheduler,
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
202 # ensure the event is removed
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
203 if ev.linked:
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
204 # Deal with race conditions wrt. the two different locks
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
205 # in the scheduler
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
206 #ev.cond.release()
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
207 with self._cond:
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
208 self._unlink(ev)
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
209 #ev.cond.acquire()
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 # cond has to be acquired on entry!
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212 def wait_until(self, when, cond, name=None):
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
213 logger.debug("Scheduling '%s' in %s seconds [%s]" %
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 75
diff changeset
214 (name, when.seconds_to(), when.__class__.__name__))
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
215 self._wait(ScheduledEvent(when, cond, name))
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216

mercurial