Compare commits

...

3 Commits

Author SHA1 Message Date
81d430b56b backup: print exceptions from reader thread 2021-10-17 20:02:46 -04:00
2d89e530be backup: split handling of log_message and progress_message 2021-10-17 20:01:55 -04:00
3024cf2e69 backup: stop main thread if reader thread dies unexpectedly
_thread.interrupt_main will trigger KeyboardInterrupt in the main thread.
2021-10-17 20:00:55 -04:00

View File

@ -15,6 +15,7 @@ import select
import pathlib
import threading
import subprocess
import _thread # for interrupt_main
import typing
@ -317,10 +318,7 @@ def main(argv: typing.List[str]):
for line in fh:
try:
data = json.loads(line)
if ((data['type'] == 'log_message' or
data['type'] == 'progress_message')
and 'message' in data):
if data['type'] == 'log_message':
# Count warnings and errors, but ignore some.
changed_msg = "file changed while we backed it up"
if data['levelname'] == 'WARNING':
@ -334,6 +332,11 @@ def main(argv: typing.List[str]):
prefix = ""
line = (prefix + data['message'] + '\n').encode()
elif (data['type'] == 'progress_message'
and 'message' in data):
line = (data['message'] + '\n').encode()
elif data['type'] == 'archive_progress':
now = time.time()
if now - last_progress > 10:
@ -353,13 +356,20 @@ def main(argv: typing.List[str]):
# ignore unknown progress line
continue
except Exception as e:
# on error, print raw line
pass
# on error, print raw line with exception
line = f"[exception: {str(e)} ]".encode() + line
sys.stdout.buffer.write(line)
sys.stdout.flush()
captured_output.append(line)
fh.close()
reader = threading.Thread(target=reader_thread, args=(borg.stdout,))
def _reader_thread(fh):
try:
return reader_thread(fh)
except BrokenPipeError:
pass
except Exception:
_thread.interrupt_main()
reader = threading.Thread(target=_reader_thread, args=(borg.stdout,))
reader.daemon = True
reader.start()