Merge branch 'master' of github.com:Budibase/budibase into cheeks-fixes

This commit is contained in:
Andrew Kingston 2024-11-06 12:56:39 +00:00
commit f756f3e8b4
No known key found for this signature in database
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 "./integrations"
export * from "./async"
export * from "./retry"
export * as cron from "./cron"
export * as schema from "./schema"
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 as coreEnv, db as dbCore, features } from "@budibase/backend-core"
import nodeFetch from "node-fetch"
import { helpers } from "@budibase/shared-core"
let sqsAvailable: boolean
async function isSqsAvailable() {
@ -12,17 +13,22 @@ async function isSqsAvailable() {
}
try {
const couchInfo = dbCore.getCouchInfo()
if (!couchInfo.sqlUrl) {
const { url } = dbCore.getCouchInfo()
if (!url) {
sqsAvailable = false
return false
}
await nodeFetch(couchInfo.sqlUrl, {
timeout: 1000,
})
await helpers.retry(
async () => {
await nodeFetch(url, { timeout: 2000 })
},
{ times: 3 }
)
console.log("connected to SQS")
sqsAvailable = true
return true
} catch (e) {
console.warn("failed to connect to SQS", e)
sqsAvailable = false
return false
}