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