From 379bd43ab80ccd45a28d45f3dd4a6dd681b731ff Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 30 Jul 2024 02:27:55 +0200 Subject: [PATCH] Add in automatic retry, some uploads fail because of a fluke --- src/Utils.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Utils.ts b/src/Utils.ts index 657405d8a..ddd9757d0 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -943,14 +943,39 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be return result["content"] } - /** - * Download function which also indicates advanced options, such as redirects - */ - public static downloadAdvanced( + public static async downloadAdvanced( url: string, headers?: Record, method: "POST" | "GET" | "PUT" | "UPDATE" | "DELETE" | "OPTIONS" = "GET", - content?: string + content?: string, + maxAttempts: number = 3 + ): Promise< + | { content: string } + | { redirect: string } + | { error: string; url: string; statuscode?: number } + >{ + + let result = undefined + for (let i = 0; i < maxAttempts; i++) { + result = await Utils.downloadAdvanced(url, headers, method, content) + if(!result["error"] ){ + return result + } + console.log(`Request to ${url} failed, Trying again in a moment. Attempt ${i+1}/${maxAttempts}`) + await Utils.waitFor((i+1) * 500) + } + return result + + } + + /** + * Download function which also indicates advanced options, such as redirects + */ + public static downloadAdvancedTryOnce( + url: string, + headers?: Record, + method: "POST" | "GET" | "PUT" | "UPDATE" | "DELETE" | "OPTIONS" = "GET", + content?: string, ): Promise< | { content: string } | { redirect: string }