mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 10:21:52 +00:00
* add error status * update types * ṗ̶̰̙̓͒̈́ͅü̵̢̙̫̣ŗ̷̪̺̺͛g̴̲͉͎̬̒̇e̵̪̎̅͌ ̶̡̜̘͐͛t̶͎͍̣̿̍̐h̴͕̩͗̈́̎̑e̵͚͒̂͝ ̸̛͙̦͈͝v̶̱͙̬̽̔ọ̶̧̡̒̓i̸̬̲͍̋̈́d̴͉̀ * fix some extra voids * add `package.rebuild` * introduce error status and pkg rebuild and fix mocks * minor fixes * fix build --------- Co-authored-by: Matt Hill <mattnine@protonmail.com>
90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import * as T from "../../../base/lib/types"
|
|
import { asError } from "../../../base/lib/util/asError"
|
|
import { ExecSpawnable, MountOptions, SubContainer } from "../util/SubContainer"
|
|
import { CommandController } from "./CommandController"
|
|
|
|
const TIMEOUT_INCREMENT_MS = 1000
|
|
const MAX_TIMEOUT_MS = 30000
|
|
/**
|
|
* This is a wrapper around CommandController that has a state of off, where the command shouldn't be running
|
|
* and the others state of running, where it will keep a living running command
|
|
*/
|
|
|
|
export class Daemon {
|
|
private commandController: CommandController | null = null
|
|
private shouldBeRunning = false
|
|
constructor(private startCommand: () => Promise<CommandController>) {}
|
|
get subContainerHandle(): undefined | ExecSpawnable {
|
|
return this.commandController?.subContainerHandle
|
|
}
|
|
static of<Manifest extends T.Manifest>() {
|
|
return async <A extends string>(
|
|
effects: T.Effects,
|
|
subcontainer:
|
|
| {
|
|
id: keyof Manifest["images"] & T.ImageId
|
|
sharedRun?: boolean
|
|
}
|
|
| SubContainer,
|
|
command: T.CommandType,
|
|
options: {
|
|
subcontainerName?: string
|
|
mounts?: { path: string; options: MountOptions }[]
|
|
env?:
|
|
| {
|
|
[variable: string]: string
|
|
}
|
|
| undefined
|
|
cwd?: string | undefined
|
|
user?: string | undefined
|
|
onStdout?: (x: Buffer) => null
|
|
onStderr?: (x: Buffer) => null
|
|
sigtermTimeout?: number
|
|
},
|
|
) => {
|
|
const startCommand = () =>
|
|
CommandController.of<Manifest>()(
|
|
effects,
|
|
subcontainer,
|
|
command,
|
|
options,
|
|
)
|
|
return new Daemon(startCommand)
|
|
}
|
|
}
|
|
async start() {
|
|
if (this.commandController) {
|
|
return
|
|
}
|
|
this.shouldBeRunning = true
|
|
let timeoutCounter = 0
|
|
new Promise(async () => {
|
|
while (this.shouldBeRunning) {
|
|
this.commandController = await this.startCommand()
|
|
await this.commandController.wait().catch((err) => console.error(err))
|
|
await new Promise((resolve) => setTimeout(resolve, timeoutCounter))
|
|
timeoutCounter += TIMEOUT_INCREMENT_MS
|
|
timeoutCounter = Math.max(MAX_TIMEOUT_MS, timeoutCounter)
|
|
}
|
|
}).catch((err) => {
|
|
console.error(asError(err))
|
|
})
|
|
}
|
|
async term(termOptions?: {
|
|
signal?: NodeJS.Signals | undefined
|
|
timeout?: number | undefined
|
|
}) {
|
|
return this.stop(termOptions)
|
|
}
|
|
async stop(termOptions?: {
|
|
signal?: NodeJS.Signals | undefined
|
|
timeout?: number | undefined
|
|
}) {
|
|
this.shouldBeRunning = false
|
|
await this.commandController
|
|
?.term({ ...termOptions })
|
|
.catch((e) => console.error(asError(e)))
|
|
this.commandController = null
|
|
}
|
|
}
|