Merge pull request #14969 from Budibase/sqs-test-fixes

Log SQS connection errors, and retry connection a few times before giving up.
This commit is contained in:
Sam Rose 2024-11-06 12:36:19 +00:00 committed by GitHub
commit 33f535c64f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 5 deletions

View File

@ -0,0 +1,3 @@
export async function wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}

View File

@ -1,5 +1,7 @@
export * from "./helpers" export * from "./helpers"
export * from "./integrations" export * from "./integrations"
export * from "./async"
export * from "./retry"
export * as cron from "./cron" export * as cron from "./cron"
export * as schema from "./schema" export * as schema from "./schema"
export * as views from "./views" export * as views from "./views"

View File

@ -0,0 +1,28 @@
import { wait } from "./async"
interface RetryOpts {
times?: number
}
export async function retry<T>(
fn: () => Promise<T>,
opts?: RetryOpts
): Promise<T> {
const { times = 3 } = opts || {}
if (times < 1) {
throw new Error(`invalid retry count: ${times}`)
}
let lastError: any
for (let i = 0; i < times; i++) {
const backoff = 1.5 ** i * 1000 * (Math.random() + 0.5)
await wait(backoff)
try {
return await fn()
} catch (e) {
lastError = e
}
}
throw lastError
}

View File

@ -2,6 +2,7 @@ import { Ctx, MaintenanceType, FeatureFlag } from "@budibase/types"
import env from "../../../environment" import env from "../../../environment"
import { env as coreEnv, db as dbCore, features } from "@budibase/backend-core" import { env as coreEnv, db as dbCore, features } from "@budibase/backend-core"
import nodeFetch from "node-fetch" import nodeFetch from "node-fetch"
import { helpers } from "@budibase/shared-core"
let sqsAvailable: boolean let sqsAvailable: boolean
async function isSqsAvailable() { async function isSqsAvailable() {
@ -12,17 +13,22 @@ async function isSqsAvailable() {
} }
try { try {
const couchInfo = dbCore.getCouchInfo() const { url } = dbCore.getCouchInfo()
if (!couchInfo.sqlUrl) { if (!url) {
sqsAvailable = false sqsAvailable = false
return false return false
} }
await nodeFetch(couchInfo.sqlUrl, { await helpers.retry(
timeout: 1000, async () => {
}) await nodeFetch(url, { timeout: 2000 })
},
{ times: 3 }
)
console.log("connected to SQS")
sqsAvailable = true sqsAvailable = true
return true return true
} catch (e) { } catch (e) {
console.warn("failed to connect to SQS", e)
sqsAvailable = false sqsAvailable = false
return false return false
} }