Handle db creation race conditions

This commit is contained in:
adrinr 2023-01-31 16:15:11 +00:00
parent bb43812ac5
commit b2813db5db
3 changed files with 9 additions and 20 deletions

View File

@ -37,19 +37,6 @@ services:
timeout: 20s timeout: 20s
retries: 3 retries: 3
couch-init:
image: curlimages/curl
environment:
PUT_CALL: "curl -u ${COUCH_DB_USER}:${COUCH_DB_PASSWORD} -X PUT couchdb-service:5984"
depends_on:
- couchdb-service
command:
[
"sh",
"-c",
"sleep 10 && $${PUT_CALL}/_users && $${PUT_CALL}/_replicator; fg;",
]
redis-service: redis-service:
restart: on-failure restart: on-failure
image: redis image: redis

View File

@ -58,7 +58,14 @@ export class DatabaseImpl implements Database {
throw new Error("DB does not exist") throw new Error("DB does not exist")
} }
if (!exists) { if (!exists) {
await DatabaseImpl.nano.db.create(this.name) try {
await DatabaseImpl.nano.db.create(this.name)
} catch (err: any) {
// Handling race conditions
if (err.statusCode !== 412) {
throw err
}
}
} }
return DatabaseImpl.nano.db.use(this.name) return DatabaseImpl.nano.db.use(this.name)
} }

View File

@ -22,7 +22,6 @@ import * as redis from "./utilities/redis"
import { events, logging, middleware } from "@budibase/backend-core" import { events, logging, middleware } from "@budibase/backend-core"
import { initialise as initialiseWebsockets } from "./websocket" import { initialise as initialiseWebsockets } from "./websocket"
import { startup } from "./startup" import { startup } from "./startup"
import { retry } from "./utilities/retry"
const Sentry = require("@sentry/node") const Sentry = require("@sentry/node")
const destroyable = require("server-destroy") const destroyable = require("server-destroy")
@ -81,11 +80,7 @@ server.on("close", async () => {
}) })
export default server.listen(env.PORT || 0, async () => { export default server.listen(env.PORT || 0, async () => {
if (!env.isTest()) { await startup(app, server)
await startup(app, server)
} else {
await retry(async () => await startup(app, server))
}
}) })
const shutdown = () => { const shutdown = () => {