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