Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log SQS connection errors, and retry connection a few times before giving up. #14969

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/shared-core/src/helpers/async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
2 changes: 2 additions & 0 deletions packages/shared-core/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
28 changes: 28 additions & 0 deletions packages/shared-core/src/helpers/retry.ts
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 11 additions & 5 deletions packages/worker/src/api/controllers/system/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down
Loading