From d160430ac8159240b325468611167f3a244e28e5 Mon Sep 17 00:00:00 2001 From: adrinr Date: Mon, 30 Jan 2023 18:51:22 +0000 Subject: [PATCH] Start race condition --- packages/server/src/app.ts | 7 ++++++- packages/server/src/utilities/retry.ts | 18 ++++++++++++++++++ packages/worker/src/tests/TestConfiguration.ts | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/utilities/retry.ts diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 177393dee7..f550932dbc 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -22,6 +22,7 @@ import * as redis from "./utilities/redis" import { events, logging, middleware } from "@budibase/backend-core" import { initialise as initialiseWebsockets } from "./websocket" import { startup } from "./startup" +import { retry } from "./utilities/retry" const Sentry = require("@sentry/node") const destroyable = require("server-destroy") @@ -80,7 +81,11 @@ server.on("close", async () => { }) export default server.listen(env.PORT || 0, async () => { - await startup(app, server) + if (!env.isTest()) { + await startup(app, server) + } else { + await retry(async () => await startup(app, server)) + } }) const shutdown = () => { diff --git a/packages/server/src/utilities/retry.ts b/packages/server/src/utilities/retry.ts new file mode 100644 index 0000000000..af5d4d7cb5 --- /dev/null +++ b/packages/server/src/utilities/retry.ts @@ -0,0 +1,18 @@ +export async function retry any>( + fn: T, + maxTry: number = 5, + retryCount = 1 +): Promise>> { + const currRetry = typeof retryCount === "number" ? retryCount : 1 + try { + const result = await fn() + return result + } catch (e) { + console.log(`Retry ${currRetry} failed.`) + if (currRetry > maxTry) { + console.log(`All ${maxTry} retry attempts exhausted`) + throw e + } + return retry(fn, maxTry, currRetry + 1) + } +} diff --git a/packages/worker/src/tests/TestConfiguration.ts b/packages/worker/src/tests/TestConfiguration.ts index b13bdefa0e..c8bbd7ce22 100644 --- a/packages/worker/src/tests/TestConfiguration.ts +++ b/packages/worker/src/tests/TestConfiguration.ts @@ -33,7 +33,7 @@ enum Mode { SELF = "self", } -export async function retry any>( +async function retry any>( fn: T, maxTry: number = 5, retryCount = 1