diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts index 012a70eee..40a152bab 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/DockerProcedureContainer.ts @@ -88,15 +88,19 @@ export class DockerProcedureContainer { return new DockerProcedureContainer(overlay) } - async exec(commands: string[]) { + async exec(commands: string[], { destroy = true } = {}) { try { return await this.overlay.exec(commands) } finally { - await this.overlay.destroy() + if (destroy) await this.overlay.destroy() } } - async execFail(commands: string[], timeoutMs: number | null) { + async execFail( + commands: string[], + timeoutMs: number | null, + { destroy = true } = {}, + ) { try { const res = await this.overlay.exec(commands, {}, timeoutMs) if (res.exitCode !== 0) { @@ -110,7 +114,7 @@ export class DockerProcedureContainer { } return res } finally { - await this.overlay.destroy() + if (destroy) await this.overlay.destroy() } } diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/MainLoop.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/MainLoop.ts index f8f0a2d6e..e29170543 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/MainLoop.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/MainLoop.ts @@ -14,6 +14,10 @@ const EMBASSY_PROPERTIES_LOOP = 30 * 1000 * Also, this has an ability to clean itself up too if need be. */ export class MainLoop { + private _mainDockerContainer?: DockerProcedureContainer + get mainDockerContainer() { + return this._mainDockerContainer + } private healthLoops?: { name: string interval: NodeJS.Timeout @@ -54,6 +58,7 @@ export class MainLoop { this.system.manifest.main, this.system.manifest.volumes, ) + this._mainDockerContainer = dockerProcedureContainer if (jsMain) { throw new Error("Unreachable") } @@ -126,6 +131,7 @@ export class MainLoop { await main?.daemon.stop().catch((e) => console.error(e)) this.effects.setMainStatus({ status: "stopped" }) if (healthLoops) healthLoops.forEach((x) => clearInterval(x.interval)) + delete this._mainDockerContainer } private constructHealthLoops() { @@ -138,17 +144,25 @@ export class MainLoop { const actionProcedure = value const timeChanged = Date.now() - start if (actionProcedure.type === "docker") { - const container = await DockerProcedureContainer.of( - effects, - manifest.id, - actionProcedure, - manifest.volumes, + // prettier-ignore + const container = + actionProcedure.inject && this._mainDockerContainer ? + this._mainDockerContainer : + await DockerProcedureContainer.of( + effects, + manifest.id, + actionProcedure, + manifest.volumes, + ) + const shouldDestroy = container !== this._mainDockerContainer + const executed = await container.exec( + [ + actionProcedure.entrypoint, + ...actionProcedure.args, + JSON.stringify(timeChanged), + ], + { destroy: shouldDestroy }, ) - const executed = await container.exec([ - actionProcedure.entrypoint, - ...actionProcedure.args, - JSON.stringify(timeChanged), - ]) if (executed.exitCode === 0) { await effects.setHealth({ id: healthId, diff --git a/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts b/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts index ffdb02988..f639d00cd 100644 --- a/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts +++ b/container-runtime/src/Adapters/Systems/SystemForEmbassy/index.ts @@ -799,12 +799,17 @@ export class SystemForEmbassy implements System { const actionProcedure = this.manifest.actions?.[actionId]?.implementation if (!actionProcedure) return { message: "Action not found", value: null } if (actionProcedure.type === "docker") { - const container = await DockerProcedureContainer.of( - effects, - this.manifest.id, - actionProcedure, - this.manifest.volumes, - ) + const container = + actionProcedure.inject && this.currentRunning?.mainDockerContainer + ? this.currentRunning?.mainDockerContainer + : await DockerProcedureContainer.of( + effects, + this.manifest.id, + actionProcedure, + this.manifest.volumes, + ) + const shouldDestroy = + container !== this.currentRunning?.mainDockerContainer return JSON.parse( ( await container.execFail( @@ -814,6 +819,7 @@ export class SystemForEmbassy implements System { JSON.stringify(formData), ], timeoutMs, + { destroy: shouldDestroy }, ) ).stdout.toString(), )