diff --git a/packages/backend-core/context.js b/packages/backend-core/context.js index 4bc100687d..2afd762536 100644 --- a/packages/backend-core/context.js +++ b/packages/backend-core/context.js @@ -5,6 +5,7 @@ const { getAppId, updateAppId, doInAppContext, + doInContext, } = require("./src/context") module.exports = { @@ -14,4 +15,5 @@ module.exports = { getAppId, updateAppId, doInAppContext, + doInContext, } diff --git a/packages/backend-core/src/context/index.js b/packages/backend-core/src/context/index.js index 3abbc84596..fa8f09b659 100644 --- a/packages/backend-core/src/context/index.js +++ b/packages/backend-core/src/context/index.js @@ -139,6 +139,16 @@ const setAppTenantId = appId => { exports.updateTenantId(appTenantId) } +// gets the tenant ID from the app ID +exports.doInContext = async (appId, task) => { + const tenantId = exports.getTenantIDFromAppID(appId) + return exports.doInTenant(tenantId, async () => { + return exports.doInAppContext(appId, async () => { + return task() + }) + }) +} + exports.doInAppContext = (appId, task, { forceNew } = {}) => { if (!appId) { throw new Error("appId is required") diff --git a/packages/server/src/automations/utils.ts b/packages/server/src/automations/utils.ts index adb3cbf2c6..3962dc4354 100644 --- a/packages/server/src/automations/utils.ts +++ b/packages/server/src/automations/utils.ts @@ -8,7 +8,7 @@ import { MetadataTypes, WebhookType } from "../constants" import { getProdAppID, doWithDB } from "@budibase/backend-core/db" import { cloneDeep } from "lodash/fp" import { getAppDB, getAppId } from "@budibase/backend-core/context" -import { tenancy } from "@budibase/backend-core" +import { context } from "@budibase/backend-core" import { quotas } from "@budibase/pro" const WH_STEP_ID = definitions.WEBHOOK.stepId @@ -22,8 +22,7 @@ export async function processEvent(job: any) { `${job.data.automation.appId} automation ${automationId} running` ) // need to actually await these so that an error can be captured properly - const tenantId = tenancy.getTenantIDFromAppID(job.data.event.appId) - return await tenancy.doInTenant(tenantId, async () => { + return await context.doInContext(job.data.event.appId, async () => { const runFn = () => Runner.run(job) return quotas.addAutomation(runFn, { automationId,