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