UNPKG

7.32 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import * as events from "events";
4import * as Q from "q";
5import * as stream from "stream";
6
7/** A module for sequencing and executing tasks and dependencies in maximum concurrency
8 */
9declare class Orchestrator extends events.EventEmitter {
10 doneCallback?: ((error?: any) => any) | undefined;
11 isRunning: boolean;
12 seq: any[];
13 tasks: { [name: string]: Orchestrator.Task };
14
15 reset(): Orchestrator;
16
17 /** Define a task
18 * @param name The name of the task.
19 * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete:
20 * - Take in a callback
21 * - Return a stream or a promise
22 */
23 add(name: string, fn?: Orchestrator.TaskFunc): Orchestrator;
24 /** Define a task
25 * @param name The name of the task.
26 * @param deps An array of task names to be executed and completed before your task will run.
27 * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete:
28 * - Take in a callback
29 * - Return a stream or a promise
30 */
31 add(name: string, deps?: string[], fn?: Orchestrator.TaskFunc): Orchestrator;
32
33 task(name: string): Orchestrator.Task;
34 task(name: string, fn: Orchestrator.TaskFunc): void;
35 task(name: string, dep: string[], fn: Orchestrator.TaskFunc): void;
36
37 /** Have you defined a task with this name?
38 * @param name The task name to query
39 */
40 hasTask(name: string): boolean;
41
42 start: Orchestrator.StartMethod;
43
44 stop(err?: any, successfulFinish?: boolean): void;
45
46 sequence: Orchestrator.Sequencify;
47
48 allDone(): boolean;
49
50 /** Listen to orchestrator internals
51 * @param event Event name to listen to:
52 * - start: from start() method, shows you the task sequence
53 * - stop: from stop() method, the queue finished successfully
54 * - err: from stop() method, the queue was aborted due to a task error
55 * - task_start: from _runTask() method, task was started
56 * - task_stop: from _runTask() method, task completed successfully
57 * - task_err: from _runTask() method, task errored
58 * - task_not_found: from start() method, you're trying to start a task that doesn't exist
59 * - task_recursion: from start() method, there are recursive dependencies in your task list
60 * @param cb Passes single argument: e: event details
61 */
62 on(event: Orchestrator.EventNames, cb: (e: Orchestrator.OnCallbackEvent) => any): this;
63
64 /** Listen to all orchestrator events from one callback
65 * @param cb Passes single argument: e: event details
66 */
67 onAll(cb: (e: Orchestrator.OnAllCallbackEvent) => any): void;
68
69 // probably supposed to be private methods, but still available on Orchestrator prototype
70 _resetTask(task: Orchestrator.Task): void;
71
72 _resetAllTasks(): void;
73
74 _resetSpecificTasks(names: string[]): void;
75
76 _runStep(): void;
77
78 _readyToRunTask(task: Orchestrator.Task): boolean;
79
80 _stopTask(task: Orchestrator.Task, meta: Orchestrator.Meta): void;
81
82 _emitTaskDone(task: Orchestrator.Task, message: string, err?: any): void;
83
84 _runTask(task: Orchestrator.Task): void;
85}
86
87declare namespace Orchestrator {
88 type Strings = string | string[];
89
90 /** The method export generated by orchestrator/lib/runTask.js */
91 type RunTask = (task: Orchestrator.TaskFunc, done: (err: any, meta: Orchestrator.Meta) => void) => void;
92
93 /** The module export of the sequencify package: https://www.npmjs.com/package/sequencify */
94 type Sequencify = (tasks: Array<{ dep: string[] }>, names: string[]) => {
95 sequence: string[];
96 missingTasks: string[];
97 recursiveDependencies: string[];
98 };
99
100 /** A task, can either call a callback to indicate task completion or return a promise or a stream: (task is marked complete when promise.then() resolves/fails or stream ends)
101 */
102 type TaskFunc = (callback: (err?: any) => void) => Q.Promise<any> | stream.Stream | any;
103
104 interface AddMethod {
105 /** Define a task
106 * @param name The name of the task.
107 * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete:
108 * - Take in a callback
109 * - Return a stream or a promise
110 */
111 (name: string, fn?: TaskFunc): Orchestrator;
112 /** Define a task
113 * @param name The name of the task.
114 * @param deps An array of task names to be executed and completed before your task will run.
115 * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete:
116 * - Take in a callback
117 * - Return a stream or a promise
118 */
119 (name: string, deps?: string[], fn?: TaskFunc): Orchestrator;
120 }
121
122 /** Start running the tasks
123 */
124 interface StartMethod {
125 /** Start running the tasks
126 * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments.
127 * @param cb Callback to call after run completed.
128 */
129 (tasks: Strings, cb?: (error?: any) => any): Orchestrator;
130 /** Start running the tasks
131 * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments.
132 * @param cb Callback to call after run completed.
133 */
134 (...tasks: Strings[] /*, cb?: (error: any) => any */): Orchestrator;
135 // TODO: TypeScript 2.1.5 cannot express varargs followed by callback as a last argument...
136 (task1: Strings, task2: Strings, cb?: (error?: any) => any): Orchestrator;
137 (task1: Strings, task2: Strings, task3: Strings, cb?: (error?: any) => any): Orchestrator;
138 (task1: Strings, task2: Strings, task3: Strings, task4: Strings, cb?: (error?: any) => any): Orchestrator;
139 (
140 task1: Strings,
141 task2: Strings,
142 task3: Strings,
143 task4: Strings,
144 task5: Strings,
145 cb?: (error?: any) => any,
146 ): Orchestrator;
147 (
148 task1: Strings,
149 task2: Strings,
150 task3: Strings,
151 task4: Strings,
152 task5: Strings,
153 task6: Strings,
154 cb?: (error?: any) => any,
155 ): Orchestrator;
156 }
157
158 interface OnCallbackEvent {
159 message: string;
160 task: string;
161 err: any;
162 duration?: number | undefined;
163 }
164
165 interface OnAllCallbackEvent extends OnCallbackEvent {
166 src: string;
167 }
168
169 interface Task {
170 fn: TaskFunc;
171 dep: string[];
172 name: string;
173 done?: boolean | undefined;
174 duration?: number | undefined;
175 hrDuration?: [number, number] | undefined;
176 running?: boolean | undefined;
177 }
178
179 interface Meta {
180 duration: number;
181 hrDuration: [number, number];
182 runMethod: "callback" | "catch" | "promise" | "stream" | "sync";
183 }
184
185 type EventNames =
186 | "start"
187 | "stop"
188 | "err"
189 | "task_start"
190 | "task_stop"
191 | "task_err"
192 | "task_not_found"
193 | "task_recursion";
194}
195
196export = Orchestrator;