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