Files
start-os/sdk/package/lib/mainFn/Oneshot.ts
Aiden McClelland 44560c8da8 Refactor/sdk init (#2947)
* fixes for main

* refactor package initialization

* fixes from testing

* more fixes

* beta.21

* do not use instanceof

* closes #2921

* beta22

* allow disabling kiosk

* migration

* fix /etc/shadow

* actionRequest -> task

* beta.23
2025-05-21 10:24:37 -06:00

51 lines
1.5 KiB
TypeScript

import * as T from "../../../base/lib/types"
import { SubContainer, SubContainerOwned } from "../util/SubContainer"
import { CommandController } from "./CommandController"
import { Daemon } from "./Daemon"
/**
* 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
* unlike Daemon, does not restart on success
*/
export class Oneshot<Manifest extends T.SDKManifest> extends Daemon<Manifest> {
static of<Manifest extends T.SDKManifest>() {
return async (
effects: T.Effects,
subcontainer: SubContainer<Manifest>,
command: T.CommandType,
options: {
env?:
| {
[variable: string]: string
}
| undefined
cwd?: string | undefined
user?: string | undefined
onStdout?: (chunk: Buffer | string | any) => void
onStderr?: (chunk: Buffer | string | any) => void
sigtermTimeout?: number
},
) => {
if (subcontainer.isOwned()) subcontainer = subcontainer.rc()
const startCommand = () =>
CommandController.of<Manifest>()(
effects,
subcontainer.rc(),
command,
options,
)
return new Oneshot(subcontainer, startCommand, true, [])
}
}
onExitSuccess(fn: () => void) {
if (this.exitedSuccess) {
fn()
} else {
this.onExitSuccessFns.push(fn)
}
}
}