feat: Add the stop/start loop for the service

This commit is contained in:
J H
2024-03-06 10:55:21 -07:00
parent 093a5d4ddf
commit 8410929e86
5 changed files with 52 additions and 13 deletions

View File

@@ -276,7 +276,13 @@ export type Effects = {
}): Promise<unknown>
/** A low level api used by makeOverlay */
createOverlayedImage(options: { imageId: string }): Promise<string>
createOverlayedImage(options: { imageId: string }): Promise<[string, string]>
/** A low level api used by destroyOverlay + makeOverlay:destroy */
destroyOverlayedImage(options: {
imageId: string
guid: string
}): Promise<void>
/** Removes all network bindings */
clearBindings(): Promise<void>

View File

@@ -10,9 +10,10 @@ export class Overlay {
readonly effects: T.Effects,
readonly imageId: string,
readonly rootfs: string,
readonly guid: string,
) {}
static async of(effects: T.Effects, imageId: string) {
const rootfs = await effects.createOverlayedImage({ imageId })
const [rootfs, guid] = await effects.createOverlayedImage({ imageId })
for (const dirPart of ["dev", "sys", "proc", "run"] as const) {
await fs.mkdir(`${rootfs}/${dirPart}`, { recursive: true })
@@ -23,7 +24,7 @@ export class Overlay {
])
}
return new Overlay(effects, imageId, rootfs)
return new Overlay(effects, imageId, rootfs, guid)
}
async mount(options: MountOptions, path: string): Promise<Overlay> {
@@ -51,8 +52,9 @@ export class Overlay {
}
async destroy() {
await execFile("umount", ["-R", this.rootfs])
await fs.rm(this.rootfs, { recursive: true, force: true })
const imageId = this.imageId
const guid = this.guid
await this.effects.destroyOverlayedImage({ imageId, guid })
}
async exec(

View File

@@ -246,7 +246,7 @@ export const createUtils = <
console.error(data.toString())
})
childProcess.on("close", (code: any) => {
childProcess.on("exit", (code: any) => {
if (code === 0) {
return resolve(null)
}
@@ -262,7 +262,7 @@ export const createUtils = <
try {
childProcess.kill(signal)
if (timeout <= NO_TIMEOUT) {
if (timeout > NO_TIMEOUT) {
const didTimeout = await Promise.race([
new Promise((resolve) => setTimeout(resolve, timeout)).then(
() => true,
@@ -270,6 +270,7 @@ export const createUtils = <
answer.then(() => false),
])
if (didTimeout) childProcess.kill(SIGKILL)
return
}
await answer
} finally {