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