chore: Add the process tree destroyer

This commit is contained in:
J H
2024-03-06 15:43:07 -07:00
parent 8410929e86
commit f3ccad192c
3 changed files with 49 additions and 6 deletions

View File

@@ -110,11 +110,21 @@ export class HostSystemStartOs implements Effects {
T.Effects["clearServiceInterfaces"] T.Effects["clearServiceInterfaces"]
> >
} }
createOverlayedImage(options: { imageId: string }): Promise<string> { createOverlayedImage(options: {
imageId: string
}): Promise<[string, string]> {
return this.rpcRound("createOverlayedImage", options) as ReturnType< return this.rpcRound("createOverlayedImage", options) as ReturnType<
T.Effects["createOverlayedImage"] T.Effects["createOverlayedImage"]
> >
} }
destroyOverlayedImage(options: {
imageId: string
guid: string
}): Promise<void> {
return this.rpcRound("destroyOverlayedImage", options) as ReturnType<
T.Effects["destroyOverlayedImage"]
>
}
executeAction(...[options]: Parameters<T.Effects["executeAction"]>) { executeAction(...[options]: Parameters<T.Effects["executeAction"]>) {
return this.rpcRound("executeAction", options) as ReturnType< return this.rpcRound("executeAction", options) as ReturnType<
T.Effects["executeAction"] T.Effects["executeAction"]

View File

@@ -137,6 +137,7 @@ export class Daemons<Manifest extends SDKManifest, Ids extends string> {
} }
return { return {
async term(options?: { signal?: Signals; timeout?: number }) { async term(options?: { signal?: Signals; timeout?: number }) {
console.error("Bluj Daemons term")
await Promise.all( await Promise.all(
Object.values<Promise<DaemonReturned>>(daemonsStarted).map((x) => Object.values<Promise<DaemonReturned>>(daemonsStarted).map((x) =>
x.then((x) => x.term(options)), x.then((x) => x.term(options)),
@@ -144,6 +145,7 @@ export class Daemons<Manifest extends SDKManifest, Ids extends string> {
) )
}, },
async wait() { async wait() {
console.error("Bluj Daemons wait")
await Promise.all( await Promise.all(
Object.values<Promise<DaemonReturned>>(daemonsStarted).map((x) => Object.values<Promise<DaemonReturned>>(daemonsStarted).map((x) =>
x.then((x) => x.wait()), x.then((x) => x.wait()),

View File

@@ -254,11 +254,21 @@ export const createUtils = <
}) })
}) })
const pid = childProcess.pid
return { return {
wait() { async wait() {
return answer const pids = pid ? await psTree(pid, overlay) : []
try {
return await answer
} finally {
for (const process of pids) {
overlay.exec(["kill", `-9`, String(process)])
}
}
}, },
async term({ signal = SIGTERM, timeout = NO_TIMEOUT } = {}) { async term({ signal = SIGTERM, timeout = NO_TIMEOUT } = {}) {
const pids = pid ? await psTree(pid, overlay) : []
console.error("Bluj killing pid ", pids)
try { try {
childProcess.kill(signal) childProcess.kill(signal)
@@ -269,13 +279,26 @@ export const createUtils = <
), ),
answer.then(() => false), answer.then(() => false),
]) ])
if (didTimeout) childProcess.kill(SIGKILL) if (didTimeout) {
return childProcess.kill(SIGKILL)
}
} else {
await answer
} }
await answer
} finally { } finally {
await overlay.destroy() await overlay.destroy()
} }
console.error("Bluj actually killing pid ", pids)
try {
for (const process of pids) {
await overlay.exec(["kill", `-${signal}`, String(process)])
}
} finally {
for (const process of pids) {
overlay.exec(["kill", `-9`, String(process)])
}
}
}, },
} }
}, },
@@ -294,3 +317,11 @@ export const createUtils = <
} }
} }
function noop(): void {} function noop(): void {}
async function psTree(pid: number, overlay: Overlay): Promise<number[]> {
const { stdout } = await overlay.exec(["pstree", `-p`, String(pid)])
const regex: RegExp = /\((\d+)\)/g
return [...stdout.toString().matchAll(regex)].map(([_all, pid]) =>
parseInt(pid),
)
}