Clean up isolates when a request is finished.

This commit is contained in:
Sam Rose 2024-02-02 09:30:33 +00:00
parent 3e84ad924d
commit ba002f9649
No known key found for this signature in database
3 changed files with 25 additions and 0 deletions

View File

@ -15,4 +15,5 @@ export type ContextMap = {
jsContext: Context
helpersModule: Module
}
cleanup?: (() => void | Promise<void>)[]
}

View File

@ -115,6 +115,14 @@ export function init() {
}
bbCtx.isolateRefs = { jsContext, jsIsolate, helpersModule }
if (!bbCtx.cleanup) {
bbCtx.cleanup = []
}
bbCtx.cleanup.push(() => {
helpersModule.release()
jsContext.release()
jsIsolate.dispose()
})
}
let { jsIsolate, jsContext, helpersModule } = bbCtx.isolateRefs!

View File

@ -0,0 +1,16 @@
import { Ctx } from "@budibase/types"
import { context } from "@budibase/backend-core"
export default async (ctx: Ctx, next: any) => {
const resp = next()
const current = context.getCurrentContext()
if (current?.cleanup) {
for (let fn of current.cleanup || []) {
await fn()
}
delete current.cleanup
}
return resp
}