scheduler.py

Wed, 24 Jan 2018 09:19:42 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 24 Jan 2018 09:19:42 +0000
changeset 60
10bd7e3906d9
parent 59
8d0a815022cc
child 69
8705e296c7a0
permissions
-rw-r--r--

py2app fails on several dependencies (xdg, pyyaml) that are satisfied
(pip reports installed), so remove them; need to be installed manually

49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Scheduler for Borgend
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 # This module simply provide a way for other threads to until a given time
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 #
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 import time
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 import borgend
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 from threading import Condition, Lock, Thread
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 logger=borgend.logger.getChild(__name__)
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
13 class QueuedEvent:
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
14 def __init__(self, cond, name=None):
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 self.next=None
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 self.prev=None
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 self.name=name
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
18 self.cond=cond
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 def __lt__(self, other):
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
21 raise NotImplementedError
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 def insert_after(self, ev):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 if not self.next:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 ev.prev=self
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 self.next=ev
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 ev.next=None
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
28 elif ev<self.next:
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 self.insert_immediately_after(ev)
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 else:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 self.next.insert_after(ev)
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 def insert_immediately_after(self, ev):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 ev.prev=self
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 ev.next=self.next
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 if ev.next:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 ev.next.prev=ev
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 self.next=ev
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 def unlink(self):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 n=self.next
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 p=self.prev
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 if n:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 n.prev=p
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 if p:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 p.next=n
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
48 class ScheduledEvent(QueuedEvent):
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
49 def __init__(self, when, cond, name=None):
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
50 super().__init__(cond, name=name)
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
51 self.when=when
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
52
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
53 def __lt__(self, other):
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
54 return self.when < other.when
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
55
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 class TerminableThread(Thread):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 def __init__(self, *args, **kwargs):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 super().__init__(*args, **kwargs)
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 self._terminate=False
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 self._cond=Condition()
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 def terminate(self):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 with self._cond:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 _terminate=True
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 self._cond.notify()
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
67 class QueueThread(TerminableThread):
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
68 def __init__(self, *args, **kwargs):
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
69 super().__init__(*args, **kwargs)
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
70 self.daemon = True
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
71 self._list = None
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
73 def _insert(self, ev):
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
74 if not self._list:
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
75 self._list=ev
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
76 elif ev<self._list:
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
77 ev.insert_immediately_after(self._list)
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
78 self._list=ev
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
79 else:
59
8d0a815022cc Oops, accidentally calling the wrong function (+log message clarification)
Tuomo Valkonen <tuomov@iki.fi>
parents: 55
diff changeset
80 self._list.insert_after(ev)
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
81
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
82 self._cond.notify()
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
83
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
84 def _unlink(self, ev):
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
85 if ev==self._list:
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
86 self._list=ev.next
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
87 ev.unlink()
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
88
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
89
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
90 class Scheduler(QueueThread):
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 # Default to precision of 60 seconds: the scheduler thread will never
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 # sleep longer than that, to get quickly back on track with the schedule
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93 # when the computer wakes up from sleep
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94 def __init__(self, precision=60):
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 self.precision = precision
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
96 self._next_event_time = None
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
97 super().__init__(target = self._scheduler_thread, name = 'Scheduler')
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
99 def _scheduler_thread(self):
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100 with self._cond:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 while not self._terminate:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 now = time.monotonic()
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
103 if not self._list:
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 timeout = None
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 else:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106 # Wait at most precision seconds, or until next event if it
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 # comes earlier
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
108 timeout=min(self.precision, self._list.when-now)
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
110 if not timeout or timeout>0:
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
111 self._cond.wait(timeout)
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
112 now = time.monotonic()
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
114 while self._list and self._list.when <= now:
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
115 ev=self._list
59
8d0a815022cc Oops, accidentally calling the wrong function (+log message clarification)
Tuomo Valkonen <tuomov@iki.fi>
parents: 55
diff changeset
116 logger.debug("Scheduler activating %s" % (ev.name or "(unknown)"))
54
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
117 # We are only allowed to remove ev from list when ev.cond allows
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
118 with ev.cond:
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
119 self._list=ev.next
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
120 ev.unlink()
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
121 ev.cond.notifyAll()
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
122
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
123 def _wait(self, ev):
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
124 with self._cond:
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
125 self._insert(ev)
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
126
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
127 # This will release the lock on cond, allowing queue manager (scheduler)
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
128 # thread to notify us if we are already to be released
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
129 ev.cond.wait()
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
130
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
131 # 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
132 # ensure the event is removed
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
133 with self._cond:
cfcaa5f6ba33 Basic repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 53
diff changeset
134 self._unlink(ev)
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 # cond has to be acquired on entry!
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137 def wait_until(self, when, cond, name=None):
53
442c558bd632 Generalisation of scheduler thread to general queue threads
Tuomo Valkonen <tuomov@iki.fi>
parents: 49
diff changeset
138 self._wait(ScheduledEvent(when, cond, name))
49
db33dfa64ad6 Improved scheduler
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139

mercurial