From 484dbbb6051f52ac84f7afd988cb9b761ca8eaa8 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 23 May 2022 23:23:49 +0100 Subject: [PATCH] Adding the ability to force a new context. --- packages/backend-core/src/context/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/backend-core/src/context/index.js b/packages/backend-core/src/context/index.js index 20e5e26693..3abbc84596 100644 --- a/packages/backend-core/src/context/index.js +++ b/packages/backend-core/src/context/index.js @@ -73,7 +73,7 @@ exports.isMultiTenant = () => { } // used for automations, API endpoints should always be in context already -exports.doInTenant = (tenantId, task) => { +exports.doInTenant = (tenantId, task, { forceNew } = {}) => { // the internal function is so that we can re-use an existing // context - don't want to close DB on a parent context async function internal(opts = { existing: false }) { @@ -98,7 +98,11 @@ exports.doInTenant = (tenantId, task) => { } } const using = cls.getFromContext(ContextKeys.IN_USE) - if (using && cls.getFromContext(ContextKeys.TENANT_ID) === tenantId) { + if ( + !forceNew && + using && + cls.getFromContext(ContextKeys.TENANT_ID) === tenantId + ) { cls.setOnContext(ContextKeys.IN_USE, using + 1) return internal({ existing: true }) } else { @@ -135,7 +139,7 @@ const setAppTenantId = appId => { exports.updateTenantId(appTenantId) } -exports.doInAppContext = (appId, task) => { +exports.doInAppContext = (appId, task, { forceNew } = {}) => { if (!appId) { throw new Error("appId is required") } @@ -162,7 +166,7 @@ exports.doInAppContext = (appId, task) => { } } const using = cls.getFromContext(ContextKeys.IN_USE) - if (using && cls.getFromContext(ContextKeys.APP_ID) === appId) { + if (!forceNew && using && cls.getFromContext(ContextKeys.APP_ID) === appId) { cls.setOnContext(ContextKeys.IN_USE, using + 1) return internal({ existing: true }) } else {