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