Log SQS connection errors, and retry connection a few times before giving up.
This commit is contained in:
parent
62bd87bbfb
commit
ff9d59e1ee
|
@ -0,0 +1,3 @@
|
||||||
|
export async function wait(ms: number) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms))
|
||||||
|
}
|
|
@ -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"
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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 { retry } from "../../../../../shared-core/src/helpers"
|
||||||
|
|
||||||
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 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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue