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)
}
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()
}
}

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.
*/
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,

View File

@@ -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(),
)