Sun, 21 Jan 2018 02:44:00 +0000
Sample configuration file added along with a few words of documentation
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # Borgend Backup instance |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
2 | 5 | import config |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
6 | import logging |
5 | 7 | import time |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
17
diff
changeset
|
8 | import keyring |
2 | 9 | from instance import BorgInstance |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
10 | from queue import Queue |
8 | 11 | from threading import Thread, Lock, Timer |
2 | 12 | |
6 | 13 | loglevel_translation={ |
14 | 'CRITICAL': logging.CRITICAL, | |
15 | 'ERROR': logging.ERROR, | |
16 | 'WARNING': logging.WARNING, | |
17 | 'DEBUG': logging.DEBUG, | |
18 | 'INFO': logging.INFO | |
19 | } | |
20 | ||
21 | def translate_loglevel(x): | |
22 | if x in loglevel_translation: | |
23 | return loglevel_translation[x] | |
24 | else: | |
25 | return logging.ERROR | |
26 | ||
15 | 27 | def safe_get_int(t, x): |
28 | if x in t: | |
29 | tmp=t[x] | |
30 | if isinstance(tmp, int): | |
31 | return tmp | |
32 | return None | |
33 | ||
34 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | class Backup: |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | |
2 | 37 | def __decode_config(self, cfg): |
38 | loc0='backup target %d' % self.identifier | |
39 | ||
40 | self.name=config.check_string(cfg, 'name', 'Name', loc0) | |
41 | ||
42 | self.loc='backup target "%s"' % self.name | |
43 | ||
44 | self.repository=config.check_string(cfg, 'repository', | |
45 | 'Target repository', self.loc) | |
46 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
47 | self.archive_prefix=config.check_string(cfg, 'archive_prefix', |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
48 | 'Archive prefix', self.loc) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
49 | |
2 | 50 | self.archive_template=config.check_string(cfg, 'archive_template', |
51 | 'Archive template', self.loc) | |
52 | ||
53 | self.backup_interval=config.check_nonneg_int(cfg, 'backup_interval', | |
54 | 'Backup interval', self.loc, | |
55 | config.defaults['backup_interval']) | |
56 | ||
57 | self.retry_interval=config.check_nonneg_int(cfg, 'retry_interval', | |
58 | 'Retry interval', self.loc, | |
59 | config.defaults['retry_interval']) | |
60 | ||
61 | self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc) | |
62 | ||
6 | 63 | self.common_parameters=config.check_list_of_dicts(cfg, 'common_parameters', |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
64 | 'Borg parameters', self.loc, |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
65 | default=[]) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
66 | |
6 | 67 | self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters', |
68 | 'Create parameters', self.loc, | |
69 | default=[]) | |
70 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
71 | self.prune_parameters=config.check_list_of_dicts(cfg, 'prune_parameters', |
6 | 72 | 'Prune parameters', self.loc, |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
73 | default=[]) |
2 | 74 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
75 | self.__keychain_account=config.check_string(cfg, 'keychain_account', |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
76 | 'Keychain account', self.loc, |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
77 | default='') |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
78 | |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
79 | if config.settings['__extract_passphrases_at_startup']: |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
80 | self.extract_passphrase() |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
17
diff
changeset
|
81 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
82 | def extract_passphrase(self): |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
83 | acc=self.__keychain_account |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
84 | if acc and acc!='': |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
85 | pw=keyring.get_password("borg-backup", acc) |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
86 | self.__passphrase=pw |
20
fdfbe5d7b677
Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents:
17
diff
changeset
|
87 | else: |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
88 | self.__passphrase=None |
2 | 89 | |
90 | def __init__(self, identifier, cfg): | |
91 | self.identifier=identifier | |
92 | ||
93 | self.__decode_config(cfg) | |
94 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
95 | self.config=config |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
96 | self.lastrun=None |
5 | 97 | self.lastrun_success=None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
98 | self.borg_instance=None |
5 | 99 | self.current_operation=None |
7 | 100 | self.thread_log=None |
8 | 101 | self.thread_res=None |
102 | self.timer=None | |
10 | 103 | self.scheduled_operation=None |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
104 | self.lock=Lock() |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
105 | self.__status_update_callback=None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
106 | |
7 | 107 | def is_running(self): |
108 | with self.lock: | |
8 | 109 | running=self.__is_running_unlocked() |
110 | return running | |
111 | ||
112 | def __is_running_unlocked(self): | |
113 | running=self.current_operation | |
114 | if not running: | |
115 | # Consistency check | |
116 | assert((not self.borg_instance and not self.thread_log and | |
117 | not self.thread_res)) | |
7 | 118 | return running |
119 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
120 | def __block_when_running(self): |
7 | 121 | running=self.is_running() |
122 | assert(not running) | |
2 | 123 | |
7 | 124 | def __log_listener(self): |
125 | logging.debug('Log listener thread waiting for entries') | |
126 | success=True | |
127 | for status in iter(self.borg_instance.read_log, None): | |
128 | logging.debug(str(status)) | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
129 | t=status['type'] |
15 | 130 | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
131 | errors_this_message=None |
15 | 132 | callback=None |
133 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
134 | if t=='progress_percent': |
15 | 135 | current=safe_get_int(status, 'current') |
136 | total=safe_get_int(status, 'total') | |
137 | if current is not None and total is not None: | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
138 | with self.lock: |
17 | 139 | self.current_operation['progress_current']=current |
140 | self.current_operation['progress_total']=total | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
141 | status, callback=self.__status_unlocked() |
15 | 142 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
143 | elif t=='archive_progress': |
15 | 144 | original_size=safe_get_int(status, 'original_size') |
145 | compressed_size=safe_get_int(status, 'compressed_size') | |
146 | deduplicated_size=safe_get_int(status, 'deduplicated_size') | |
147 | if original_size is not None and original_size is not None and deduplicated_size is not None: | |
148 | with self.lock: | |
17 | 149 | self.current_operation['original_size']=original_size |
150 | self.current_operation['compressed_size']=compressed_size | |
151 | self.current_operation['deduplicated_size']=deduplicated_size | |
15 | 152 | status, callback=self.__status_unlocked() |
153 | ||
154 | elif t=='progress_message': | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
155 | pass |
15 | 156 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
157 | elif t=='file_status': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
158 | pass |
15 | 159 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
160 | elif t=='log_message': |
6 | 161 | if 'levelname' not in status: |
162 | status['levelname']='ERROR' | |
163 | if 'message' not in status: | |
164 | status['message']='UNKNOWN' | |
165 | if 'name' not in status: | |
166 | status['name']='borg' | |
15 | 167 | lvl=translate_loglevel(status['levelname']) |
168 | logging.log(lvl, status['name'] + ': ' + status['message']) | |
169 | if lvl>=logging.WARNING: | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
170 | errors_this_message=status |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
171 | with self.lock: |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
172 | self.current_operation['errors']=True |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
173 | status, callback=self.__status_unlocked() |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
174 | else: |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
175 | logging.debug('Unrecognised log entry %s' % str(status)) |
15 | 176 | |
177 | if callback: | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
178 | callback(self, status, errors=errors_this_message) |
6 | 179 | |
7 | 180 | logging.debug('Waiting for borg subprocess to terminate in log thread') |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
181 | |
6 | 182 | self.borg_instance.wait() |
183 | ||
7 | 184 | logging.debug('Borg subprocess terminated; terminating log listener thread') |
185 | ||
186 | def __result_listener(self): | |
10 | 187 | with self.lock: |
188 | status, callback=self.__status_unlocked() | |
189 | if callback: | |
190 | callback(self, status) | |
191 | ||
7 | 192 | logging.debug('Result listener thread waiting for result') |
193 | ||
194 | res=self.borg_instance.read_result() | |
195 | ||
196 | success=True | |
197 | ||
198 | logging.debug('Borg result: %s' % str(res)) | |
199 | ||
200 | if res==None: | |
201 | success=False | |
202 | ||
203 | logging.debug('Waiting for borg subprocess to terminate in result thread') | |
204 | ||
8 | 205 | success=success and self.borg_instance.wait() |
7 | 206 | |
8 | 207 | logging.debug('Borg subprocess terminated (success: %s); terminating result listener thread' % str(success)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
208 | |
12 | 209 | self.thread_log.join() |
210 | ||
5 | 211 | with self.lock: |
10 | 212 | if self.current_operation['operation']=='create': |
213 | self.lastrun=self.current_operation['when_monotonic'] | |
5 | 214 | self.lastrun_success=success |
7 | 215 | self.thread_res=None |
12 | 216 | self.thread_log=None |
217 | self.borg_instance=None | |
218 | self.current_operation=None | |
219 | self.__schedule_unlocked() | |
10 | 220 | status, callback=self.__status_unlocked() |
221 | if callback: | |
222 | callback(self, status) | |
7 | 223 | |
10 | 224 | def __do_launch(self, queue, op, archive_or_repository, *args): |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
225 | self.extract_passphrase() |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
226 | |
10 | 227 | inst=BorgInstance(op['operation'], archive_or_repository, *args) |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
228 | inst.launch(passphrase=self.__passphrase) |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
229 | |
7 | 230 | t_log=Thread(target=self.__log_listener) |
231 | t_log.daemon=True | |
2 | 232 | |
7 | 233 | t_res=Thread(target=self.__result_listener) |
234 | t_res.daemon=True | |
235 | ||
236 | self.thread_log=t_log | |
237 | self.thread_res=t_res | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
238 | self.borg_instance=inst |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
239 | self.queue=queue |
10 | 240 | self.current_operation=op |
241 | self.current_operation['when_monotonic']=time.monotonic() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
242 | |
7 | 243 | t_log.start() |
244 | t_res.start() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
245 | |
10 | 246 | def __launch(self, op, queue): |
8 | 247 | if self.__is_running_unlocked(): |
248 | logging.info('Cannot start %s: already running %s' | |
249 | % (operation, self.current_operation)) | |
250 | return False | |
251 | else: | |
252 | if self.timer: | |
253 | logging.debug('Unscheduling timed operation due to launch of operation') | |
254 | self.timer=None | |
10 | 255 | self.scheduled_operation=None |
8 | 256 | |
10 | 257 | logging.debug("Launching '%s' on '%s'" % (op['operation'], self.name)) |
8 | 258 | |
10 | 259 | if op['operation']=='create': |
8 | 260 | archive="%s::%s%s" % (self.repository, |
261 | self.archive_prefix, | |
262 | self.archive_template) | |
2 | 263 | |
10 | 264 | self.__do_launch(queue, op, archive, |
8 | 265 | self.common_parameters+self.create_parameters, |
266 | self.paths) | |
10 | 267 | elif op['operation']=='prune': |
268 | self.__do_launch(queue, op, self.repository, | |
8 | 269 | ([{'prefix': self.archive_prefix}] + |
270 | self.common_parameters + | |
271 | self.prune_parameters)) | |
272 | else: | |
10 | 273 | logging.error("Invalid operaton '%s'" % op['operation']) |
8 | 274 | self.__schedule_unlocked() |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
275 | |
8 | 276 | return True |
277 | ||
278 | def create(self, queue): | |
10 | 279 | op={'operation': 'create', 'detail': 'manual'} |
8 | 280 | with self.lock: |
10 | 281 | res=self.__launch(op, queue) |
8 | 282 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
283 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
284 | def prune(self, queue): |
10 | 285 | op={'operation': 'prune', 'detail': 'manual'} |
8 | 286 | with self.lock: |
10 | 287 | res=self.__launch(op, queue) |
8 | 288 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
289 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
290 | # TODO: Decide exact (manual) abort mechanism. Perhaps two stages |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
291 | def abort(self): |
5 | 292 | with self.lock: |
293 | if self.borg_instance: | |
294 | self.borg_instance.terminate() | |
7 | 295 | thread_log=self.thread_log |
296 | thread_res=self.thread_res | |
297 | ||
298 | if thread_log: | |
299 | thread_log.terminate() | |
300 | ||
301 | if thread_res: | |
302 | thread_res.terminate() | |
303 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
304 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
305 | def join(self): |
8 | 306 | logging.debug('Waiting for borg listener threads to terminate') |
7 | 307 | |
308 | with self.lock: | |
309 | thread_log=self.thread_log | |
310 | thread_res=self.thread_res | |
311 | ||
312 | if thread_log: | |
313 | thread_log.join() | |
314 | ||
315 | if thread_res: | |
316 | thread_res.join() | |
317 | ||
318 | assert(self.thread_log==None and self.thread_res==None) | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
319 | |
8 | 320 | def __queue_timed_operation(self): |
321 | with self.lock: | |
10 | 322 | op=self.scheduled_operation |
323 | self.scheduled_operation=None | |
8 | 324 | self.timer=None |
325 | ||
326 | if self.__is_running_unlocked(): | |
327 | logging.info('Aborted queue operation, as an operation is already running') | |
328 | else: | |
329 | # TODO: Queue on 'repository' and online status for SSH, etc. | |
330 | ||
331 | # TODO: UI comms. queue? | |
10 | 332 | self.__launch(op, None) |
8 | 333 | |
334 | def __next_operation_unlocked(self): | |
335 | # TODO: pruning as well | |
5 | 336 | now=time.monotonic() |
337 | if not self.lastrun: | |
10 | 338 | initial_interval=self.retry_interval |
339 | if initial_interval==0: | |
340 | initial_interval=self.backup_interval | |
341 | if initial_interval==0: | |
342 | return None | |
343 | else: | |
344 | return {'operation': 'create', | |
345 | 'detail': 'initial', | |
346 | 'when_monotonic': now+initial_interval} | |
5 | 347 | elif not self.lastrun_success: |
10 | 348 | if self.retry_interval==0: |
349 | return None | |
350 | else: | |
351 | return {'operation': 'create', | |
352 | 'detail': 'retry', | |
353 | 'when_monotonic': self.lastrun+self.retry_interval} | |
5 | 354 | else: |
355 | if self.backup_interval==0: | |
10 | 356 | return None |
5 | 357 | else: |
10 | 358 | return {'operation': 'create', |
359 | 'detail': None, | |
360 | 'when_monotonic': self.lastrun+self.backup_interval} | |
361 | ||
362 | def __schedule_unlocked(self): | |
363 | if self.current_operation: | |
364 | return self.current_operation | |
365 | else: | |
366 | op=self.__next_operation_unlocked() | |
367 | ||
368 | if op: | |
369 | now=time.monotonic() | |
370 | delay=max(0, op['when_monotonic']-now) | |
371 | logging.info("Scheduling '%s' (detail: %s) of '%s' in %d seconds" % | |
372 | (op['operation'], op['detail'], self.name, delay)) | |
373 | tmr=Timer(delay, self.__queue_timed_operation) | |
374 | self.scheduled_operation=op | |
375 | self.timer=tmr | |
376 | tmr.start() | |
377 | ||
378 | return op | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
379 | |
8 | 380 | def schedule(self): |
381 | with self.lock: | |
382 | return self.__schedule_unlocked() | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
383 | |
10 | 384 | def status(self): |
385 | with self.lock: | |
386 | res=self.__status_unlocked() | |
387 | return res[0] | |
388 | ||
389 | def __status_unlocked(self): | |
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
390 | callback=self.__status_update_callback |
10 | 391 | if self.current_operation: |
392 | status=self.current_operation | |
393 | status['type']='current' | |
394 | elif self.scheduled_operation: | |
395 | status=self.scheduled_operation | |
396 | status['type']='scheduled' | |
397 | else: | |
398 | status={'type': 'nothing'} | |
399 | ||
400 | status['name']=self.name | |
401 | ||
15 | 402 | if 'errors' not in status: |
403 | status['errors']=False | |
404 | ||
10 | 405 | if 'when_monotonic' in status: |
406 | status['when']=(status['when_monotonic'] | |
407 | -time.monotonic()+time.time()) | |
408 | ||
409 | return status, callback | |
410 | ||
21
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
411 | def set_status_update_callback(self, callback): |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
412 | with self.lock: |
c36e549a7f12
Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
20
diff
changeset
|
413 | self.__status_update_callback=callback |
10 | 414 | |
415 |