Wed, 24 Jan 2018 22:04:16 +0000
Turned Status and Operation into classes instead of dictionaries
| 49 | 1 | # |
| 2 | # Scheduler for Borgend | |
| 3 | # | |
| 4 | # This module simply provide a way for other threads to until a given time | |
| 5 | # | |
| 6 | ||
| 7 | import time | |
| 8 | import borgend | |
| 9 | from threading import Condition, Lock, Thread | |
| 10 | ||
| 11 | logger=borgend.logger.getChild(__name__) | |
| 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 | 15 | self.next=None |
| 16 | self.prev=None | |
| 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 | 19 | |
| 20 | def __lt__(self, other): | |
| 54 | 21 | raise NotImplementedError |
| 49 | 22 | |
| 23 | def insert_after(self, ev): | |
| 24 | if not self.next: | |
| 25 | ev.prev=self | |
| 26 | self.next=ev | |
| 27 | ev.next=None | |
| 54 | 28 | elif ev<self.next: |
| 49 | 29 | self.insert_immediately_after(ev) |
| 30 | else: | |
| 31 | self.next.insert_after(ev) | |
| 32 | ||
| 33 | def insert_immediately_after(self, ev): | |
| 34 | ev.prev=self | |
| 35 | ev.next=self.next | |
| 36 | if ev.next: | |
| 37 | ev.next.prev=ev | |
| 38 | self.next=ev | |
| 39 | ||
| 40 | def unlink(self): | |
| 41 | n=self.next | |
| 42 | p=self.prev | |
| 43 | if n: | |
| 44 | n.prev=p | |
| 45 | if p: | |
| 46 | p.next=n | |
| 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 | 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 | 56 | class TerminableThread(Thread): |
| 57 | def __init__(self, *args, **kwargs): | |
| 58 | super().__init__(*args, **kwargs) | |
| 59 | self._terminate=False | |
| 60 | self._cond=Condition() | |
| 61 | ||
| 62 | def terminate(self): | |
| 63 | with self._cond: | |
| 64 | _terminate=True | |
| 65 | self._cond.notify() | |
| 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 | 69 | super().__init__(*args, **kwargs) |
| 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 | 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 | 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 | 84 | def _unlink(self, ev): |
| 85 | if ev==self._list: | |
| 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 | 91 | # Default to precision of 60 seconds: the scheduler thread will never |
| 92 | # sleep longer than that, to get quickly back on track with the schedule | |
| 93 | # when the computer wakes up from sleep | |
| 94 | def __init__(self, precision=60): | |
| 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 | 97 | super().__init__(target = self._scheduler_thread, name = 'Scheduler') |
| 49 | 98 | |
| 54 | 99 | def _scheduler_thread(self): |
| 49 | 100 | with self._cond: |
| 101 | while not self._terminate: | |
| 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 | 104 | timeout = None |
| 105 | else: | |
| 106 | # Wait at most precision seconds, or until next event if it | |
| 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 | 109 | |
| 110 | if not timeout or timeout>0: | |
| 111 | self._cond.wait(timeout) | |
| 112 | now = time.monotonic() | |
| 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 | 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 | 117 | # We are only allowed to remove ev from list when ev.cond allows |
| 118 | with ev.cond: | |
| 119 | self._list=ev.next | |
| 120 | ev.unlink() | |
| 121 | ev.cond.notifyAll() | |
| 122 | ||
| 123 | def _wait(self, ev): | |
| 124 | with self._cond: | |
| 125 | self._insert(ev) | |
| 126 | ||
| 127 | # This will release the lock on cond, allowing queue manager (scheduler) | |
| 128 | # thread to notify us if we are already to be released | |
| 129 | ev.cond.wait() | |
| 130 | ||
| 131 | # If we were woken up by some other event, not the scheduler, | |
| 132 | # ensure the event is removed | |
| 133 | with self._cond: | |
| 134 | self._unlink(ev) | |
| 49 | 135 | |
| 136 | # cond has to be acquired on entry! | |
| 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 | 139 |