Files
recap/node_modules/@start9labs/start-sdk/package/lib/mainFn/Daemon.d.ts
T

78 lines
3.6 KiB
TypeScript

import * as T from '../../../base/lib/types';
import { Drop } from '../util';
import { SubContainer, SubContainerRc } from '../util/SubContainer';
import { CommandController } from './CommandController';
import { DaemonCommandType } from './Daemons';
import { Oneshot } from './Oneshot';
/**
* A managed long-running process wrapper around {@link CommandController}.
*
* When started, the daemon automatically restarts its underlying command on failure
* with exponential backoff (up to 30 seconds). When stopped, the command is terminated
* gracefully. Implements {@link Drop} for automatic cleanup when the context is left.
*
* @typeParam Manifest - The service manifest type
* @typeParam C - The subcontainer type, or `null` for JS-only daemons
*/
export declare class Daemon<Manifest extends T.SDKManifest, C extends SubContainer<Manifest> | null = SubContainer<Manifest> | null> extends Drop {
private subcontainer;
private startCommand;
readonly oneshot: boolean;
private commandController;
protected exitedSuccess: boolean;
private onExitFns;
private loop;
private _managed;
protected constructor(subcontainer: C, startCommand: () => Promise<CommandController<Manifest, C>>, oneshot?: boolean);
/** Returns true if this daemon is a one-shot process (exits after success) */
isOneshot(): this is Oneshot<Manifest>;
/**
* Factory method to create a new Daemon.
*
* Returns a curried function: `(effects, subcontainer, exec) => Daemon`.
* Registers an `onLeaveContext` callback that terminates the daemon when the
* effects context is left.
*/
static of<Manifest extends T.SDKManifest>(): <C extends SubContainer<Manifest> | null>(effects: T.Effects, subcontainer: C, exec: DaemonCommandType<Manifest, C>) => Daemon<Manifest, SubContainer<Manifest, T.Effects> | null>;
/**
* Start the daemon. If it is already running, this is a no-op.
*
* The daemon will automatically restart on failure with increasing backoff
* until {@link term} is called.
*/
start(): Promise<void>;
private runLoop;
/**
* Terminate the daemon, stopping its underlying command.
*
* Sends the configured signal (default SIGTERM) and waits for the process to exit.
* Optionally destroys the subcontainer after termination.
*
* @param termOptions - Optional termination settings
* @param termOptions.signal - The signal to send (default: SIGTERM)
* @param termOptions.timeout - Milliseconds to wait before SIGKILL
* @param termOptions.destroySubcontainer - Whether to destroy the subcontainer after exit
*/
term(termOptions?: {
signal?: NodeJS.Signals | undefined;
timeout?: number | undefined;
destroySubcontainer?: boolean;
}): Promise<void>;
/**
* Mark this daemon as managed by a {@link Daemons} instance.
* Suppresses the individual `onLeaveContext` termination since the
* `Daemons` instance handles ordered shutdown.
*/
markManaged(): void;
/** Get a reference-counted handle to the daemon's subcontainer, or null if there is none */
subcontainerRc(): SubContainerRc<Manifest> | null;
/** Check whether this daemon shares the same subcontainer as another daemon */
sharesSubcontainerWith(other: Daemon<Manifest, SubContainer<Manifest> | null>): boolean;
/**
* Register a callback to be invoked each time the daemon's process exits.
* @param fn - Callback receiving `true` on clean exit, `false` on error
*/
onExit(fn: (success: boolean) => void): void;
onDrop(): void;
}