fix: Inject for actions and health

This commit is contained in:
J H
2024-08-07 06:19:04 -06:00
parent 7c32404b69
commit 419d4986f6
3 changed files with 44 additions and 20 deletions

View File

@@ -88,15 +88,19 @@ export class DockerProcedureContainer {
return new DockerProcedureContainer(overlay) return new DockerProcedureContainer(overlay)
} }
async exec(commands: string[]) { async exec(commands: string[], { destroy = true } = {}) {
try { try {
return await this.overlay.exec(commands) return await this.overlay.exec(commands)
} finally { } 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 { try {
const res = await this.overlay.exec(commands, {}, timeoutMs) const res = await this.overlay.exec(commands, {}, timeoutMs)
if (res.exitCode !== 0) { if (res.exitCode !== 0) {
@@ -110,7 +114,7 @@ export class DockerProcedureContainer {
} }
return res return res
} finally { } finally {
await this.overlay.destroy() if (destroy) await this.overlay.destroy()
} }
} }

View File

@@ -14,6 +14,10 @@ const EMBASSY_PROPERTIES_LOOP = 30 * 1000
* Also, this has an ability to clean itself up too if need be. * Also, this has an ability to clean itself up too if need be.
*/ */
export class MainLoop { export class MainLoop {
private _mainDockerContainer?: DockerProcedureContainer
get mainDockerContainer() {
return this._mainDockerContainer
}
private healthLoops?: { private healthLoops?: {
name: string name: string
interval: NodeJS.Timeout interval: NodeJS.Timeout
@@ -54,6 +58,7 @@ export class MainLoop {
this.system.manifest.main, this.system.manifest.main,
this.system.manifest.volumes, this.system.manifest.volumes,
) )
this._mainDockerContainer = dockerProcedureContainer
if (jsMain) { if (jsMain) {
throw new Error("Unreachable") throw new Error("Unreachable")
} }
@@ -126,6 +131,7 @@ export class MainLoop {
await main?.daemon.stop().catch((e) => console.error(e)) await main?.daemon.stop().catch((e) => console.error(e))
this.effects.setMainStatus({ status: "stopped" }) this.effects.setMainStatus({ status: "stopped" })
if (healthLoops) healthLoops.forEach((x) => clearInterval(x.interval)) if (healthLoops) healthLoops.forEach((x) => clearInterval(x.interval))
delete this._mainDockerContainer
} }
private constructHealthLoops() { private constructHealthLoops() {
@@ -138,17 +144,25 @@ export class MainLoop {
const actionProcedure = value const actionProcedure = value
const timeChanged = Date.now() - start const timeChanged = Date.now() - start
if (actionProcedure.type === "docker") { if (actionProcedure.type === "docker") {
const container = await DockerProcedureContainer.of( // prettier-ignore
effects, const container =
manifest.id, actionProcedure.inject && this._mainDockerContainer ?
actionProcedure, this._mainDockerContainer :
manifest.volumes, 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) { if (executed.exitCode === 0) {
await effects.setHealth({ await effects.setHealth({
id: healthId, id: healthId,

View File

@@ -799,12 +799,17 @@ export class SystemForEmbassy implements System {
const actionProcedure = this.manifest.actions?.[actionId]?.implementation const actionProcedure = this.manifest.actions?.[actionId]?.implementation
if (!actionProcedure) return { message: "Action not found", value: null } if (!actionProcedure) return { message: "Action not found", value: null }
if (actionProcedure.type === "docker") { if (actionProcedure.type === "docker") {
const container = await DockerProcedureContainer.of( const container =
effects, actionProcedure.inject && this.currentRunning?.mainDockerContainer
this.manifest.id, ? this.currentRunning?.mainDockerContainer
actionProcedure, : await DockerProcedureContainer.of(
this.manifest.volumes, effects,
) this.manifest.id,
actionProcedure,
this.manifest.volumes,
)
const shouldDestroy =
container !== this.currentRunning?.mainDockerContainer
return JSON.parse( return JSON.parse(
( (
await container.execFail( await container.execFail(
@@ -814,6 +819,7 @@ export class SystemForEmbassy implements System {
JSON.stringify(formData), JSON.stringify(formData),
], ],
timeoutMs, timeoutMs,
{ destroy: shouldDestroy },
) )
).stdout.toString(), ).stdout.toString(),
) )