From 0b6077514219b1f530ca10869bb8e91b95d5be03 Mon Sep 17 00:00:00 2001 From: Shun Takahashi Date: Thu, 19 Jan 2017 20:52:18 -0800 Subject: [PATCH] Add strong-cluster-control typing files (#14128) --- strong-cluster-control/index.d.ts | 98 +++++++++++++++++++ .../strong-cluster-control-tests.ts | 30 ++++++ strong-cluster-control/tsconfig.json | 20 ++++ strong-cluster-control/tslint.json | 1 + 4 files changed, 149 insertions(+) create mode 100644 strong-cluster-control/index.d.ts create mode 100644 strong-cluster-control/strong-cluster-control-tests.ts create mode 100644 strong-cluster-control/tsconfig.json create mode 100644 strong-cluster-control/tslint.json diff --git a/strong-cluster-control/index.d.ts b/strong-cluster-control/index.d.ts new file mode 100644 index 0000000000..e0bb92b82f --- /dev/null +++ b/strong-cluster-control/index.d.ts @@ -0,0 +1,98 @@ +// Type definitions for strong-cluster-control 2.2 +// Project: https://github.com/strongloop/strong-cluster-control +// Definitions by: Shun Takahashi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + +declare namespace StrongClusterControl { + type pid = number; + + export interface StartOptions { + size?: number; + env?: {}; + shutdownTimeout?: number; + terminateTimeout?: number; + throttleDelay?: number; + } + + export interface ClusterMaster { + pid: number; + setSize?: number; + startTime: number; + } + + export interface ClusterWorker extends ClusterMaster { + id: number; + } + + export interface ClusterStatus { + master: ClusterMaster; + workers: ClusterWorker[]; + } + + interface CMD { + SHUTDOWN: "CLUSTER_CONTROL_shutdown"; + } + + export interface Control extends NodeJS.EventEmitter { + readonly cmd: CMD; + readonly CPUS: number; + readonly options: StartOptions; + + /** + * @description Start the controller + * @param [options] - An options object, no default, and options object is not required. + * @param [options.size] - Number of workers that should be running, the default is to not control the number of workers + * @param [options.env=null] - Environment properties object passed to cluster.fork() if the controller has to start a worker to resize the cluster, default is null. + * @param [options.shutdownTimeout=5000] - Number of milliseconds to wait after shutdown before terminating a worker, the default is 5 seconds + * @param [options.terminateTimeout=5000] - Number of milliseconds to wait after terminate before killing a worker, the default is 5 seconds + * @param [options.throttoleDelay] - Number of milliseconds to delay restarting workers after they are exiting abnormally. Abnormal is defined as as not suicide. + */ + start(options?: StartOptions, callback?: () => any): this; + start(callback?: () => any): this; + + /** + * @description Stop the controller, after stopping workers (if the size is being controlled, see setSize()). + * @param callback + */ + stop(callback?: () => any): this; + + /** + * @description Restart workers one by one, until all current workers have been restarted. + */ + restart(): this; + + /** + * @description Returns the current cluster status + */ + status(): ClusterStatus; + + /** + * @description Set the size of the cluster. + * @param N - The size of the cluster is the number of workers that should be maintained online. + */ + setSize(N?: number): this; + + /** + * @description Disconnect worker id and take increasingly agressive action until it exits. + * @param id - Cluster worker ID, + */ + shutdown(id: number): this; + + /** + * @description Disconnect worker id and take increasingly agressive action until it exits. + * @param id - Cluster worker ID, + */ + terminate(id: number): this; + + on(event: "start" | "stop" | "restart", handler: () => any): this; + on(event: "setSize" | "resize", handler: (size: number) => any): this; + on(event: "startWorker", handler: (worker: ClusterWorker) => any): this; + on(event: "startRestart", handler: (workers: pid[]) => any): this; + on(event: "stopWorker", handler: (worker: ClusterWorker, code: number, signal: string) => any): this; + on(event: "error", handler: (error: Error | Error[]) => any): this; + } +} + +declare const control: StrongClusterControl.Control; +export = control; diff --git a/strong-cluster-control/strong-cluster-control-tests.ts b/strong-cluster-control/strong-cluster-control-tests.ts new file mode 100644 index 0000000000..9fefd005b8 --- /dev/null +++ b/strong-cluster-control/strong-cluster-control-tests.ts @@ -0,0 +1,30 @@ +import * as control from "strong-cluster-control"; + +control.start({ size: control.CPUS}, (): void => { console.log("starting"); }) + .on("error", (err: Error): void => { + console.error(err); + }); + +control.start((): void => { console.log("staring"); }) + .on("error", (err: Error): void => { + console.error(err); + }); + +control.stop() + .on("stop", (): void => { + console.log("stopped"); + }); + +control.setSize(2) + .on("setSize", (size) => console.log(size)) + .on("resize", (size) => console.log(size)); + +control.restart() + .on("startRestart", (pids) => console.log(`Restarting ${pids.length} workers`)) + .on("restart", () => console.log("restarted")); + +control.shutdown(123) + .on("stopWorker", (worker) => console.log(`Worker ${worker.pid} stopped`)); + +control.terminate(123) + .on("stopWorker", (worker) => console.log(`Worker ${worker.pid} stopped`)); diff --git a/strong-cluster-control/tsconfig.json b/strong-cluster-control/tsconfig.json new file mode 100644 index 0000000000..3623adaea2 --- /dev/null +++ b/strong-cluster-control/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "strong-cluster-control-tests.ts" + ] +} diff --git a/strong-cluster-control/tslint.json b/strong-cluster-control/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/strong-cluster-control/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }