Respond to PR feedback.
This commit is contained in:
parent
ba66bc3232
commit
b8b12ff939
|
@ -1 +1 @@
|
||||||
Subproject commit 52f51dcfb96d3fe58c8cc7a905e7d733f7cd84c2
|
Subproject commit 4de0d98e2f8d80ee7631dffe076063273812a441
|
|
@ -1 +1 @@
|
||||||
Subproject commit 4f9616f163039a0eea81319d8e2288340a2ebc79
|
Subproject commit 60e47a8249fd6291a6bc20fe3fe6776b11938fa1
|
|
@ -9,6 +9,7 @@ import { context, logging } from "@budibase/backend-core"
|
||||||
import tracer from "dd-trace"
|
import tracer from "dd-trace"
|
||||||
|
|
||||||
import { IsolatedVM } from "./vm"
|
import { IsolatedVM } from "./vm"
|
||||||
|
import type { VM } from "@budibase/types"
|
||||||
|
|
||||||
export function init() {
|
export function init() {
|
||||||
setJSRunner((js: string, ctx: Record<string, any>) => {
|
setJSRunner((js: string, ctx: Record<string, any>) => {
|
||||||
|
@ -16,22 +17,26 @@ export function init() {
|
||||||
try {
|
try {
|
||||||
const bbCtx = context.getCurrentContext()
|
const bbCtx = context.getCurrentContext()
|
||||||
|
|
||||||
const vm = bbCtx?.vm
|
let vm: VM
|
||||||
? bbCtx.vm
|
if (bbCtx && bbCtx.vm) {
|
||||||
: new IsolatedVM({
|
vm = bbCtx.vm
|
||||||
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
|
} else {
|
||||||
invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS,
|
vm = new IsolatedVM({
|
||||||
isolateAccumulatedTimeout: env.JS_PER_REQUEST_TIMEOUT_MS,
|
memoryLimit: env.JS_RUNNER_MEMORY_LIMIT,
|
||||||
}).withHelpers()
|
invocationTimeout: env.JS_PER_INVOCATION_TIMEOUT_MS,
|
||||||
|
isolateAccumulatedTimeout: env.JS_PER_REQUEST_TIMEOUT_MS,
|
||||||
|
}).withHelpers()
|
||||||
|
}
|
||||||
|
|
||||||
if (bbCtx) {
|
if (bbCtx && !bbCtx.vm) {
|
||||||
// If we have a context, we want to persist it to reuse the isolate
|
|
||||||
bbCtx.vm = vm
|
bbCtx.vm = vm
|
||||||
bbCtx.cleanup = bbCtx.cleanup || []
|
bbCtx.cleanup = bbCtx.cleanup || []
|
||||||
bbCtx.cleanup.push(() => {
|
bbCtx.cleanup.push(() => vm.close())
|
||||||
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
|
const { helpers, ...rest } = ctx
|
||||||
return vm.withContext(rest, () => vm.execute(js))
|
return vm.withContext(rest, () => vm.execute(js))
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|
|
@ -2,14 +2,28 @@ import { Ctx } from "@budibase/types"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context } from "@budibase/backend-core"
|
||||||
|
|
||||||
export default async (ctx: Ctx, next: any) => {
|
export default async (ctx: Ctx, next: any) => {
|
||||||
const resp = next()
|
const resp = await next()
|
||||||
|
|
||||||
const current = context.getCurrentContext()
|
const current = context.getCurrentContext()
|
||||||
if (current?.cleanup) {
|
if (!current || !current.cleanup) {
|
||||||
for (let fn of current.cleanup || []) {
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
let errors = []
|
||||||
|
for (let fn of current.cleanup) {
|
||||||
|
try {
|
||||||
await fn()
|
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
|
return resp
|
||||||
|
|
Loading…
Reference in New Issue