Start race condition

This commit is contained in:
adrinr 2023-01-30 18:51:22 +00:00
parent 08a2e77871
commit d160430ac8
3 changed files with 25 additions and 2 deletions

View File

@ -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 = () => {

View File

@ -0,0 +1,18 @@
export async function retry<T extends (...arg0: any[]) => any>(
fn: T,
maxTry: number = 5,
retryCount = 1
): Promise<Awaited<ReturnType<T>>> {
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)
}
}

View File

@ -33,7 +33,7 @@ enum Mode {
SELF = "self",
}
export async function retry<T extends (...arg0: any[]) => any>(
async function retry<T extends (...arg0: any[]) => any>(
fn: T,
maxTry: number = 5,
retryCount = 1