@eggjs/cluster 3.0.1 → 3.1.0-beta.11

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.
Files changed (127) hide show
  1. package/README.md +17 -21
  2. package/dist/agent_worker.d.ts +1 -0
  3. package/dist/agent_worker.js +55 -0
  4. package/dist/app_worker.d.ts +1 -0
  5. package/dist/app_worker.js +131 -0
  6. package/dist/dirname.js +11 -0
  7. package/dist/error/ClusterAgentWorkerError.d.ts +13 -0
  8. package/dist/error/ClusterAgentWorkerError.js +22 -0
  9. package/dist/error/ClusterWorkerExceptionError.d.ts +10 -0
  10. package/dist/error/ClusterWorkerExceptionError.js +17 -0
  11. package/dist/index.d.ts +22 -0
  12. package/dist/index.js +24 -0
  13. package/dist/master.d.ts +96 -0
  14. package/dist/master.js +426 -0
  15. package/dist/utils/messenger.d.ts +96 -0
  16. package/dist/utils/messenger.js +144 -0
  17. package/dist/utils/mode/base/agent.d.ts +45 -0
  18. package/dist/utils/mode/base/agent.js +63 -0
  19. package/dist/utils/mode/base/app.d.ts +56 -0
  20. package/dist/utils/mode/base/app.js +77 -0
  21. package/dist/utils/mode/impl/process/agent.d.ts +22 -0
  22. package/dist/utils/mode/impl/process/agent.js +93 -0
  23. package/dist/utils/mode/impl/process/app.d.ts +12 -0
  24. package/dist/utils/mode/impl/process/app.js +117 -0
  25. package/dist/utils/mode/impl/worker_threads/agent.d.ts +22 -0
  26. package/dist/utils/mode/impl/worker_threads/agent.js +79 -0
  27. package/dist/utils/mode/impl/worker_threads/app.d.ts +13 -0
  28. package/dist/utils/mode/impl/worker_threads/app.js +128 -0
  29. package/dist/utils/options.d.ts +83 -0
  30. package/dist/utils/options.js +56 -0
  31. package/dist/utils/terminate.js +62 -0
  32. package/dist/utils/worker_manager.d.ts +32 -0
  33. package/dist/utils/worker_manager.js +68 -0
  34. package/package.json +41 -63
  35. package/dist/commonjs/agent_worker.d.ts +0 -1
  36. package/dist/commonjs/agent_worker.js +0 -69
  37. package/dist/commonjs/app_worker.d.ts +0 -1
  38. package/dist/commonjs/app_worker.js +0 -173
  39. package/dist/commonjs/dirname.d.ts +0 -1
  40. package/dist/commonjs/dirname.js +0 -17
  41. package/dist/commonjs/error/ClusterAgentWorkerError.d.ts +0 -10
  42. package/dist/commonjs/error/ClusterAgentWorkerError.js +0 -23
  43. package/dist/commonjs/error/ClusterWorkerExceptionError.d.ts +0 -7
  44. package/dist/commonjs/error/ClusterWorkerExceptionError.js +0 -18
  45. package/dist/commonjs/error/index.d.ts +0 -2
  46. package/dist/commonjs/error/index.js +0 -19
  47. package/dist/commonjs/index.d.ts +0 -17
  48. package/dist/commonjs/index.js +0 -37
  49. package/dist/commonjs/master.d.ts +0 -90
  50. package/dist/commonjs/master.js +0 -560
  51. package/dist/commonjs/package.json +0 -3
  52. package/dist/commonjs/utils/messenger.d.ts +0 -92
  53. package/dist/commonjs/utils/messenger.js +0 -186
  54. package/dist/commonjs/utils/mode/base/agent.d.ts +0 -38
  55. package/dist/commonjs/utils/mode/base/agent.js +0 -68
  56. package/dist/commonjs/utils/mode/base/app.d.ts +0 -48
  57. package/dist/commonjs/utils/mode/base/app.js +0 -83
  58. package/dist/commonjs/utils/mode/impl/process/agent.d.ts +0 -18
  59. package/dist/commonjs/utils/mode/impl/process/agent.js +0 -108
  60. package/dist/commonjs/utils/mode/impl/process/app.d.ts +0 -21
  61. package/dist/commonjs/utils/mode/impl/process/app.js +0 -127
  62. package/dist/commonjs/utils/mode/impl/worker_threads/agent.d.ts +0 -18
  63. package/dist/commonjs/utils/mode/impl/worker_threads/agent.js +0 -91
  64. package/dist/commonjs/utils/mode/impl/worker_threads/app.d.ts +0 -26
  65. package/dist/commonjs/utils/mode/impl/worker_threads/app.js +0 -142
  66. package/dist/commonjs/utils/options.d.ts +0 -80
  67. package/dist/commonjs/utils/options.js +0 -83
  68. package/dist/commonjs/utils/terminate.d.ts +0 -6
  69. package/dist/commonjs/utils/terminate.js +0 -89
  70. package/dist/commonjs/utils/worker_manager.d.ts +0 -25
  71. package/dist/commonjs/utils/worker_manager.js +0 -76
  72. package/dist/esm/agent_worker.d.ts +0 -1
  73. package/dist/esm/agent_worker.js +0 -67
  74. package/dist/esm/app_worker.d.ts +0 -1
  75. package/dist/esm/app_worker.js +0 -168
  76. package/dist/esm/dirname.d.ts +0 -1
  77. package/dist/esm/dirname.js +0 -11
  78. package/dist/esm/error/ClusterAgentWorkerError.d.ts +0 -10
  79. package/dist/esm/error/ClusterAgentWorkerError.js +0 -19
  80. package/dist/esm/error/ClusterWorkerExceptionError.d.ts +0 -7
  81. package/dist/esm/error/ClusterWorkerExceptionError.js +0 -14
  82. package/dist/esm/error/index.d.ts +0 -2
  83. package/dist/esm/error/index.js +0 -3
  84. package/dist/esm/index.d.ts +0 -17
  85. package/dist/esm/index.js +0 -19
  86. package/dist/esm/master.d.ts +0 -90
  87. package/dist/esm/master.js +0 -553
  88. package/dist/esm/package.json +0 -3
  89. package/dist/esm/utils/messenger.d.ts +0 -92
  90. package/dist/esm/utils/messenger.js +0 -179
  91. package/dist/esm/utils/mode/base/agent.d.ts +0 -38
  92. package/dist/esm/utils/mode/base/agent.js +0 -60
  93. package/dist/esm/utils/mode/base/app.d.ts +0 -48
  94. package/dist/esm/utils/mode/base/app.js +0 -75
  95. package/dist/esm/utils/mode/impl/process/agent.d.ts +0 -18
  96. package/dist/esm/utils/mode/impl/process/agent.js +0 -103
  97. package/dist/esm/utils/mode/impl/process/app.d.ts +0 -21
  98. package/dist/esm/utils/mode/impl/process/app.js +0 -119
  99. package/dist/esm/utils/mode/impl/worker_threads/agent.d.ts +0 -18
  100. package/dist/esm/utils/mode/impl/worker_threads/agent.js +0 -83
  101. package/dist/esm/utils/mode/impl/worker_threads/app.d.ts +0 -26
  102. package/dist/esm/utils/mode/impl/worker_threads/app.js +0 -137
  103. package/dist/esm/utils/options.d.ts +0 -80
  104. package/dist/esm/utils/options.js +0 -77
  105. package/dist/esm/utils/terminate.d.ts +0 -6
  106. package/dist/esm/utils/terminate.js +0 -86
  107. package/dist/esm/utils/worker_manager.d.ts +0 -25
  108. package/dist/esm/utils/worker_manager.js +0 -72
  109. package/dist/package.json +0 -4
  110. package/src/agent_worker.ts +0 -80
  111. package/src/app_worker.ts +0 -196
  112. package/src/dirname.ts +0 -11
  113. package/src/error/ClusterAgentWorkerError.ts +0 -19
  114. package/src/error/ClusterWorkerExceptionError.ts +0 -17
  115. package/src/error/index.ts +0 -2
  116. package/src/index.ts +0 -26
  117. package/src/master.ts +0 -658
  118. package/src/utils/messenger.ts +0 -207
  119. package/src/utils/mode/base/agent.ts +0 -90
  120. package/src/utils/mode/base/app.ts +0 -119
  121. package/src/utils/mode/impl/process/agent.ts +0 -119
  122. package/src/utils/mode/impl/process/app.ts +0 -140
  123. package/src/utils/mode/impl/worker_threads/agent.ts +0 -99
  124. package/src/utils/mode/impl/worker_threads/app.ts +0 -164
  125. package/src/utils/options.ts +0 -171
  126. package/src/utils/terminate.ts +0 -97
  127. package/src/utils/worker_manager.ts +0 -87
