Skip to content

Commit

Permalink
Return the last Restic progress message
Browse files Browse the repository at this point in the history
Use agent.run_restic() to capture progress. The agent.run_restic() can
now capture the JSON output by itself and invoke a callback function.

The same function can intercept the last JSON message from Restic and
return it in the action output.
  • Loading branch information
DavidePrincipi committed Nov 15, 2024
1 parent 6871e65 commit 6b251c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
29 changes: 15 additions & 14 deletions imageroot/actions/restore-backup-content/50restore_backup_content
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ restic_args = [
f"--include={maildir_path_goescaped}*",
]
podman_args = ["--workdir=/srv"] + agent.agent.get_state_volume_args()
restic_cmd, restic_env = agent.prepare_restic_command(agent.redis_connect(), request["destination"], request["repopath"], podman_args, restic_args)
with subprocess.Popen(restic_cmd, env=restic_env, stdout=subprocess.PIPE, text=True, errors='replace') as prestore:
set_restore_progress_value = agent.get_progress_callback(5, 95)
while True:
line = prestore.stdout.readline()
if not line:
break
try:
omessage = json.loads(line)
if omessage['message_type'] == 'status':
fpercent = float(omessage['percent_done'])
set_restore_progress_value(int(fpercent * 100))
except Exception as ex:
print(agent.SD_WARNING+"Error decoding Restic status message", ex, file=sys.stderr)

# Prepare progress callback function that captures non-progress messages too:
last_restic_message = {}
def build_restore_progress_callback():
restore_progress = agent.get_progress_callback(5, 95)
def fprog(omessage):
global last_restic_message
last_restic_message = omessage
if omessage['message_type'] == 'status':
fpercent = float(omessage['percent_done'])
restore_progress(int(fpercent * 100))
return fprog
# Run the restic restore command capturing the progress status:
prestore = agent.run_restic(agent.redis_connect(), request["destination"], request["repopath"], podman_args, restic_args, progress_callback=build_restore_progress_callback())
if prestore.returncode != 0:
print(agent.SD_ERR + f"Restic restore command failed with exit code {prestore.returncode}.", file=sys.stderr)
sys.exit(1)
Expand All @@ -79,4 +79,5 @@ if 'QUOTA_DISABLED_BY_RESTORE' in proc_import.stdout:
json.dump({
"request": request,
"quota_disabled": quota_disabled,
"last_restic_message": last_restic_message,
}, fp=sys.stdout)
4 changes: 4 additions & 0 deletions imageroot/actions/restore-backup-content/validate-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"type": "boolean",
"description": "If the restore has disabled the user's quota or not"
},
"last_restic_message": {
"type": "object",
"description": "Last JSON message from Restic restore"
},
"request": {
"type": "object",
"title": "Original request object"
Expand Down

0 comments on commit 6b251c7

Please sign in to comment.