container.ts
Version:
Modular application framework
107 lines (106 loc) • 4.26 kB
TypeScript
/// <reference types="node" />
import * as childProcess from "child_process";
import * as net from "net";
import { IEnvironmentVariables, ILogMetadata, IModuleOptions, Module } from "../../container";
import { BehaviorSubject, Observable, Subject } from "../../container/RxJS";
import { ErrorChain } from "../error";
import { EProcessMessageType, IProcessCallOptions, IProcessEventData, IProcessEventOptions, IProcessMessage, IProcessSend } from "./ChildProcess";
/** Scripts process options. */
export interface IScriptsOptions {
args?: string[];
env?: IEnvironmentVariables;
sockets?: {
parent: net.Socket;
child: net.Socket;
};
}
/** Scripts worker options. */
export interface IScriptsWorkerOptions extends IScriptsOptions {
/** Worker process should restart after exit. */
restart?: boolean;
/** Worker process restarts maximum number of times. */
restartLimit?: number;
/** Maximum script uptime as ISO8601 duration. */
uptimeLimit?: string;
}
/** Scripts worker. */
export interface IScriptsWorker {
process: ScriptsProcess;
unsubscribe$: Subject<void>;
next$: BehaviorSubject<ScriptsProcess>;
restarts: number;
}
/** Scripts error class. */
export declare class ScriptsError extends ErrorChain {
constructor(cause?: Error);
}
/** ScriptsProcess error class. */
export declare class ScriptsProcessError extends ErrorChain {
constructor(target: string, cause?: Error);
}
/** Spawned scripts process interface. */
export declare class ScriptsProcess implements IProcessSend {
readonly scripts: Scripts;
readonly target: string;
readonly process: childProcess.ChildProcess;
readonly options: IScriptsOptions;
readonly exit$: Observable<number | string>;
readonly messages$: Subject<IProcessMessage>;
readonly events$: Subject<IProcessEventData>;
readonly socket?: net.Socket;
readonly isConnected: boolean;
protected currentIdentifier: number;
constructor(scripts: Scripts, target: string, process: childProcess.ChildProcess, options: IScriptsOptions);
/** End child process with signal. */
kill(signal?: string): Observable<number | string>;
/** Send message to child process. */
send(type: EProcessMessageType, data: any): void;
/** Send socket channel to child process. */
sendChannel(name: string, socket: net.Socket): Observable<boolean>;
/** Make call to module.method in child process. */
call<T>(target: string, method: string, options?: IProcessCallOptions): Observable<T>;
/** Send event with optional data to child process. */
event<T>(name: string, options?: IProcessEventOptions<T>): void;
/** Listen for event sent by child process. */
listen<T>(name: string): Observable<T>;
/** Incrementing counter for unique identifiers. */
protected readonly nextIdentifier: number;
/** Handle messages received from child process. */
protected handleMessage(message: IProcessMessage): void;
}
/** Node.js scripts interface. */
export declare class Scripts extends Module {
/** Default module name. */
static readonly moduleName: string;
/** Environment variable names. */
static readonly ENV: {
PATH: string;
};
/** Log names. */
static readonly LOG: {
WORKER_START: string;
WORKER_STOP: string;
WORKER_EXIT: string;
WORKER_RESTART: string;
WORKER_RESTART_LIMIT: string;
WORKER_UPTIME_LIMIT: string;
};
readonly path: string;
readonly workers: {
[name: string]: IScriptsWorker;
};
constructor(options: IModuleOptions);
moduleDown(): void | Observable<void>;
/** Spawn new Node.js process using script file. */
fork(target: string, options?: IScriptsOptions): ScriptsProcess;
startWorker(name: string, target: string, options?: IScriptsWorkerOptions): Observable<ScriptsProcess>;
stopWorker(name: string): Observable<string | number>;
protected readonly envPath: string;
protected getUptimeLimit(limit?: string): number | null;
protected getWorkerLogMetadata(data: {
name: string;
worker: IScriptsWorker;
options?: IScriptsWorkerOptions;
code?: string | number;
}): ILogMetadata;
}