From b2813db5db41f99b61f166a5c32bd05eaf13bd77 Mon Sep 17 00:00:00 2001 From: adrinr Date: Tue, 31 Jan 2023 16:15:11 +0000 Subject: [PATCH] Handle db creation race conditions --- hosting/docker-compose.test.yaml | 13 ------------- packages/backend-core/src/db/couch/DatabaseImpl.ts | 9 ++++++++- packages/server/src/app.ts | 7 +------ 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/hosting/docker-compose.test.yaml b/hosting/docker-compose.test.yaml index f11d32fc3b..dfd78621c5 100644 --- a/hosting/docker-compose.test.yaml +++ b/hosting/docker-compose.test.yaml @@ -37,19 +37,6 @@ services: timeout: 20s 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: restart: on-failure image: redis diff --git a/packages/backend-core/src/db/couch/DatabaseImpl.ts b/packages/backend-core/src/db/couch/DatabaseImpl.ts index 4919d8db4a..de45c14033 100644 --- a/packages/backend-core/src/db/couch/DatabaseImpl.ts +++ b/packages/backend-core/src/db/couch/DatabaseImpl.ts @@ -58,7 +58,14 @@ export class DatabaseImpl implements Database { throw new Error("DB does not exist") } 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) } diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index f550932dbc..177393dee7 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -22,7 +22,6 @@ import * as redis from "./utilities/redis" import { events, logging, middleware } from "@budibase/backend-core" import { initialise as initialiseWebsockets } from "./websocket" import { startup } from "./startup" -import { retry } from "./utilities/retry" const Sentry = require("@sentry/node") const destroyable = require("server-destroy") @@ -81,11 +80,7 @@ server.on("close", async () => { }) export default server.listen(env.PORT || 0, async () => { - if (!env.isTest()) { - await startup(app, server) - } else { - await retry(async () => await startup(app, server)) - } + await startup(app, server) }) const shutdown = () => {