@eggjs/cluster 3.0.1 → 3.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -21
- package/dist/dirname.js +11 -0
- package/dist/error/ClusterAgentWorkerError.d.ts +13 -0
- package/dist/error/ClusterAgentWorkerError.js +22 -0
- package/dist/error/ClusterWorkerExceptionError.d.ts +10 -0
- package/dist/error/ClusterWorkerExceptionError.js +17 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +24 -0
- package/dist/master.d.ts +96 -0
- package/dist/master.js +426 -0
- package/dist/utils/messenger.d.ts +96 -0
- package/dist/utils/messenger.js +144 -0
- package/dist/utils/mode/base/agent.d.ts +45 -0
- package/dist/utils/mode/base/agent.js +63 -0
- package/dist/utils/mode/base/app.d.ts +56 -0
- package/dist/utils/mode/base/app.js +77 -0
- package/dist/utils/mode/impl/process/agent.d.ts +22 -0
- package/dist/utils/mode/impl/process/agent.js +93 -0
- package/dist/utils/mode/impl/process/app.d.ts +12 -0
- package/dist/utils/mode/impl/process/app.js +117 -0
- package/dist/utils/mode/impl/worker_threads/agent.d.ts +22 -0
- package/dist/utils/mode/impl/worker_threads/agent.js +79 -0
- package/dist/utils/mode/impl/worker_threads/app.d.ts +13 -0
- package/dist/utils/mode/impl/worker_threads/app.js +128 -0
- package/dist/utils/options.d.ts +83 -0
- package/dist/utils/options.js +56 -0
- package/dist/utils/terminate.js +62 -0
- package/dist/utils/worker_manager.d.ts +32 -0
- package/dist/utils/worker_manager.js +68 -0
- package/package.json +35 -61
- package/dist/commonjs/agent_worker.d.ts +0 -1
- package/dist/commonjs/agent_worker.js +0 -69
- package/dist/commonjs/app_worker.d.ts +0 -1
- package/dist/commonjs/app_worker.js +0 -173
- package/dist/commonjs/dirname.d.ts +0 -1
- package/dist/commonjs/dirname.js +0 -17
- package/dist/commonjs/error/ClusterAgentWorkerError.d.ts +0 -10
- package/dist/commonjs/error/ClusterAgentWorkerError.js +0 -23
- package/dist/commonjs/error/ClusterWorkerExceptionError.d.ts +0 -7
- package/dist/commonjs/error/ClusterWorkerExceptionError.js +0 -18
- package/dist/commonjs/error/index.d.ts +0 -2
- package/dist/commonjs/error/index.js +0 -19
- package/dist/commonjs/index.d.ts +0 -17
- package/dist/commonjs/index.js +0 -37
- package/dist/commonjs/master.d.ts +0 -90
- package/dist/commonjs/master.js +0 -560
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/utils/messenger.d.ts +0 -92
- package/dist/commonjs/utils/messenger.js +0 -186
- package/dist/commonjs/utils/mode/base/agent.d.ts +0 -38
- package/dist/commonjs/utils/mode/base/agent.js +0 -68
- package/dist/commonjs/utils/mode/base/app.d.ts +0 -48
- package/dist/commonjs/utils/mode/base/app.js +0 -83
- package/dist/commonjs/utils/mode/impl/process/agent.d.ts +0 -18
- package/dist/commonjs/utils/mode/impl/process/agent.js +0 -108
- package/dist/commonjs/utils/mode/impl/process/app.d.ts +0 -21
- package/dist/commonjs/utils/mode/impl/process/app.js +0 -127
- package/dist/commonjs/utils/mode/impl/worker_threads/agent.d.ts +0 -18
- package/dist/commonjs/utils/mode/impl/worker_threads/agent.js +0 -91
- package/dist/commonjs/utils/mode/impl/worker_threads/app.d.ts +0 -26
- package/dist/commonjs/utils/mode/impl/worker_threads/app.js +0 -142
- package/dist/commonjs/utils/options.d.ts +0 -80
- package/dist/commonjs/utils/options.js +0 -83
- package/dist/commonjs/utils/terminate.d.ts +0 -6
- package/dist/commonjs/utils/terminate.js +0 -89
- package/dist/commonjs/utils/worker_manager.d.ts +0 -25
- package/dist/commonjs/utils/worker_manager.js +0 -76
- package/dist/esm/agent_worker.d.ts +0 -1
- package/dist/esm/agent_worker.js +0 -67
- package/dist/esm/app_worker.d.ts +0 -1
- package/dist/esm/app_worker.js +0 -168
- package/dist/esm/dirname.d.ts +0 -1
- package/dist/esm/dirname.js +0 -11
- package/dist/esm/error/ClusterAgentWorkerError.d.ts +0 -10
- package/dist/esm/error/ClusterAgentWorkerError.js +0 -19
- package/dist/esm/error/ClusterWorkerExceptionError.d.ts +0 -7
- package/dist/esm/error/ClusterWorkerExceptionError.js +0 -14
- package/dist/esm/error/index.d.ts +0 -2
- package/dist/esm/error/index.js +0 -3
- package/dist/esm/index.d.ts +0 -17
- package/dist/esm/index.js +0 -19
- package/dist/esm/master.d.ts +0 -90
- package/dist/esm/master.js +0 -553
- package/dist/esm/package.json +0 -3
- package/dist/esm/utils/messenger.d.ts +0 -92
- package/dist/esm/utils/messenger.js +0 -179
- package/dist/esm/utils/mode/base/agent.d.ts +0 -38
- package/dist/esm/utils/mode/base/agent.js +0 -60
- package/dist/esm/utils/mode/base/app.d.ts +0 -48
- package/dist/esm/utils/mode/base/app.js +0 -75
- package/dist/esm/utils/mode/impl/process/agent.d.ts +0 -18
- package/dist/esm/utils/mode/impl/process/agent.js +0 -103
- package/dist/esm/utils/mode/impl/process/app.d.ts +0 -21
- package/dist/esm/utils/mode/impl/process/app.js +0 -119
- package/dist/esm/utils/mode/impl/worker_threads/agent.d.ts +0 -18
- package/dist/esm/utils/mode/impl/worker_threads/agent.js +0 -83
- package/dist/esm/utils/mode/impl/worker_threads/app.d.ts +0 -26
- package/dist/esm/utils/mode/impl/worker_threads/app.js +0 -137
- package/dist/esm/utils/options.d.ts +0 -80
- package/dist/esm/utils/options.js +0 -77
- package/dist/esm/utils/terminate.d.ts +0 -6
- package/dist/esm/utils/terminate.js +0 -86
- package/dist/esm/utils/worker_manager.d.ts +0 -25
- package/dist/esm/utils/worker_manager.js +0 -72
- package/dist/package.json +0 -4
- package/src/agent_worker.ts +0 -80
- package/src/app_worker.ts +0 -196
- package/src/dirname.ts +0 -11
- package/src/error/ClusterAgentWorkerError.ts +0 -19
- package/src/error/ClusterWorkerExceptionError.ts +0 -17
- package/src/error/index.ts +0 -2
- package/src/index.ts +0 -26
- package/src/master.ts +0 -658
- package/src/utils/messenger.ts +0 -207
- package/src/utils/mode/base/agent.ts +0 -90
- package/src/utils/mode/base/app.ts +0 -119
- package/src/utils/mode/impl/process/agent.ts +0 -119
- package/src/utils/mode/impl/process/app.ts +0 -140
- package/src/utils/mode/impl/worker_threads/agent.ts +0 -99
- package/src/utils/mode/impl/worker_threads/app.ts +0 -164
- package/src/utils/options.ts +0 -171
- package/src/utils/terminate.ts +0 -97
- package/src/utils/worker_manager.ts +0 -87
package/README.md
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @eggjs/cluster
|
|
2
2
|
|
|
3
3
|
[![NPM version][npm-image]][npm-url]
|
|
4
|
-
[](https://github.com/eggjs/cluster/actions/workflows/nodejs.yml)
|
|
5
|
-
[![Test coverage][codecov-image]][codecov-url]
|
|
6
4
|
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
7
5
|
[![npm download][download-image]][download-url]
|
|
8
6
|
[](https://nodejs.org/en/download/)
|
|
9
7
|
|
|
10
8
|
[npm-image]: https://img.shields.io/npm/v/@eggjs/cluster.svg?style=flat-square
|
|
11
9
|
[npm-url]: https://npmjs.org/package/@eggjs/cluster
|
|
12
|
-
[codecov-image]: https://codecov.io/github/eggjs/cluster/coverage.svg?branch=master
|
|
13
|
-
[codecov-url]: https://codecov.io/github/eggjs/cluster?branch=master
|
|
14
10
|
[snyk-image]: https://snyk.io/test/npm/@eggjs/cluster/badge.svg?style=flat-square
|
|
15
11
|
[snyk-url]: https://snyk.io/test/npm/@eggjs/cluster
|
|
16
12
|
[download-image]: https://img.shields.io/npm/dm/@eggjs/cluster.svg?style=flat-square
|
|
@@ -59,21 +55,21 @@ startCluster({
|
|
|
59
55
|
|
|
60
56
|
## Options
|
|
61
57
|
|
|
62
|
-
| Param
|
|
63
|
-
|
|
|
64
|
-
| baseDir
|
|
65
|
-
| framework
|
|
66
|
-
| plugins
|
|
67
|
-
| workers
|
|
68
|
-
| sticky
|
|
69
|
-
| port
|
|
70
|
-
| debugPort
|
|
71
|
-
| https
|
|
72
|
-
| require
|
|
73
|
-
| pidFile
|
|
74
|
-
| startMode
|
|
75
|
-
| ports
|
|
76
|
-
| env
|
|
58
|
+
| Param | Type | Description |
|
|
59
|
+
| --------- | --------------- | ----------------------------------------------------------------------------------------------------------------- |
|
|
60
|
+
| baseDir | `String` | directory of application |
|
|
61
|
+
| framework | `String` | specify framework that can be absolute path or npm package |
|
|
62
|
+
| plugins | `Object` | plugins for unittest |
|
|
63
|
+
| workers | `Number` | numbers of app workers |
|
|
64
|
+
| sticky | `Boolean` | sticky mode server |
|
|
65
|
+
| port | `Number` | port |
|
|
66
|
+
| debugPort | `Number` | the debug port only listen on http protocol |
|
|
67
|
+
| https | `Object` | start a https server, note: `key` / `cert` / `ca` should be full path to file |
|
|
68
|
+
| require | `Array\|String` | will inject into worker/agent process |
|
|
69
|
+
| pidFile | `String` | will save master pid to this file |
|
|
70
|
+
| startMode | `String` | default is 'process', use 'worker_threads' to start the app & agent worker by worker_threads |
|
|
71
|
+
| ports | `Array` | startup port of each app worker, such as: [7001, 7002, 7003], only effects when the startMode is 'worker_threads' |
|
|
72
|
+
| env | `String` | custom env, default is process.env.EGG_SERVER_ENV |
|
|
77
73
|
|
|
78
74
|
## Env
|
|
79
75
|
|
|
@@ -87,6 +83,6 @@ startCluster({
|
|
|
87
83
|
|
|
88
84
|
## Contributors
|
|
89
85
|
|
|
90
|
-
[](https://github.com/eggjs/egg/graphs/contributors)
|
|
91
87
|
|
|
92
88
|
Made with [contributors-img](https://contrib.rocks).
|
package/dist/dirname.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
//#region src/dirname.ts
|
|
5
|
+
function getSrcDirname() {
|
|
6
|
+
if (typeof __dirname !== "undefined") return __dirname;
|
|
7
|
+
return path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { getSrcDirname };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/error/ClusterAgentWorkerError.d.ts
|
|
2
|
+
declare class ClusterAgentWorkerError extends Error {
|
|
3
|
+
id: number;
|
|
4
|
+
/**
|
|
5
|
+
* pid in process mode
|
|
6
|
+
* tid in worker_threads mode
|
|
7
|
+
*/
|
|
8
|
+
workerId: number;
|
|
9
|
+
status: string;
|
|
10
|
+
constructor(id: number, workerId: number, status: string, error: Error);
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { ClusterAgentWorkerError };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/error/ClusterAgentWorkerError.ts
|
|
2
|
+
var ClusterAgentWorkerError = class extends Error {
|
|
3
|
+
id;
|
|
4
|
+
/**
|
|
5
|
+
* pid in process mode
|
|
6
|
+
* tid in worker_threads mode
|
|
7
|
+
*/
|
|
8
|
+
workerId;
|
|
9
|
+
status;
|
|
10
|
+
constructor(id, workerId, status, error) {
|
|
11
|
+
const message = `Got agent worker error: ${error.message}`;
|
|
12
|
+
super(message, { cause: error });
|
|
13
|
+
this.name = this.constructor.name;
|
|
14
|
+
this.id = id;
|
|
15
|
+
this.workerId = workerId;
|
|
16
|
+
this.status = status;
|
|
17
|
+
Error.captureStackTrace(this, this.constructor);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { ClusterAgentWorkerError };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/error/ClusterWorkerExceptionError.d.ts
|
|
2
|
+
declare class ClusterWorkerExceptionError extends Error {
|
|
3
|
+
count: {
|
|
4
|
+
agent: number;
|
|
5
|
+
worker: number;
|
|
6
|
+
};
|
|
7
|
+
constructor(agent: number, worker: number);
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { ClusterWorkerExceptionError };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/error/ClusterWorkerExceptionError.ts
|
|
2
|
+
var ClusterWorkerExceptionError = class extends Error {
|
|
3
|
+
count;
|
|
4
|
+
constructor(agent, worker) {
|
|
5
|
+
const message = `[master] ${agent} agent and ${worker} worker(s) alive, exit to avoid unknown state`;
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
this.count = {
|
|
9
|
+
agent,
|
|
10
|
+
worker
|
|
11
|
+
};
|
|
12
|
+
Error.captureStackTrace(this, this.constructor);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { ClusterWorkerExceptionError };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ClusterHTTPSSecureOptions, ClusterOptions, ClusterStartMode } from "./utils/options.js";
|
|
2
|
+
import { Master, MasterOptions } from "./master.js";
|
|
3
|
+
import { ClusterAgentWorkerError } from "./error/ClusterAgentWorkerError.js";
|
|
4
|
+
import { ClusterWorkerExceptionError } from "./error/ClusterWorkerExceptionError.js";
|
|
5
|
+
|
|
6
|
+
//#region src/index.d.ts
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* cluster start flow:
|
|
10
|
+
*
|
|
11
|
+
* [startCluster] -> master -> agent_worker -> new [Agent] -> agentWorkerLoader
|
|
12
|
+
* `-> app_worker -> new [Application] -> appWorkerLoader
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* start egg app
|
|
17
|
+
* @function Egg#startCluster
|
|
18
|
+
* @param {Object} options {@link Master}
|
|
19
|
+
*/
|
|
20
|
+
declare function startCluster(options: ClusterOptions): Promise<void>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { ClusterAgentWorkerError, type ClusterHTTPSSecureOptions, type ClusterOptions, type ClusterStartMode, ClusterWorkerExceptionError, Master, type MasterOptions, startCluster };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import "./utils/options.js";
|
|
2
|
+
import { ClusterAgentWorkerError } from "./error/ClusterAgentWorkerError.js";
|
|
3
|
+
import { ClusterWorkerExceptionError } from "./error/ClusterWorkerExceptionError.js";
|
|
4
|
+
import { Master } from "./master.js";
|
|
5
|
+
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
/**
|
|
8
|
+
* cluster start flow:
|
|
9
|
+
*
|
|
10
|
+
* [startCluster] -> master -> agent_worker -> new [Agent] -> agentWorkerLoader
|
|
11
|
+
* `-> app_worker -> new [Application] -> appWorkerLoader
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* start egg app
|
|
16
|
+
* @function Egg#startCluster
|
|
17
|
+
* @param {Object} options {@link Master}
|
|
18
|
+
*/
|
|
19
|
+
async function startCluster(options) {
|
|
20
|
+
await new Master(options).ready();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { ClusterAgentWorkerError, ClusterWorkerExceptionError, Master, startCluster };
|
package/dist/master.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { WorkerManager } from "./utils/worker_manager.js";
|
|
2
|
+
import { Messenger } from "./utils/messenger.js";
|
|
3
|
+
import { BaseAppWorker } from "./utils/mode/base/app.js";
|
|
4
|
+
import { ClusterOptions, ParsedClusterOptions } from "./utils/options.js";
|
|
5
|
+
import { AgentProcessUtils } from "./utils/mode/impl/process/agent.js";
|
|
6
|
+
import { AppProcessUtils } from "./utils/mode/impl/process/app.js";
|
|
7
|
+
import { AgentThreadUtils } from "./utils/mode/impl/worker_threads/agent.js";
|
|
8
|
+
import { AppThreadUtils } from "./utils/mode/impl/worker_threads/app.js";
|
|
9
|
+
import { ReadyEventEmitter } from "get-ready";
|
|
10
|
+
import { EggConsoleLogger } from "egg-logger";
|
|
11
|
+
import * as worker_threads0 from "worker_threads";
|
|
12
|
+
import * as cluster0 from "cluster";
|
|
13
|
+
|
|
14
|
+
//#region src/master.d.ts
|
|
15
|
+
interface MasterOptions extends ParsedClusterOptions {
|
|
16
|
+
clusterPort?: number;
|
|
17
|
+
stickyWorkerPort?: number;
|
|
18
|
+
}
|
|
19
|
+
declare class Master extends ReadyEventEmitter {
|
|
20
|
+
#private;
|
|
21
|
+
options: MasterOptions;
|
|
22
|
+
isStarted: boolean;
|
|
23
|
+
workerManager: WorkerManager;
|
|
24
|
+
messenger: Messenger;
|
|
25
|
+
isProduction: boolean;
|
|
26
|
+
agentWorkerIndex: number;
|
|
27
|
+
closed: boolean;
|
|
28
|
+
logger: EggConsoleLogger;
|
|
29
|
+
agentWorker: AgentProcessUtils | AgentThreadUtils;
|
|
30
|
+
appWorker: AppProcessUtils | AppThreadUtils;
|
|
31
|
+
constructor(options?: ClusterOptions);
|
|
32
|
+
startByProcess(): void;
|
|
33
|
+
startByWorkerThreads(): void;
|
|
34
|
+
detectPorts(): Promise<void>;
|
|
35
|
+
log(msg: string, ...args: any[]): void;
|
|
36
|
+
startMasterSocketServer(cb: (err?: Error) => void): void;
|
|
37
|
+
stickyWorker(ip: string): BaseAppWorker<worker_threads0.Worker | cluster0.Worker>;
|
|
38
|
+
forkAgentWorker(): void;
|
|
39
|
+
forkAppWorkers(): void;
|
|
40
|
+
/**
|
|
41
|
+
* close agent worker, App Worker will closed by cluster
|
|
42
|
+
*
|
|
43
|
+
* https://www.exratione.com/2013/05/die-child-process-die/
|
|
44
|
+
* make sure Agent Worker exit before master exit
|
|
45
|
+
*
|
|
46
|
+
* @param {number} timeout - kill agent timeout
|
|
47
|
+
* @return {Promise} -
|
|
48
|
+
*/
|
|
49
|
+
killAgentWorker(timeout: number): Promise<void>;
|
|
50
|
+
killAppWorkers(timeout: number): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Agent Worker exit handler
|
|
53
|
+
* Will exit during startup, and refork during running.
|
|
54
|
+
*/
|
|
55
|
+
onAgentExit(data: {
|
|
56
|
+
/** exit code */
|
|
57
|
+
code: number;
|
|
58
|
+
/** received signal */
|
|
59
|
+
signal: string;
|
|
60
|
+
}): void;
|
|
61
|
+
onAgentStart(): void;
|
|
62
|
+
/**
|
|
63
|
+
* App Worker exit handler
|
|
64
|
+
*/
|
|
65
|
+
onAppExit(data: {
|
|
66
|
+
workerId: number;
|
|
67
|
+
code: number;
|
|
68
|
+
signal: string;
|
|
69
|
+
}): void;
|
|
70
|
+
/**
|
|
71
|
+
* after app worker
|
|
72
|
+
*/
|
|
73
|
+
onAppStart(data: {
|
|
74
|
+
workerId: number;
|
|
75
|
+
address: ListeningAddress;
|
|
76
|
+
}): void;
|
|
77
|
+
/**
|
|
78
|
+
* master exit handler
|
|
79
|
+
*/
|
|
80
|
+
onExit(code: number): void;
|
|
81
|
+
onSignal(signal: string): void;
|
|
82
|
+
/**
|
|
83
|
+
* reload workers, for develop purpose
|
|
84
|
+
*/
|
|
85
|
+
onReload(): void;
|
|
86
|
+
close(): Promise<void>;
|
|
87
|
+
_doClose(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
interface ListeningAddress {
|
|
90
|
+
port: number;
|
|
91
|
+
protocol: string;
|
|
92
|
+
address?: string;
|
|
93
|
+
addressType?: number;
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { Master, MasterOptions };
|