From f693b1a383e55eb1332e03a905dd0f371180f092 Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Sun, 15 Sep 2024 12:21:18 +0900 Subject: [PATCH] even better autoretry code --- action.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/action.py b/action.py index b56be78..9e5e52b 100644 --- a/action.py +++ b/action.py @@ -189,7 +189,18 @@ def send_payload_to_api(args, output_files_gcs_paths, logs_gcs_path, workflow_na # Send POST request headers = {"Content-Type": "application/json"} - response = requests.post(args.api_endpoint, headers=headers, data=payload_json, timeout=REQUEST_TIMEOUT) + response = None + try: + response = requests.post(args.api_endpoint, headers=headers, data=payload_json, timeout=REQUEST_TIMEOUT) + except Exception as e: + if can_retry: + print(f"API request failed with exception {e}, retrying in 10 seconds") + time.sleep(10) + send_payload_to_api(args, output_files_gcs_paths, logs_gcs_path, workflow_name, start_time, end_time, vram_time_series, status, False) + return + print(f"API request failed with exception {e}") + traceback.print_exc() + raise e try: print("#### Payload ####") pprint.pprint(payload) @@ -211,6 +222,7 @@ def send_payload_to_api(args, output_files_gcs_paths, logs_gcs_path, workflow_na # Check the response code if response.status_code != 200: if can_retry: + print(f"API request failed with status code {response.status_code} and text {response.text}, retrying in 10 seconds") time.sleep(10) send_payload_to_api(args, output_files_gcs_paths, logs_gcs_path, workflow_name, start_time, end_time, vram_time_series, status, False) return