From 3b94438204ef29d572ca3d8c3b39d7a5c685b83f Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Fri, 21 May 2021 18:07:00 +0200 Subject: [PATCH] LiveSync: Add retry option to initial export --- livesync/indico_livesync/cli.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/livesync/indico_livesync/cli.py b/livesync/indico_livesync/cli.py index 2c9ddce..f33833b 100644 --- a/livesync/indico_livesync/cli.py +++ b/livesync/indico_livesync/cli.py @@ -5,7 +5,10 @@ # them and/or modify them under the terms of the MIT License; # see the LICENSE file for more details. +import os +import sys import time +import traceback import click from flask_pluginengine import current_plugin @@ -64,11 +67,12 @@ def agents(): @cli.command() @click.argument('agent_id', type=int) +@click.option('--retry', '-r', is_flag=True, help="Restart automatically after a failure") @click.option('--force', '-f', is_flag=True, help="Perform export even if it has already been done once.") @click.option('--verbose', '-v', is_flag=True, help="Be more verbose (what this does is up to the backend)") @click.option('--batch', type=int, default=5000, help="The amount of records yielded per export batch.", show_default=True, metavar='N') -def initial_export(agent_id, batch, force, verbose): +def initial_export(agent_id, batch, force, verbose, retry): """Performs the initial data export for an agent""" agent = LiveSyncAgent.get(agent_id) if agent is None: @@ -91,9 +95,18 @@ def initial_export(agent_id, batch, force, verbose): print(cformat('To re-run it, use %{yellow!}--force%{reset}')) return - if not backend.run_initial_export(batch, force, verbose): - print('The initial export failed; not marking it as done') - return + try: + if not backend.run_initial_export(batch, force, verbose): + print('The initial export failed; not marking it as done') + return + except Exception: + if not retry: + raise + traceback.print_exc() + print('Restarting in 2 seconds') + time.sleep(2) + os.execl(sys.argv[0], *sys.argv) + return # exec doesn't return but just in case... agent.initial_data_exported = True db.session.commit()