@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.
Files changed (123) hide show
  1. package/README.md +17 -21
  2. package/dist/dirname.js +11 -0
  3. package/dist/error/ClusterAgentWorkerError.d.ts +13 -0
  4. package/dist/error/ClusterAgentWorkerError.js +22 -0
  5. package/dist/error/ClusterWorkerExceptionError.d.ts +10 -0
  6. package/dist/error/ClusterWorkerExceptionError.js +17 -0
  7. package/dist/index.d.ts +22 -0
  8. package/dist/index.js +24 -0
  9. package/dist/master.d.ts +96 -0
  10. package/dist/master.js +426 -0
  11. package/dist/utils/messenger.d.ts +96 -0
  12. package/dist/utils/messenger.js +144 -0
  13. package/dist/utils/mode/base/agent.d.ts +45 -0
  14. package/dist/utils/mode/base/agent.js +63 -0
  15. package/dist/utils/mode/base/app.d.ts +56 -0
  16. package/dist/utils/mode/base/app.js +77 -0
  17. package/dist/utils/mode/impl/process/agent.d.ts +22 -0
  18. package/dist/utils/mode/impl/process/agent.js +93 -0
  19. package/dist/utils/mode/impl/process/app.d.ts +12 -0
  20. package/dist/utils/mode/impl/process/app.js +117 -0
  21. package/dist/utils/mode/impl/worker_threads/agent.d.ts +22 -0
  22. package/dist/utils/mode/impl/worker_threads/agent.js +79 -0
  23. package/dist/utils/mode/impl/worker_threads/app.d.ts +13 -0
  24. package/dist/utils/mode/impl/worker_threads/app.js +128 -0
  25. package/dist/utils/options.d.ts +83 -0
  26. package/dist/utils/options.js +56 -0
  27. package/dist/utils/terminate.js +62 -0
  28. package/dist/utils/worker_manager.d.ts +32 -0
  29. package/dist/utils/worker_manager.js +68 -0
  30. package/package.json +35 -61
  31. package/dist/commonjs/agent_worker.d.ts +0 -1
  32. package/dist/commonjs/agent_worker.js +0 -69
  33. package/dist/commonjs/app_worker.d.ts +0 -1
  34. package/dist/commonjs/app_worker.js +0 -173
  35. package/dist/commonjs/dirname.d.ts +0 -1
  36. package/dist/commonjs/dirname.js +0 -17
  37. package/dist/commonjs/error/ClusterAgentWorkerError.d.ts +0 -10
  38. package/dist/commonjs/error/ClusterAgentWorkerError.js +0 -23
  39. package/dist/commonjs/error/ClusterWorkerExceptionError.d.ts +0 -7
  40. package/dist/commonjs/error/ClusterWorkerExceptionError.js +0 -18
  41. package/dist/commonjs/error/index.d.ts +0 -2
  42. package/dist/commonjs/error/index.js +0 -19
  43. package/dist/commonjs/index.d.ts +0 -17
  44. package/dist/commonjs/index.js +0 -37
  45. package/dist/commonjs/master.d.ts +0 -90
  46. package/dist/commonjs/master.js +0 -560
  47. package/dist/commonjs/package.json +0 -3
  48. package/dist/commonjs/utils/messenger.d.ts +0 -92
  49. package/dist/commonjs/utils/messenger.js +0 -186
  50. package/dist/commonjs/utils/mode/base/agent.d.ts +0 -38
  51. package/dist/commonjs/utils/mode/base/agent.js +0 -68
  52. package/dist/commonjs/utils/mode/base/app.d.ts +0 -48
  53. package/dist/commonjs/utils/mode/base/app.js +0 -83
  54. package/dist/commonjs/utils/mode/impl/process/agent.d.ts +0 -18
  55. package/dist/commonjs/utils/mode/impl/process/agent.js +0 -108
  56. package/dist/commonjs/utils/mode/impl/process/app.d.ts +0 -21
  57. package/dist/commonjs/utils/mode/impl/process/app.js +0 -127
  58. package/dist/commonjs/utils/mode/impl/worker_threads/agent.d.ts +0 -18
  59. package/dist/commonjs/utils/mode/impl/worker_threads/agent.js +0 -91
  60. package/dist/commonjs/utils/mode/impl/worker_threads/app.d.ts +0 -26
  61. package/dist/commonjs/utils/mode/impl/worker_threads/app.js +0 -142
  62. package/dist/commonjs/utils/options.d.ts +0 -80
  63. package/dist/commonjs/utils/options.js +0 -83
  64. package/dist/commonjs/utils/terminate.d.ts +0 -6
  65. package/dist/commonjs/utils/terminate.js +0 -89
  66. package/dist/commonjs/utils/worker_manager.d.ts +0 -25
  67. package/dist/commonjs/utils/worker_manager.js +0 -76
  68. package/dist/esm/agent_worker.d.ts +0 -1
  69. package/dist/esm/agent_worker.js +0 -67
  70. package/dist/esm/app_worker.d.ts +0 -1
  71. package/dist/esm/app_worker.js +0 -168
  72. package/dist/esm/dirname.d.ts +0 -1
  73. package/dist/esm/dirname.js +0 -11
  74. package/dist/esm/error/ClusterAgentWorkerError.d.ts +0 -10
  75. package/dist/esm/error/ClusterAgentWorkerError.js +0 -19
  76. package/dist/esm/error/ClusterWorkerExceptionError.d.ts +0 -7
  77. package/dist/esm/error/ClusterWorkerExceptionError.js +0 -14
  78. package/dist/esm/error/index.d.ts +0 -2
  79. package/dist/esm/error/index.js +0 -3
  80. package/dist/esm/index.d.ts +0 -17
  81. package/dist/esm/index.js +0 -19
  82. package/dist/esm/master.d.ts +0 -90
  83. package/dist/esm/master.js +0 -553
  84. package/dist/esm/package.json +0 -3
  85. package/dist/esm/utils/messenger.d.ts +0 -92
  86. package/dist/esm/utils/messenger.js +0 -179
  87. package/dist/esm/utils/mode/base/agent.d.ts +0 -38
  88. package/dist/esm/utils/mode/base/agent.js +0 -60
  89. package/dist/esm/utils/mode/base/app.d.ts +0 -48
  90. package/dist/esm/utils/mode/base/app.js +0 -75
  91. package/dist/esm/utils/mode/impl/process/agent.d.ts +0 -18
  92. package/dist/esm/utils/mode/impl/process/agent.js +0 -103
  93. package/dist/esm/utils/mode/impl/process/app.d.ts +0 -21
  94. package/dist/esm/utils/mode/impl/process/app.js +0 -119
  95. package/dist/esm/utils/mode/impl/worker_threads/agent.d.ts +0 -18
  96. package/dist/esm/utils/mode/impl/worker_threads/agent.js +0 -83
  97. package/dist/esm/utils/mode/impl/worker_threads/app.d.ts +0 -26
  98. package/dist/esm/utils/mode/impl/worker_threads/app.js +0 -137
  99. package/dist/esm/utils/options.d.ts +0 -80
  100. package/dist/esm/utils/options.js +0 -77
  101. package/dist/esm/utils/terminate.d.ts +0 -6
  102. package/dist/esm/utils/terminate.js +0 -86
  103. package/dist/esm/utils/worker_manager.d.ts +0 -25
  104. package/dist/esm/utils/worker_manager.js +0 -72
  105. package/dist/package.json +0 -4
  106. package/src/agent_worker.ts +0 -80
  107. package/src/app_worker.ts +0 -196
  108. package/src/dirname.ts +0 -11
  109. package/src/error/ClusterAgentWorkerError.ts +0 -19
  110. package/src/error/ClusterWorkerExceptionError.ts +0 -17
  111. package/src/error/index.ts +0 -2
  112. package/src/index.ts +0 -26
  113. package/src/master.ts +0 -658
  114. package/src/utils/messenger.ts +0 -207
  115. package/src/utils/mode/base/agent.ts +0 -90
  116. package/src/utils/mode/base/app.ts +0 -119
  117. package/src/utils/mode/impl/process/agent.ts +0 -119
  118. package/src/utils/mode/impl/process/app.ts +0 -140
  119. package/src/utils/mode/impl/worker_threads/agent.ts +0 -99
  120. package/src/utils/mode/impl/worker_threads/app.ts +0 -164
  121. package/src/utils/options.ts +0 -171
  122. package/src/utils/terminate.ts +0 -97
  123. 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,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 };