package/README.md CHANGED
@@ -1,16 +1,12 @@
1
- # egg-cluster
1
+ # @eggjs/cluster
2
2
 
3
3
  [![NPM version][npm-image]][npm-url]
4
- [![CI](https://github.com/eggjs/cluster/actions/workflows/nodejs.yml/badge.svg)](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
  [![Node.js Version](https://img.shields.io/node/v/@eggjs/cluster.svg?style=flat)](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 | Type | Description |
63
- | ------------ | --------- | ---------------------------------------- |
64
- | baseDir | `String` | directory of application |
65
- | framework | `String` | specify framework that can be absolute path or npm package |
66
- | plugins | `Object` | plugins for unittest |
67
- | workers | `Number` | numbers of app workers |
68
- | sticky | `Boolean` | sticky mode server |
69
- | port | `Number` | port |
70
- | debugPort | `Number` | the debug port only listen on http protocol |
71
- | https | `Object` | start a https server, note: `key` / `cert` / `ca` should be full path to file |
72
- | require | `Array\|String` | will inject into worker/agent process |
73
- | pidFile | `String` | will save master pid to this file |
74
- | startMode | `String` | default is 'process', use 'worker_threads' to start the app & agent worker by worker_threads |
75
- | ports | `Array` | startup port of each app worker, such as: [7001, 7002, 7003], only effects when the startMode is 'worker_threads' |
76
- | env | `String` | custom env, default is process.env.EGG_SERVER_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
- [![Contributors](https://contrib.rocks/image?repo=eggjs/cluster)](https://github.com/eggjs/cluster/graphs/contributors)
86
+ [![Contributors](https://contrib.rocks/image?repo=eggjs/egg)](https://github.com/eggjs/egg/graphs/contributors)
91
87
 
92
88
  Made with [contributors-img](https://contrib.rocks).
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,55 @@
1
+ import { AgentProcessWorker } from "./utils/mode/impl/process/agent.js";
2
+ import { AgentThreadWorker } from "./utils/mode/impl/worker_threads/agent.js";
3
+ import { debuglog } from "node:util";
4
+ import { EggConsoleLogger } from "egg-logger";
5
+ import { importModule } from "@eggjs/utils";
6
+
7
+ //#region src/agent_worker.ts
8
+ const debug = debuglog("egg/cluster/agent_worker");
9
+ /**
10
+ * agent worker is child_process forked by master.
11
+ *
12
+ * agent worker only exit in two cases:
13
+ * - receive signal SIGTERM, exit code 0 (exit gracefully)
14
+ * - receive disconnect event, exit code 110 (maybe master exit in accident)
15
+ */
16
+ async function main() {
17
+ const options = JSON.parse(process.argv[2]);
18
+ if (options.require) for (const mod of options.require) await importModule(mod, { paths: [options.baseDir] });
19
+ let AgentWorker;
20
+ if (options.startMode === "worker_threads") AgentWorker = AgentThreadWorker;
21
+ else AgentWorker = AgentProcessWorker;
22
+ const consoleLogger = new EggConsoleLogger({ level: process.env.EGG_AGENT_WORKER_LOGGER_LEVEL });
23
+ const { Agent } = await importModule(options.framework, { paths: [options.baseDir] });
24
+ debug("new Agent with options %j", options);
25
+ let agent;
26
+ try {
27
+ agent = new Agent(options);
28
+ } catch (err) {
29
+ consoleLogger.error(err);
30
+ throw err;
31
+ }
32
+ function startErrorHandler(err) {
33
+ consoleLogger.error(err);
34
+ consoleLogger.error("[agent_worker] start error, exiting with code:1");
35
+ AgentWorker.kill();
36
+ }
37
+ agent.ready((err) => {
38
+ if (err) return;
39
+ agent.removeListener("error", startErrorHandler);
40
+ AgentWorker.send({
41
+ action: "agent-start",
42
+ to: "master"
43
+ });
44
+ });
45
+ agent.once("error", startErrorHandler);
46
+ AgentWorker.gracefulExit({
47
+ logger: consoleLogger,
48
+ label: "agent_worker",
49
+ beforeExit: () => agent.close()
50
+ });
51
+ }
52
+ main();
53
+
54
+ //#endregion
55
+ export { };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,131 @@
1
+ import { AppProcessWorker } from "./utils/mode/impl/process/app.js";
2
+ import { AppThreadWorker } from "./utils/mode/impl/worker_threads/app.js";
3
+ import { debuglog } from "node:util";
4
+ import fs from "node:fs";
5
+ import { EggConsoleLogger } from "egg-logger";
6
+ import { importModule } from "@eggjs/utils";
7
+ import { createServer } from "node:http";
8
+ import { createServer as createServer$1 } from "node:https";
9
+
10
+ //#region src/app_worker.ts
11
+ const debug = debuglog("egg/cluster/app_worker");
12
+ async function main() {
13
+ const options = JSON.parse(process.argv[2]);
14
+ if (options.require) for (const mod of options.require) await importModule(mod, { paths: [options.baseDir] });
15
+ let AppWorker;
16
+ if (options.startMode === "worker_threads") AppWorker = AppThreadWorker;
17
+ else AppWorker = AppProcessWorker;
18
+ const consoleLogger = new EggConsoleLogger({ level: process.env.EGG_APP_WORKER_LOGGER_LEVEL });
19
+ const { Application } = await importModule(options.framework, { paths: [options.baseDir] });
20
+ debug("[app_worker:%s] new Application with options %j", process.pid, options);
21
+ let app;
22
+ try {
23
+ app = new Application(options);
24
+ } catch (err) {
25
+ consoleLogger.error(err);
26
+ throw err;
27
+ }
28
+ app.ready(startServer);
29
+ function exitProcess() {
30
+ AppWorker.kill();
31
+ }
32
+ app.once("startTimeout", startTimeoutHandler);
33
+ function startTimeoutHandler() {
34
+ consoleLogger.error("[app_worker] start timeout, exiting with code:1");
35
+ exitProcess();
36
+ }
37
+ function startServer(err) {
38
+ if (err) {
39
+ consoleLogger.error(err);
40
+ consoleLogger.error("[app_worker] start error, exiting with code:1");
41
+ exitProcess();
42
+ return;
43
+ }
44
+ const clusterConfig = app.config.cluster ?? {};
45
+ const listenConfig = clusterConfig.listen ?? {};
46
+ const httpsOptions = {
47
+ ...clusterConfig.https,
48
+ ...options.https
49
+ };
50
+ const port = app.options.port = options.port || listenConfig.port;
51
+ const debugPort = options.debugPort;
52
+ const protocol = httpsOptions.key && httpsOptions.cert ? "https" : "http";
53
+ debug("[app_worker:%s] listenConfig: %j, real port: %o, protocol: %o, debugPort: %o", process.pid, listenConfig, port, protocol, debugPort);
54
+ AppWorker.send({
55
+ to: "master",
56
+ action: "realport",
57
+ data: {
58
+ port,
59
+ protocol
60
+ }
61
+ });
62
+ app.removeListener("startTimeout", startTimeoutHandler);
63
+ let server;
64
+ let debugPortServer;
65
+ if (protocol === "https") {
66
+ httpsOptions.key = fs.readFileSync(httpsOptions.key);
67
+ httpsOptions.cert = fs.readFileSync(httpsOptions.cert);
68
+ httpsOptions.ca = httpsOptions.ca && fs.readFileSync(httpsOptions.ca);
69
+ server = createServer$1(httpsOptions, app.callback());
70
+ if (debugPort) debugPortServer = createServer(app.callback());
71
+ } else {
72
+ server = createServer(app.callback());
73
+ if (debugPort) debugPortServer = server;
74
+ }
75
+ server.once("error", (err$1) => {
76
+ consoleLogger.error("[app_worker] server got error: %s, code: %s", err$1.message, err$1.code);
77
+ exitProcess();
78
+ });
79
+ app.emit("server", server);
80
+ if (options.sticky && options.stickyWorkerPort) {
81
+ server.listen(options.stickyWorkerPort, "127.0.0.1");
82
+ AppWorker.on("message", (message, connection) => {
83
+ if (message !== "sticky-session:connection") return;
84
+ server.emit("connection", connection);
85
+ connection.resume();
86
+ });
87
+ } else {
88
+ if (listenConfig.path) server.listen(listenConfig.path);
89
+ else {
90
+ if (typeof port !== "number") {
91
+ consoleLogger.error("[app_worker:%s] port should be number, but got %s(%s)", process.pid, port, typeof port);
92
+ exitProcess();
93
+ return;
94
+ }
95
+ const args = [port];
96
+ if (listenConfig.hostname) args.push(listenConfig.hostname);
97
+ debug("listen options %j", args);
98
+ server.listen(...args);
99
+ }
100
+ if (debugPortServer) {
101
+ debug("listen on debug port: %s", debugPort);
102
+ debugPortServer.listen(debugPort);
103
+ }
104
+ }
105
+ server.once("listening", () => {
106
+ let address = server.address() || { port };
107
+ if (typeof address === "string") address = {
108
+ address,
109
+ addressType: -1
110
+ };
111
+ debug("[app_worker:%s] listening at %j", process.pid, address);
112
+ AppWorker.send({
113
+ to: "master",
114
+ action: "app-start",
115
+ data: {
116
+ address,
117
+ workerId: AppWorker.workerId
118
+ }
119
+ });
120
+ });
121
+ }
122
+ AppWorker.gracefulExit({
123
+ logger: consoleLogger,
124
+ label: "app_worker",
125
+ beforeExit: () => app.close()
126
+ });
127
+ }
128
+ main();
129
+
130
+ //#endregion
131
+ export { };
@@ -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 };
@@ -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 };
@@ -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 };