Adding the ability to force a new context.
This commit is contained in:
parent
89c51a1afc
commit
fe1016d01b
|
@ -73,7 +73,7 @@ exports.isMultiTenant = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// used for automations, API endpoints should always be in context already
|
// 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
|
// the internal function is so that we can re-use an existing
|
||||||
// context - don't want to close DB on a parent context
|
// context - don't want to close DB on a parent context
|
||||||
async function internal(opts = { existing: false }) {
|
async function internal(opts = { existing: false }) {
|
||||||
|
@ -98,7 +98,11 @@ exports.doInTenant = (tenantId, task) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const using = cls.getFromContext(ContextKeys.IN_USE)
|
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)
|
cls.setOnContext(ContextKeys.IN_USE, using + 1)
|
||||||
return internal({ existing: true })
|
return internal({ existing: true })
|
||||||
} else {
|
} else {
|
||||||
|
@ -135,7 +139,7 @@ const setAppTenantId = appId => {
|
||||||
exports.updateTenantId(appTenantId)
|
exports.updateTenantId(appTenantId)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.doInAppContext = (appId, task) => {
|
exports.doInAppContext = (appId, task, { forceNew } = {}) => {
|
||||||
if (!appId) {
|
if (!appId) {
|
||||||
throw new Error("appId is required")
|
throw new Error("appId is required")
|
||||||
}
|
}
|
||||||
|
@ -162,7 +166,7 @@ exports.doInAppContext = (appId, task) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const using = cls.getFromContext(ContextKeys.IN_USE)
|
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)
|
cls.setOnContext(ContextKeys.IN_USE, using + 1)
|
||||||
return internal({ existing: true })
|
return internal({ existing: true })
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue