1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | declare module 'cluster' {
|
55 | import * as child from 'node:child_process';
|
56 | import EventEmitter = require('node:events');
|
57 | import * as net from 'node:net';
|
58 | export interface ClusterSettings {
|
59 | execArgv?: string[] | undefined;
|
60 | exec?: string | undefined;
|
61 | args?: string[] | undefined;
|
62 | silent?: boolean | undefined;
|
63 | stdio?: any[] | undefined;
|
64 | uid?: number | undefined;
|
65 | gid?: number | undefined;
|
66 | inspectPort?: number | (() => number) | undefined;
|
67 | }
|
68 | export interface Address {
|
69 | address: string;
|
70 | port: number;
|
71 | addressType: number | 'udp4' | 'udp6'; // 4, 6, -1, "udp4", "udp6"
|
72 | }
|
73 | /**
|
74 | * A `Worker` object contains all public information and method about a worker.
|
75 | * In the primary it can be obtained using `cluster.workers`. In a worker
|
76 | * it can be obtained using `cluster.worker`.
|
77 | * @since v0.7.0
|
78 | */
|
79 | export class Worker extends EventEmitter {
|
80 | /**
|
81 | * Each new worker is given its own unique id, this id is stored in the`id`.
|
82 | *
|
83 | * While a worker is alive, this is the key that indexes it in`cluster.workers`.
|
84 | * @since v0.8.0
|
85 | */
|
86 | id: number;
|
87 | /**
|
88 | * All workers are created using `child_process.fork()`, the returned object
|
89 | * from this function is stored as `.process`. In a worker, the global `process`is stored.
|
90 | *
|
91 | * See: `Child Process module`.
|
92 | *
|
93 | * Workers will call `process.exit(0)` if the `'disconnect'` event occurs
|
94 | * on `process` and `.exitedAfterDisconnect` is not `true`. This protects against
|
95 | * accidental disconnection.
|
96 | * @since v0.7.0
|
97 | */
|
98 | process: child.ChildProcess;
|
99 | /**
|
100 | * Send a message to a worker or primary, optionally with a handle.
|
101 | *
|
102 | * In the primary this sends a message to a specific worker. It is identical to `ChildProcess.send()`.
|
103 | *
|
104 | * In a worker this sends a message to the primary. It is identical to`process.send()`.
|
105 | *
|
106 | * This example will echo back all messages from the primary:
|
107 | *
|
108 | * ```js
|
109 | * if (cluster.isPrimary) {
|
110 | * const worker = cluster.fork();
|
111 | * worker.send('hi there');
|
112 | *
|
113 | * } else if (cluster.isWorker) {
|
114 | * process.on('message', (msg) => {
|
115 | * process.send(msg);
|
116 | * });
|
117 | * }
|
118 | * ```
|
119 | * @since v0.7.0
|
120 | * @param options The `options` argument, if present, is an object used to parameterize the sending of certain types of handles. `options` supports the following properties:
|
121 | */
|
122 | send(message: child.Serializable, callback?: (error: Error | null) => void): boolean;
|
123 | send(message: child.Serializable, sendHandle: child.SendHandle, callback?: (error: Error | null) => void): boolean;
|
124 | send(message: child.Serializable, sendHandle: child.SendHandle, options?: child.MessageOptions, callback?: (error: Error | null) => void): boolean;
|
125 | /**
|
126 | * This function will kill the worker. In the primary, it does this
|
127 | * by disconnecting the `worker.process`, and once disconnected, killing
|
128 | * with `signal`. In the worker, it does it by disconnecting the channel,
|
129 | * and then exiting with code `0`.
|
130 | *
|
131 | * Because `kill()` attempts to gracefully disconnect the worker process, it is
|
132 | * susceptible to waiting indefinitely for the disconnect to complete. For example,
|
133 | * if the worker enters an infinite loop, a graceful disconnect will never occur.
|
134 | * If the graceful disconnect behavior is not needed, use `worker.process.kill()`.
|
135 | *
|
136 | * Causes `.exitedAfterDisconnect` to be set.
|
137 | *
|
138 | * This method is aliased as `worker.destroy()` for backward compatibility.
|
139 | *
|
140 | * In a worker, `process.kill()` exists, but it is not this function;
|
141 | * it is `kill()`.
|
142 | * @since v0.9.12
|
143 | * @param [signal='SIGTERM'] Name of the kill signal to send to the worker process.
|
144 | */
|
145 | kill(signal?: string): void;
|
146 | destroy(signal?: string): void;
|
147 | /**
|
148 | * In a worker, this function will close all servers, wait for the `'close'` event
|
149 | * on those servers, and then disconnect the IPC channel.
|
150 | *
|
151 | * In the primary, an internal message is sent to the worker causing it to call`.disconnect()` on itself.
|
152 | *
|
153 | * Causes `.exitedAfterDisconnect` to be set.
|
154 | *
|
155 | * After a server is closed, it will no longer accept new connections,
|
156 | * but connections may be accepted by any other listening worker. Existing
|
157 | * connections will be allowed to close as usual. When no more connections exist,
|
158 | * see `server.close()`, the IPC channel to the worker will close allowing it
|
159 | * to die gracefully.
|
160 | *
|
161 | * The above applies _only_ to server connections, client connections are not
|
162 | * automatically closed by workers, and disconnect does not wait for them to close
|
163 | * before exiting.
|
164 | *
|
165 | * In a worker, `process.disconnect` exists, but it is not this function;
|
166 | * it is `disconnect()`.
|
167 | *
|
168 | * Because long living server connections may block workers from disconnecting, it
|
169 | * may be useful to send a message, so application specific actions may be taken to
|
170 | * close them. It also may be useful to implement a timeout, killing a worker if
|
171 | * the `'disconnect'` event has not been emitted after some time.
|
172 | *
|
173 | * ```js
|
174 | * if (cluster.isPrimary) {
|
175 | * const worker = cluster.fork();
|
176 | * let timeout;
|
177 | *
|
178 | * worker.on('listening', (address) => {
|
179 | * worker.send('shutdown');
|
180 | * worker.disconnect();
|
181 | * timeout = setTimeout(() => {
|
182 | * worker.kill();
|
183 | * }, 2000);
|
184 | * });
|
185 | *
|
186 | * worker.on('disconnect', () => {
|
187 | * clearTimeout(timeout);
|
188 | * });
|
189 | *
|
190 | * } else if (cluster.isWorker) {
|
191 | * const net = require('net');
|
192 | * const server = net.createServer((socket) => {
|
193 | *
|
194 | * });
|
195 | *
|
196 | * server.listen(8000);
|
197 | *
|
198 | * process.on('message', (msg) => {
|
199 | * if (msg === 'shutdown') {
|
200 | *
|
201 | * }
|
202 | * });
|
203 | * }
|
204 | * ```
|
205 | * @since v0.7.7
|
206 | * @return A reference to `worker`.
|
207 | */
|
208 | disconnect(): void;
|
209 | /**
|
210 | * This function returns `true` if the worker is connected to its primary via its
|
211 | * IPC channel, `false` otherwise. A worker is connected to its primary after it
|
212 | * has been created. It is disconnected after the `'disconnect'` event is emitted.
|
213 | * @since v0.11.14
|
214 | */
|
215 | isConnected(): boolean;
|
216 | /**
|
217 | * This function returns `true` if the worker's process has terminated (either
|
218 | * because of exiting or being signaled). Otherwise, it returns `false`.
|
219 | *
|
220 | * ```js
|
221 | * import cluster from 'cluster';
|
222 | * import http from 'http';
|
223 | * import { cpus } from 'os';
|
224 | * import process from 'process';
|
225 | *
|
226 | * const numCPUs = cpus().length;
|
227 | *
|
228 | * if (cluster.isPrimary) {
|
229 | * console.log(`Primary ${process.pid} is running`);
|
230 | *
|
231 | * // Fork workers.
|
232 | * for (let i = 0; i < numCPUs; i++) {
|
233 | * cluster.fork();
|
234 | * }
|
235 | *
|
236 | * cluster.on('fork', (worker) => {
|
237 | * console.log('worker is dead:', worker.isDead());
|
238 | * });
|
239 | *
|
240 | * cluster.on('exit', (worker, code, signal) => {
|
241 | * console.log('worker is dead:', worker.isDead());
|
242 | * });
|
243 | * } else {
|
244 | * // Workers can share any TCP connection. In this case, it is an HTTP server.
|
245 | * http.createServer((req, res) => {
|
246 | * res.writeHead(200);
|
247 | * res.end(`Current process\n ${process.pid}`);
|
248 | * process.kill(process.pid);
|
249 | * }).listen(8000);
|
250 | * }
|
251 | * ```
|
252 | * @since v0.11.14
|
253 | */
|
254 | isDead(): boolean;
|
255 | /**
|
256 | * This property is `true` if the worker exited due to `.kill()` or`.disconnect()`. If the worker exited any other way, it is `false`. If the
|
257 | * worker has not exited, it is `undefined`.
|
258 | *
|
259 | * The boolean `worker.exitedAfterDisconnect` allows distinguishing between
|
260 | * voluntary and accidental exit, the primary may choose not to respawn a worker
|
261 | * based on this value.
|
262 | *
|
263 | * ```js
|
264 | * cluster.on('exit', (worker, code, signal) => {
|
265 | * if (worker.exitedAfterDisconnect === true) {
|
266 | * console.log('Oh, it was just voluntary – no need to worry');
|
267 | * }
|
268 | * });
|
269 | *
|
270 | * // kill worker
|
271 | * worker.kill();
|
272 | * ```
|
273 | * @since v6.0.0
|
274 | */
|
275 | exitedAfterDisconnect: boolean;
|
276 | /**
|
277 | * events.EventEmitter
|
278 | * 1. disconnect
|
279 | * 2. error
|
280 | * 3. exit
|
281 | * 4. listening
|
282 | * 5. message
|
283 | * 6. online
|
284 | */
|
285 | addListener(event: string, listener: (...args: any[]) => void): this;
|
286 | addListener(event: 'disconnect', listener: () => void): this;
|
287 | addListener(event: 'error', listener: (error: Error) => void): this;
|
288 | addListener(event: 'exit', listener: (code: number, signal: string) => void): this;
|
289 | addListener(event: 'listening', listener: (address: Address) => void): this;
|
290 | addListener(event: 'message', listener: (message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
291 | addListener(event: 'online', listener: () => void): this;
|
292 | emit(event: string | symbol, ...args: any[]): boolean;
|
293 | emit(event: 'disconnect'): boolean;
|
294 | emit(event: 'error', error: Error): boolean;
|
295 | emit(event: 'exit', code: number, signal: string): boolean;
|
296 | emit(event: 'listening', address: Address): boolean;
|
297 | emit(event: 'message', message: any, handle: net.Socket | net.Server): boolean;
|
298 | emit(event: 'online'): boolean;
|
299 | on(event: string, listener: (...args: any[]) => void): this;
|
300 | on(event: 'disconnect', listener: () => void): this;
|
301 | on(event: 'error', listener: (error: Error) => void): this;
|
302 | on(event: 'exit', listener: (code: number, signal: string) => void): this;
|
303 | on(event: 'listening', listener: (address: Address) => void): this;
|
304 | on(event: 'message', listener: (message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
305 | on(event: 'online', listener: () => void): this;
|
306 | once(event: string, listener: (...args: any[]) => void): this;
|
307 | once(event: 'disconnect', listener: () => void): this;
|
308 | once(event: 'error', listener: (error: Error) => void): this;
|
309 | once(event: 'exit', listener: (code: number, signal: string) => void): this;
|
310 | once(event: 'listening', listener: (address: Address) => void): this;
|
311 | once(event: 'message', listener: (message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
312 | once(event: 'online', listener: () => void): this;
|
313 | prependListener(event: string, listener: (...args: any[]) => void): this;
|
314 | prependListener(event: 'disconnect', listener: () => void): this;
|
315 | prependListener(event: 'error', listener: (error: Error) => void): this;
|
316 | prependListener(event: 'exit', listener: (code: number, signal: string) => void): this;
|
317 | prependListener(event: 'listening', listener: (address: Address) => void): this;
|
318 | prependListener(event: 'message', listener: (message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
319 | prependListener(event: 'online', listener: () => void): this;
|
320 | prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
321 | prependOnceListener(event: 'disconnect', listener: () => void): this;
|
322 | prependOnceListener(event: 'error', listener: (error: Error) => void): this;
|
323 | prependOnceListener(event: 'exit', listener: (code: number, signal: string) => void): this;
|
324 | prependOnceListener(event: 'listening', listener: (address: Address) => void): this;
|
325 | prependOnceListener(event: 'message', listener: (message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
326 | prependOnceListener(event: 'online', listener: () => void): this;
|
327 | }
|
328 | export interface Cluster extends EventEmitter {
|
329 | disconnect(callback?: () => void): void;
|
330 | fork(env?: any): Worker;
|
331 | /** @deprecated since v16.0.0 - use isPrimary. */
|
332 | readonly isMaster: boolean;
|
333 | readonly isPrimary: boolean;
|
334 | readonly isWorker: boolean;
|
335 | schedulingPolicy: number;
|
336 | readonly settings: ClusterSettings;
|
337 | /** @deprecated since v16.0.0 - use setupPrimary. */
|
338 | setupMaster(settings?: ClusterSettings): void;
|
339 | /**
|
340 | * `setupPrimary` is used to change the default 'fork' behavior. Once called, the settings will be present in cluster.settings.
|
341 | */
|
342 | setupPrimary(settings?: ClusterSettings): void;
|
343 | readonly worker?: Worker | undefined;
|
344 | readonly workers?: NodeJS.Dict<Worker> | undefined;
|
345 | readonly SCHED_NONE: number;
|
346 | readonly SCHED_RR: number;
|
347 | /**
|
348 | * events.EventEmitter
|
349 | * 1. disconnect
|
350 | * 2. exit
|
351 | * 3. fork
|
352 | * 4. listening
|
353 | * 5. message
|
354 | * 6. online
|
355 | * 7. setup
|
356 | */
|
357 | addListener(event: string, listener: (...args: any[]) => void): this;
|
358 | addListener(event: 'disconnect', listener: (worker: Worker) => void): this;
|
359 | addListener(event: 'exit', listener: (worker: Worker, code: number, signal: string) => void): this;
|
360 | addListener(event: 'fork', listener: (worker: Worker) => void): this;
|
361 | addListener(event: 'listening', listener: (worker: Worker, address: Address) => void): this;
|
362 | addListener(event: 'message', listener: (worker: Worker, message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
363 | addListener(event: 'online', listener: (worker: Worker) => void): this;
|
364 | addListener(event: 'setup', listener: (settings: ClusterSettings) => void): this;
|
365 | emit(event: string | symbol, ...args: any[]): boolean;
|
366 | emit(event: 'disconnect', worker: Worker): boolean;
|
367 | emit(event: 'exit', worker: Worker, code: number, signal: string): boolean;
|
368 | emit(event: 'fork', worker: Worker): boolean;
|
369 | emit(event: 'listening', worker: Worker, address: Address): boolean;
|
370 | emit(event: 'message', worker: Worker, message: any, handle: net.Socket | net.Server): boolean;
|
371 | emit(event: 'online', worker: Worker): boolean;
|
372 | emit(event: 'setup', settings: ClusterSettings): boolean;
|
373 | on(event: string, listener: (...args: any[]) => void): this;
|
374 | on(event: 'disconnect', listener: (worker: Worker) => void): this;
|
375 | on(event: 'exit', listener: (worker: Worker, code: number, signal: string) => void): this;
|
376 | on(event: 'fork', listener: (worker: Worker) => void): this;
|
377 | on(event: 'listening', listener: (worker: Worker, address: Address) => void): this;
|
378 | on(event: 'message', listener: (worker: Worker, message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
379 | on(event: 'online', listener: (worker: Worker) => void): this;
|
380 | on(event: 'setup', listener: (settings: ClusterSettings) => void): this;
|
381 | once(event: string, listener: (...args: any[]) => void): this;
|
382 | once(event: 'disconnect', listener: (worker: Worker) => void): this;
|
383 | once(event: 'exit', listener: (worker: Worker, code: number, signal: string) => void): this;
|
384 | once(event: 'fork', listener: (worker: Worker) => void): this;
|
385 | once(event: 'listening', listener: (worker: Worker, address: Address) => void): this;
|
386 | once(event: 'message', listener: (worker: Worker, message: any, handle: net.Socket | net.Server) => void): this; // the handle is a net.Socket or net.Server object, or undefined.
|
387 | once(event: 'online', listener: (worker: Worker) => void): this;
|
388 | once(event: 'setup', listener: (settings: ClusterSettings) => void): this;
|
389 | prependListener(event: string, listener: (...args: any[]) => void): this;
|
390 | prependListener(event: 'disconnect', listener: (worker: Worker) => void): this;
|
391 | prependListener(event: 'exit', listener: (worker: Worker, code: number, signal: string) => void): this;
|
392 | prependListener(event: 'fork', listener: (worker: Worker) => void): this;
|
393 | prependListener(event: 'listening', listener: (worker: Worker, address: Address) => void): this;
|
394 | // the handle is a net.Socket or net.Server object, or undefined.
|
395 | prependListener(event: 'message', listener: (worker: Worker, message: any, handle?: net.Socket | net.Server) => void): this;
|
396 | prependListener(event: 'online', listener: (worker: Worker) => void): this;
|
397 | prependListener(event: 'setup', listener: (settings: ClusterSettings) => void): this;
|
398 | prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
399 | prependOnceListener(event: 'disconnect', listener: (worker: Worker) => void): this;
|
400 | prependOnceListener(event: 'exit', listener: (worker: Worker, code: number, signal: string) => void): this;
|
401 | prependOnceListener(event: 'fork', listener: (worker: Worker) => void): this;
|
402 | prependOnceListener(event: 'listening', listener: (worker: Worker, address: Address) => void): this;
|
403 | // the handle is a net.Socket or net.Server object, or undefined.
|
404 | prependOnceListener(event: 'message', listener: (worker: Worker, message: any, handle: net.Socket | net.Server) => void): this;
|
405 | prependOnceListener(event: 'online', listener: (worker: Worker) => void): this;
|
406 | prependOnceListener(event: 'setup', listener: (settings: ClusterSettings) => void): this;
|
407 | }
|
408 | const cluster: Cluster;
|
409 | export default cluster;
|
410 | }
|
411 | declare module 'node:cluster' {
|
412 | export * from 'cluster';
|
413 | export { default as default } from 'cluster';
|
414 | }
|
415 |
|
\ | No newline at end of file |