diff --git a/pw_contest.py b/pw_contest.py index f118bb1..e84af99 100755 --- a/pw_contest.py +++ b/pw_contest.py @@ -273,20 +273,26 @@ def patch_state_update(pw, state: dict, link: str): log_end_sec() -def main_loop(pw) -> int: +def main_loop(pw, retries: int) -> int: config = parse_configs() + refresh = int(config.get('cfg', 'refresh')) try: with open(config.get('state', 'patch_state'), "rb") as fp: patch_state = json.load(fp) except FileNotFoundError: patch_state = {'series':{}, 'prs':{}} - with open(config.get('input', 'branch_info'), "rb") as fp: - branches = json.load(fp) - with open(config.get('input', 'results'), "rb") as fp: - results = json.load(fp) - with open(config.get('input', 'filters'), "rb") as fp: - filters = json.load(fp) + + try: + with open(config.get('input', 'branch_info'), "rb") as fp: + branches = json.load(fp) + with open(config.get('input', 'results'), "rb") as fp: + results = json.load(fp) + with open(config.get('input', 'filters'), "rb") as fp: + filters = json.load(fp) + except (FileNotFoundError, json.decoder.JSONDecodeError) as e: + log("Unable to read input files, retry later:", str(e)) + return refresh, retries + 1 results_by_branch = results_pivot(filters, results) branch_outcome = branch_summarize(filters, results_by_branch) @@ -304,7 +310,7 @@ def main_loop(pw) -> int: with open(config.get('state', 'patch_state'), 'w') as fp: json.dump(patch_state, fp) - return int(config.get('cfg', 'refresh')) + return refresh, 0 def parse_configs(): @@ -322,11 +328,17 @@ def main() -> None: force_single_thread=True) pw = Patchwork(config) + retries = 0 # We could do a file system watch here, because the inputs are all local. while True: log("Running at " + str(datetime.datetime.now())) - delay = main_loop(pw) + delay, retries = main_loop(pw, retries) + + if retries > 4: + log("Multiple succeeding errors, stop here") + sys.exit(1) + try: time.sleep(delay) except KeyboardInterrupt: