Sat, 20 Jan 2018 15:08:16 +0000
basic scheduling
|
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 |
| 2 | 8 | from instance import BorgInstance |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
9 | from queue import Queue |
| 8 | 10 | from threading import Thread, Lock, Timer |
| 2 | 11 | |
| 6 | 12 | loglevel_translation={ |
| 13 | 'CRITICAL': logging.CRITICAL, | |
| 14 | 'ERROR': logging.ERROR, | |
| 15 | 'WARNING': logging.WARNING, | |
| 16 | 'DEBUG': logging.DEBUG, | |
| 17 | 'INFO': logging.INFO | |
| 18 | } | |
| 19 | ||
| 20 | def translate_loglevel(x): | |
| 21 | if x in loglevel_translation: | |
| 22 | return loglevel_translation[x] | |
| 23 | else: | |
| 24 | return logging.ERROR | |
| 25 | ||
|
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | class Backup: |
|
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | |
| 2 | 28 | def __decode_config(self, cfg): |
| 29 | loc0='backup target %d' % self.identifier | |
| 30 | ||
| 31 | self.name=config.check_string(cfg, 'name', 'Name', loc0) | |
| 32 | ||
| 33 | self.loc='backup target "%s"' % self.name | |
| 34 | ||
| 35 | self.repository=config.check_string(cfg, 'repository', | |
| 36 | 'Target repository', self.loc) | |
| 37 | ||
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
38 | 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
|
39 | 'Archive prefix', self.loc) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
40 | |
| 2 | 41 | self.archive_template=config.check_string(cfg, 'archive_template', |
| 42 | 'Archive template', self.loc) | |
| 43 | ||
| 44 | self.backup_interval=config.check_nonneg_int(cfg, 'backup_interval', | |
| 45 | 'Backup interval', self.loc, | |
| 46 | config.defaults['backup_interval']) | |
| 47 | ||
| 48 | self.retry_interval=config.check_nonneg_int(cfg, 'retry_interval', | |
| 49 | 'Retry interval', self.loc, | |
| 50 | config.defaults['retry_interval']) | |
| 51 | ||
| 52 | self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc) | |
| 53 | ||
| 6 | 54 | 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
|
55 | 'Borg parameters', self.loc, |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
56 | default=[]) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
57 | |
| 6 | 58 | self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters', |
| 59 | 'Create parameters', self.loc, | |
| 60 | default=[]) | |
| 61 | ||
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
62 | self.prune_parameters=config.check_list_of_dicts(cfg, 'prune_parameters', |
| 6 | 63 | 'Prune parameters', self.loc, |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
64 | default=[]) |
| 2 | 65 | |
| 66 | ||
| 67 | def __init__(self, identifier, cfg): | |
| 68 | self.identifier=identifier | |
| 69 | ||
| 70 | self.__decode_config(cfg) | |
| 71 | ||
|
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | self.config=config |
|
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
73 | self.lastrun=None |
| 5 | 74 | self.lastrun_success=None |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
75 | self.borg_instance=None |
| 5 | 76 | self.current_operation=None |
| 7 | 77 | self.thread_log=None |
| 8 | 78 | self.thread_res=None |
| 79 | self.timer=None | |
| 80 | self.timer_operation=None | |
| 81 | self.timer_time=None | |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
82 | self.lock=Lock() |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
83 | |
| 7 | 84 | def is_running(self): |
| 85 | with self.lock: | |
| 8 | 86 | running=self.__is_running_unlocked() |
| 87 | return running | |
| 88 | ||
| 89 | def __is_running_unlocked(self): | |
| 90 | running=self.current_operation | |
| 91 | if not running: | |
| 92 | # Consistency check | |
| 93 | assert((not self.borg_instance and not self.thread_log and | |
| 94 | not self.thread_res)) | |
| 7 | 95 | return running |
| 96 | ||
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
97 | def __block_when_running(self): |
| 7 | 98 | running=self.is_running() |
| 99 | assert(not running) | |
| 2 | 100 | |
| 7 | 101 | def __log_listener(self): |
| 102 | logging.debug('Log listener thread waiting for entries') | |
| 103 | success=True | |
| 104 | for status in iter(self.borg_instance.read_log, None): | |
| 105 | logging.debug(str(status)) | |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
106 | t=status['type'] |
| 7 | 107 | #may_indicate_finished=False |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
108 | if t=='progress_percent': |
| 7 | 109 | #may_indicate_finished=True |
| 110 | # Temporary output | |
| 111 | if 'current' not in status: | |
| 112 | status['current']=0 | |
| 113 | if 'total' not in status: | |
| 114 | status['total']=0 | |
| 115 | print('%d / %d' % (status['current'], status['total'])) | |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
116 | elif t=='archive_progress': |
|
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
117 | pass |
|
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
118 | elif t=='progress_message': |
| 7 | 119 | #may_indicate_finished=True |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
120 | pass |
|
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
121 | elif t=='file_status': |
|
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
122 | pass |
|
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
123 | elif t=='log_message': |
| 6 | 124 | if 'levelname' not in status: |
| 125 | status['levelname']='ERROR' | |
| 126 | if 'message' not in status: | |
| 127 | status['message']='UNKNOWN' | |
| 128 | if 'name' not in status: | |
| 129 | status['name']='borg' | |
| 130 | logging.log(translate_loglevel(status['levelname']), | |
| 131 | status['name'] + ': ' + status['message']) | |
| 7 | 132 | # set success=False? |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
133 | elif t=='exception': |
| 7 | 134 | success=False |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
135 | elif t=='unparsed_error': |
| 7 | 136 | success=False |
| 6 | 137 | |
| 7 | 138 | 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
|
139 | |
| 6 | 140 | self.borg_instance.wait() |
| 141 | ||
| 7 | 142 | logging.debug('Borg subprocess terminated; terminating log listener thread') |
| 143 | ||
| 144 | with self.lock: | |
| 145 | self.thread_log=None | |
| 8 | 146 | self.__finish_and_reschedule_if_both_listeners_terminated() |
| 7 | 147 | |
| 148 | ||
| 149 | def __result_listener(self): | |
| 150 | logging.debug('Result listener thread waiting for result') | |
| 151 | ||
| 152 | res=self.borg_instance.read_result() | |
| 153 | ||
| 154 | success=True | |
| 155 | ||
| 156 | logging.debug('Borg result: %s' % str(res)) | |
| 157 | ||
| 158 | if res==None: | |
| 159 | success=False | |
| 160 | ||
| 161 | logging.debug('Waiting for borg subprocess to terminate in result thread') | |
| 162 | ||
| 8 | 163 | success=success and self.borg_instance.wait() |
| 7 | 164 | |
| 8 | 165 | 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
|
166 | |
| 5 | 167 | with self.lock: |
| 168 | if self.current_operation=='create': | |
| 169 | self.lastrun=self.time_started | |
| 170 | self.lastrun_success=success | |
| 7 | 171 | self.thread_res=None |
| 8 | 172 | self.__finish_and_reschedule_if_both_listeners_terminated() |
| 7 | 173 | |
| 8 | 174 | def __finish_and_reschedule_if_both_listeners_terminated(self): |
| 7 | 175 | if self.thread_res==None and self.thread_log==None: |
| 176 | logging.debug('Both threads terminated') | |
| 5 | 177 | self.borg_instance=None |
| 8 | 178 | self.time_started=None |
| 5 | 179 | self.current_operation=None |
| 8 | 180 | self.__schedule_unlocked() |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
181 | |
| 8 | 182 | def __do_launch(self, queue, operation, archive_or_repository, *args): |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
183 | inst=BorgInstance(operation, archive_or_repository, *args) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
184 | inst.launch() |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
185 | |
| 7 | 186 | t_log=Thread(target=self.__log_listener) |
| 187 | t_log.daemon=True | |
| 2 | 188 | |
| 7 | 189 | t_res=Thread(target=self.__result_listener) |
| 190 | t_res.daemon=True | |
| 191 | ||
| 192 | self.thread_log=t_log | |
| 193 | self.thread_res=t_res | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
194 | self.borg_instance=inst |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
195 | self.queue=queue |
| 5 | 196 | self.current_operation=operation |
| 197 | self.time_started=time.monotonic() | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
198 | |
| 7 | 199 | t_log.start() |
| 200 | t_res.start() | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
201 | |
| 8 | 202 | def __launch(self, operation, queue): |
| 203 | if self.__is_running_unlocked(): | |
| 204 | logging.info('Cannot start %s: already running %s' | |
| 205 | % (operation, self.current_operation)) | |
| 206 | return False | |
| 207 | else: | |
| 208 | if self.timer: | |
| 209 | logging.debug('Unscheduling timed operation due to launch of operation') | |
| 210 | self.timer=None | |
| 211 | self.timer_operation=None | |
| 212 | self.timer_time=None | |
| 213 | ||
| 214 | logging.debug("Launching '%s' on '%s'" % (operation, self.name)) | |
| 215 | ||
| 216 | if operation=='create': | |
| 217 | archive="%s::%s%s" % (self.repository, | |
| 218 | self.archive_prefix, | |
| 219 | self.archive_template) | |
| 2 | 220 | |
| 8 | 221 | self.__do_launch(queue, operation, archive, |
| 222 | self.common_parameters+self.create_parameters, | |
| 223 | self.paths) | |
| 224 | elif operation=='prune': | |
| 225 | self.__do_launch(queue, 'prune', self.repository, | |
| 226 | ([{'prefix': self.archive_prefix}] + | |
| 227 | self.common_parameters + | |
| 228 | self.prune_parameters)) | |
| 229 | else: | |
| 230 | logging.error("Invalid operaton '%s'" % operation) | |
| 231 | self.__schedule_unlocked() | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
232 | |
| 8 | 233 | return True |
| 234 | ||
| 235 | def create(self, queue): | |
| 236 | with self.lock: | |
| 237 | res=self.__launch('create', queue) | |
| 238 | return res | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
239 | |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
240 | def prune(self, queue): |
| 8 | 241 | with self.lock: |
| 242 | res=self.__launch('prune', queue) | |
| 243 | return res | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
244 | |
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
245 | # 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
|
246 | def abort(self): |
| 5 | 247 | with self.lock: |
| 248 | if self.borg_instance: | |
| 249 | self.borg_instance.terminate() | |
| 7 | 250 | thread_log=self.thread_log |
| 251 | thread_res=self.thread_res | |
| 252 | ||
| 253 | if thread_log: | |
| 254 | thread_log.terminate() | |
| 255 | ||
| 256 | if thread_res: | |
| 257 | thread_res.terminate() | |
| 258 | ||
|
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
259 | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
260 | def join(self): |
| 8 | 261 | logging.debug('Waiting for borg listener threads to terminate') |
| 7 | 262 | |
| 263 | with self.lock: | |
| 264 | thread_log=self.thread_log | |
| 265 | thread_res=self.thread_res | |
| 266 | ||
| 267 | if thread_log: | |
| 268 | thread_log.join() | |
| 269 | ||
| 270 | if thread_res: | |
| 271 | thread_res.join() | |
| 272 | ||
| 273 | 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
|
274 | |
| 8 | 275 | def __queue_timed_operation(self): |
| 276 | with self.lock: | |
| 277 | operation=self.timer_operation | |
| 278 | self.timer_operation=None | |
| 279 | self.timer_time=None | |
| 280 | self.timer=None | |
| 281 | ||
| 282 | if self.__is_running_unlocked(): | |
| 283 | logging.info('Aborted queue operation, as an operation is already running') | |
| 284 | else: | |
| 285 | # TODO: Queue on 'repository' and online status for SSH, etc. | |
| 286 | ||
| 287 | # TODO: UI comms. queue? | |
| 288 | self.__launch(operation, None) | |
| 289 | ||
| 290 | def __schedule_unlocked(self): | |
| 291 | if self.current_operation: | |
| 292 | return self.current_operation, None | |
| 293 | else: | |
| 294 | operation, when=self.__next_operation_unlocked() | |
| 295 | ||
| 296 | if operation: | |
| 297 | now=time.monotonic() | |
| 298 | delay=max(0, when-now) | |
| 299 | logging.info("Scheduling '%s' of '%s' in %d seconds" % | |
| 300 | (operation, self.name, delay)) | |
| 301 | tmr=Timer(delay, self.__queue_timed_operation) | |
| 302 | self.timer_operation=operation | |
| 303 | self.timer_time=when | |
| 304 | self.timer=tmr | |
| 305 | tmr.start() | |
| 306 | ||
| 307 | return operation, time | |
| 308 | ||
| 309 | def __next_operation_unlocked(self): | |
| 310 | # TODO: pruning as well | |
| 5 | 311 | now=time.monotonic() |
| 312 | if not self.lastrun: | |
| 313 | return 'create', now+self.retry_interval | |
| 314 | elif not self.lastrun_success: | |
| 315 | return 'create', self.lastrun+self.retry_interval | |
| 316 | else: | |
| 317 | if self.backup_interval==0: | |
| 318 | return 'none', 0 | |
| 319 | else: | |
| 320 | return 'create', self.lastrun+self.backup_interval | |
|
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
321 | |
| 8 | 322 | def schedule(self): |
| 323 | with self.lock: | |
| 324 | return self.__schedule_unlocked() | |
|
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
325 | |
| 8 | 326 |