Respond to PR feedback.

This commit is contained in:
Sam Rose 2024-02-21 15:26:26 +00:00
parent ba66bc3232
commit b8b12ff939
No known key found for this signature in database
4 changed files with 37 additions and 18 deletions

@ -1 +1 @@
Subproject commit 52f51dcfb96d3fe58c8cc7a905e7d733f7cd84c2
Subproject commit 4de0d98e2f8d80ee7631dffe076063273812a441

@ -1 +1 @@
Subproject commit 4f9616f163039a0eea81319d8e2288340a2ebc79
Subproject commit 60e47a8249fd6291a6bc20fe3fe6776b11938fa1

View File

@ -9,6 +9,7 @@ import { context, logging } from "@budibase/backend-core"
import tracer from "dd-trace"
import { IsolatedVM } from "./vm"
import type { VM } from "@budibase/types"
export function init() {
setJSRunner((js: string, ctx: Record<string, any>) => {
@ -16,22 +17,26 @@ export function init() {
try {
const bbCtx = context.getCurrentContext()
const vm = bbCtx?.vm
? bbCtx.vm
: new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS,
isolateAccumulatedTimeout: env.JS_PER_REQUEST_TIMEOUT_MS,
}).withHelpers()
let vm: VM
if (bbCtx && bbCtx.vm) {
vm = bbCtx.vm
} else {
vm = new IsolatedVM({
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS,
isolateAccumulatedTimeout: env.JS_PER_REQUEST_TIMEOUT_MS,
}).withHelpers()
}
if (bbCtx) {
// If we have a context, we want to persist it to reuse the isolate
if (bbCtx && !bbCtx.vm) {
bbCtx.vm = vm
bbCtx.cleanup = bbCtx.cleanup || []
bbCtx.cleanup.push(() => {
vm.close()
})
bbCtx.cleanup.push(() => vm.close())
}
// Because we can't pass functions into an Isolate, we remove them from
// the passed context and rely on the withHelpers() method to add them
// back in.
const { helpers, ...rest } = ctx
return vm.withContext(rest, () => vm.execute(js))
} catch (error: any) {

View File

@ -2,14 +2,28 @@ import { Ctx } from "@budibase/types"
import { context } from "@budibase/backend-core"
export default async (ctx: Ctx, next: any) => {
const resp = next()
const resp = await next()
const current = context.getCurrentContext()
if (current?.cleanup) {
for (let fn of current.cleanup || []) {
if (!current || !current.cleanup) {
return resp
}
let errors = []
for (let fn of current.cleanup) {
try {
await fn()
} catch (e) {
// We catch errors here to ensure we at least attempt to run all cleanup
// functions. We'll throw the first error we encounter after all cleanup
// functions have been run.
errors.push(e)
}
delete current.cleanup
}
delete current.cleanup
if (errors.length > 0) {
throw errors[0]
}
return resp