@eggjs/cluster 3.0.1 → 3.1.0-beta.3
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
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
import { BaseAgentWorker } from './mode/base/agent.js';
|
|
3
|
-
import { BaseAppWorker } from './mode/base/app.js';
|
|
4
|
-
export declare class WorkerManager extends EventEmitter {
|
|
5
|
-
agent: BaseAgentWorker | null;
|
|
6
|
-
workers: Map<number, BaseAppWorker<import("worker_threads").Worker | import("cluster").Worker>>;
|
|
7
|
-
exception: number;
|
|
8
|
-
timer: NodeJS.Timeout;
|
|
9
|
-
constructor();
|
|
10
|
-
getWorkers(): number[];
|
|
11
|
-
setAgent(agent: BaseAgentWorker): void;
|
|
12
|
-
getAgent(): BaseAgentWorker<import("child_process").ChildProcess | import("worker_threads").Worker> | null;
|
|
13
|
-
deleteAgent(): void;
|
|
14
|
-
setWorker(worker: BaseAppWorker): void;
|
|
15
|
-
getWorker(workerId: number): BaseAppWorker<import("worker_threads").Worker | import("cluster").Worker> | undefined;
|
|
16
|
-
deleteWorker(workerId: number): void;
|
|
17
|
-
listWorkerIds(): number[];
|
|
18
|
-
listWorkers(): BaseAppWorker<import("worker_threads").Worker | import("cluster").Worker>[];
|
|
19
|
-
getListeningWorkerIds(): number[];
|
|
20
|
-
count(): {
|
|
21
|
-
agent: number;
|
|
22
|
-
worker: number;
|
|
23
|
-
};
|
|
24
|
-
startCheck(): void;
|
|
25
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
// worker manager to record agent and worker forked by egg-cluster
|
|
3
|
-
// can do some check stuff here to monitor the healthy
|
|
4
|
-
export class WorkerManager extends EventEmitter {
|
|
5
|
-
agent;
|
|
6
|
-
workers = new Map();
|
|
7
|
-
exception = 0;
|
|
8
|
-
timer;
|
|
9
|
-
constructor() {
|
|
10
|
-
super();
|
|
11
|
-
this.agent = null;
|
|
12
|
-
}
|
|
13
|
-
getWorkers() {
|
|
14
|
-
return Array.from(this.workers.keys());
|
|
15
|
-
}
|
|
16
|
-
setAgent(agent) {
|
|
17
|
-
this.agent = agent;
|
|
18
|
-
}
|
|
19
|
-
getAgent() {
|
|
20
|
-
return this.agent;
|
|
21
|
-
}
|
|
22
|
-
deleteAgent() {
|
|
23
|
-
this.agent = null;
|
|
24
|
-
}
|
|
25
|
-
setWorker(worker) {
|
|
26
|
-
this.workers.set(worker.workerId, worker);
|
|
27
|
-
}
|
|
28
|
-
getWorker(workerId) {
|
|
29
|
-
return this.workers.get(workerId);
|
|
30
|
-
}
|
|
31
|
-
deleteWorker(workerId) {
|
|
32
|
-
this.workers.delete(workerId);
|
|
33
|
-
}
|
|
34
|
-
listWorkerIds() {
|
|
35
|
-
return Array.from(this.workers.keys());
|
|
36
|
-
}
|
|
37
|
-
listWorkers() {
|
|
38
|
-
return Array.from(this.workers.values());
|
|
39
|
-
}
|
|
40
|
-
getListeningWorkerIds() {
|
|
41
|
-
const keys = [];
|
|
42
|
-
for (const [id, worker] of this.workers.entries()) {
|
|
43
|
-
if (worker.state === 'listening') {
|
|
44
|
-
keys.push(id);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return keys;
|
|
48
|
-
}
|
|
49
|
-
count() {
|
|
50
|
-
return {
|
|
51
|
-
agent: this.agent?.status === 'started' ? 1 : 0,
|
|
52
|
-
worker: this.listWorkerIds().length,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
// check agent and worker must both alive
|
|
56
|
-
// if exception appear 3 times, emit an exception event
|
|
57
|
-
startCheck() {
|
|
58
|
-
this.timer = setInterval(() => {
|
|
59
|
-
const count = this.count();
|
|
60
|
-
if (count.agent > 0 && count.worker > 0) {
|
|
61
|
-
this.exception = 0;
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
this.exception++;
|
|
65
|
-
if (this.exception >= 3) {
|
|
66
|
-
this.emit('exception', count);
|
|
67
|
-
clearInterval(this.timer);
|
|
68
|
-
}
|
|
69
|
-
}, 10000);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ya2VyX21hbmFnZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdXRpbHMvd29ya2VyX21hbmFnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFlBQVksRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUkzQyxrRUFBa0U7QUFDbEUsc0RBQXNEO0FBQ3RELE1BQU0sT0FBTyxhQUFjLFNBQVEsWUFBWTtJQUM3QyxLQUFLLENBQXlCO0lBQzlCLE9BQU8sR0FBRyxJQUFJLEdBQUcsRUFBeUIsQ0FBQztJQUMzQyxTQUFTLEdBQUcsQ0FBQyxDQUFDO0lBQ2QsS0FBSyxDQUFpQjtJQUV0QjtRQUNFLEtBQUssRUFBRSxDQUFDO1FBQ1IsSUFBSSxDQUFDLEtBQUssR0FBRyxJQUFJLENBQUM7SUFDcEIsQ0FBQztJQUVELFVBQVU7UUFDUixPQUFPLEtBQUssQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLEVBQUUsQ0FBQyxDQUFDO0lBQ3pDLENBQUM7SUFFRCxRQUFRLENBQUMsS0FBc0I7UUFDN0IsSUFBSSxDQUFDLEtBQUssR0FBRyxLQUFLLENBQUM7SUFDckIsQ0FBQztJQUVELFFBQVE7UUFDTixPQUFPLElBQUksQ0FBQyxLQUFLLENBQUM7SUFDcEIsQ0FBQztJQUVELFdBQVc7UUFDVCxJQUFJLENBQUMsS0FBSyxHQUFHLElBQUksQ0FBQztJQUNwQixDQUFDO0lBRUQsU0FBUyxDQUFDLE1BQXFCO1FBQzdCLElBQUksQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQyxRQUFRLEVBQUUsTUFBTSxDQUFDLENBQUM7SUFDNUMsQ0FBQztJQUVELFNBQVMsQ0FBQyxRQUFnQjtRQUN4QixPQUFPLElBQUksQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0lBQ3BDLENBQUM7SUFFRCxZQUFZLENBQUMsUUFBZ0I7UUFDM0IsSUFBSSxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsUUFBUSxDQUFDLENBQUM7SUFDaEMsQ0FBQztJQUVELGFBQWE7UUFDWCxPQUFPLEtBQUssQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLEVBQUUsQ0FBQyxDQUFDO0lBQ3pDLENBQUM7SUFFRCxXQUFXO1FBQ1QsT0FBTyxLQUFLLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsTUFBTSxFQUFFLENBQUMsQ0FBQztJQUMzQyxDQUFDO0lBRUQscUJBQXFCO1FBQ25CLE1BQU0sSUFBSSxHQUFHLEVBQUUsQ0FBQztRQUNoQixLQUFLLE1BQU0sQ0FBRSxFQUFFLEVBQUUsTUFBTSxDQUFFLElBQUksSUFBSSxDQUFDLE9BQU8sQ0FBQyxPQUFPLEVBQUUsRUFBRSxDQUFDO1lBQ3BELElBQUksTUFBTSxDQUFDLEtBQUssS0FBSyxXQUFXLEVBQUUsQ0FBQztnQkFDakMsSUFBSSxDQUFDLElBQUksQ0FBQyxFQUFFLENBQUMsQ0FBQztZQUNoQixDQUFDO1FBQ0gsQ0FBQztRQUNELE9BQU8sSUFBSSxDQUFDO0lBQ2QsQ0FBQztJQUVELEtBQUs7UUFDSCxPQUFPO1lBQ0wsS0FBSyxFQUFFLElBQUksQ0FBQyxLQUFLLEVBQUUsTUFBTSxLQUFLLFNBQVMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO1lBQy9DLE1BQU0sRUFBRSxJQUFJLENBQUMsYUFBYSxFQUFFLENBQUMsTUFBTTtTQUNwQyxDQUFDO0lBQ0osQ0FBQztJQUVELHlDQUF5QztJQUN6Qyx1REFBdUQ7SUFDdkQsVUFBVTtRQUNSLElBQUksQ0FBQyxLQUFLLEdBQUcsV0FBVyxDQUFDLEdBQUcsRUFBRTtZQUM1QixNQUFNLEtBQUssR0FBRyxJQUFJLENBQUMsS0FBSyxFQUFFLENBQUM7WUFDM0IsSUFBSSxLQUFLLENBQUMsS0FBSyxHQUFHLENBQUMsSUFBSSxLQUFLLENBQUMsTUFBTSxHQUFHLENBQUMsRUFBRSxDQUFDO2dCQUN4QyxJQUFJLENBQUMsU0FBUyxHQUFHLENBQUMsQ0FBQztnQkFDbkIsT0FBTztZQUNULENBQUM7WUFDRCxJQUFJLENBQUMsU0FBUyxFQUFFLENBQUM7WUFDakIsSUFBSSxJQUFJLENBQUMsU0FBUyxJQUFJLENBQUMsRUFBRSxDQUFDO2dCQUN4QixJQUFJLENBQUMsSUFBSSxDQUFDLFdBQVcsRUFBRSxLQUFLLENBQUMsQ0FBQztnQkFDOUIsYUFBYSxDQUFDLElBQUksQ0FBQyxLQUFLLENBQUMsQ0FBQztZQUM1QixDQUFDO1FBQ0gsQ0FBQyxFQUFFLEtBQUssQ0FBQyxDQUFDO0lBQ1osQ0FBQztDQUNGIn0=
|
package/dist/package.json
DELETED
package/src/agent_worker.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import { EggConsoleLogger as ConsoleLogger } from 'egg-logger';
|
|
3
|
-
import { importModule } from '@eggjs/utils';
|
|
4
|
-
import { BaseAgentWorker } from './utils/mode/base/agent.js';
|
|
5
|
-
import { AgentThreadWorker } from './utils/mode/impl/worker_threads/agent.js';
|
|
6
|
-
import { AgentProcessWorker } from './utils/mode/impl/process/agent.js';
|
|
7
|
-
|
|
8
|
-
const debug = debuglog('@eggjs/cluster/agent_worker');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* agent worker is child_process forked by master.
|
|
12
|
-
*
|
|
13
|
-
* agent worker only exit in two cases:
|
|
14
|
-
* - receive signal SIGTERM, exit code 0 (exit gracefully)
|
|
15
|
-
* - receive disconnect event, exit code 110 (maybe master exit in accident)
|
|
16
|
-
*/
|
|
17
|
-
async function main() {
|
|
18
|
-
// $ node agent_worker.js options
|
|
19
|
-
const options = JSON.parse(process.argv[2]) as {
|
|
20
|
-
framework: string;
|
|
21
|
-
baseDir: string;
|
|
22
|
-
require?: string[];
|
|
23
|
-
startMode?: 'process' | 'worker_threads';
|
|
24
|
-
};
|
|
25
|
-
if (options.require) {
|
|
26
|
-
// inject
|
|
27
|
-
for (const mod of options.require) {
|
|
28
|
-
await importModule(mod, {
|
|
29
|
-
paths: [ options.baseDir ],
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let AgentWorker: typeof BaseAgentWorker;
|
|
35
|
-
if (options.startMode === 'worker_threads') {
|
|
36
|
-
AgentWorker = AgentThreadWorker as any;
|
|
37
|
-
} else {
|
|
38
|
-
AgentWorker = AgentProcessWorker as any;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const consoleLogger = new ConsoleLogger({ level: process.env.EGG_AGENT_WORKER_LOGGER_LEVEL });
|
|
42
|
-
const { Agent } = await importModule(options.framework, {
|
|
43
|
-
paths: [ options.baseDir ],
|
|
44
|
-
});
|
|
45
|
-
debug('new Agent with options %j', options);
|
|
46
|
-
let agent: any;
|
|
47
|
-
try {
|
|
48
|
-
agent = new Agent(options);
|
|
49
|
-
} catch (err) {
|
|
50
|
-
consoleLogger.error(err);
|
|
51
|
-
throw err;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function startErrorHandler(err: Error) {
|
|
55
|
-
consoleLogger.error(err);
|
|
56
|
-
consoleLogger.error('[agent_worker] start error, exiting with code:1');
|
|
57
|
-
AgentWorker.kill();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
agent.ready((err?: Error) => {
|
|
61
|
-
// don't send started message to master when start error
|
|
62
|
-
if (err) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
agent.removeListener('error', startErrorHandler);
|
|
67
|
-
AgentWorker.send({ action: 'agent-start', to: 'master' });
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// exit if agent start error
|
|
71
|
-
agent.once('error', startErrorHandler);
|
|
72
|
-
|
|
73
|
-
AgentWorker.gracefulExit({
|
|
74
|
-
logger: consoleLogger,
|
|
75
|
-
label: 'agent_worker',
|
|
76
|
-
beforeExit: () => agent.close(),
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
main();
|
package/src/app_worker.ts
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import { createServer as createHttpServer, type Server } from 'node:http';
|
|
3
|
-
import { createServer as createHttpsServer } from 'node:https';
|
|
4
|
-
import type { Socket } from 'node:net';
|
|
5
|
-
import { debuglog } from 'node:util';
|
|
6
|
-
import { EggConsoleLogger as ConsoleLogger } from 'egg-logger';
|
|
7
|
-
import { importModule } from '@eggjs/utils';
|
|
8
|
-
import { BaseAppWorker } from './utils/mode/base/app.js';
|
|
9
|
-
import { AppThreadWorker } from './utils/mode/impl/worker_threads/app.js';
|
|
10
|
-
import { AppProcessWorker } from './utils/mode/impl/process/app.js';
|
|
11
|
-
|
|
12
|
-
const debug = debuglog('@eggjs/cluster/app_worker');
|
|
13
|
-
|
|
14
|
-
async function main() {
|
|
15
|
-
// $ node app_worker.js options-json-string
|
|
16
|
-
const options = JSON.parse(process.argv[2]) as {
|
|
17
|
-
framework: string;
|
|
18
|
-
baseDir: string;
|
|
19
|
-
require?: string[];
|
|
20
|
-
startMode?: 'process' | 'worker_threads';
|
|
21
|
-
port: number;
|
|
22
|
-
debugPort?: number;
|
|
23
|
-
https?: object;
|
|
24
|
-
sticky?: boolean;
|
|
25
|
-
stickyWorkerPort?: number;
|
|
26
|
-
};
|
|
27
|
-
if (options.require) {
|
|
28
|
-
// inject
|
|
29
|
-
for (const mod of options.require) {
|
|
30
|
-
await importModule(mod, {
|
|
31
|
-
paths: [ options.baseDir ],
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let AppWorker: typeof BaseAppWorker;
|
|
37
|
-
if (options.startMode === 'worker_threads') {
|
|
38
|
-
AppWorker = AppThreadWorker as any;
|
|
39
|
-
} else {
|
|
40
|
-
AppWorker = AppProcessWorker as any;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const consoleLogger = new ConsoleLogger({
|
|
44
|
-
level: process.env.EGG_APP_WORKER_LOGGER_LEVEL,
|
|
45
|
-
});
|
|
46
|
-
const { Application } = await importModule(options.framework, {
|
|
47
|
-
paths: [ options.baseDir ],
|
|
48
|
-
});
|
|
49
|
-
debug('[app_worker:%s] new Application with options %j', process.pid, options);
|
|
50
|
-
let app: any;
|
|
51
|
-
try {
|
|
52
|
-
app = new Application(options);
|
|
53
|
-
} catch (err) {
|
|
54
|
-
consoleLogger.error(err);
|
|
55
|
-
throw err;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
app.ready(startServer);
|
|
59
|
-
|
|
60
|
-
function exitProcess() {
|
|
61
|
-
// Use SIGTERM kill process, ensure trigger the gracefulExit
|
|
62
|
-
AppWorker.kill();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// exit if worker start timeout
|
|
66
|
-
app.once('startTimeout', startTimeoutHandler);
|
|
67
|
-
|
|
68
|
-
function startTimeoutHandler() {
|
|
69
|
-
consoleLogger.error('[app_worker] start timeout, exiting with code:1');
|
|
70
|
-
exitProcess();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function startServer(err?: Error) {
|
|
74
|
-
if (err) {
|
|
75
|
-
consoleLogger.error(err);
|
|
76
|
-
consoleLogger.error('[app_worker] start error, exiting with code:1');
|
|
77
|
-
exitProcess();
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const clusterConfig = app.config.cluster ?? {};
|
|
82
|
-
const listenConfig = clusterConfig.listen ?? {};
|
|
83
|
-
const httpsOptions = {
|
|
84
|
-
...clusterConfig.https,
|
|
85
|
-
...options.https,
|
|
86
|
-
};
|
|
87
|
-
const port = app.options.port = options.port || listenConfig.port;
|
|
88
|
-
const debugPort = options.debugPort;
|
|
89
|
-
const protocol = (httpsOptions.key && httpsOptions.cert) ? 'https' : 'http';
|
|
90
|
-
debug('[app_worker:%s] listenConfig: %j, real port: %o, protocol: %o, debugPort: %o',
|
|
91
|
-
process.pid, listenConfig, port, protocol, debugPort);
|
|
92
|
-
|
|
93
|
-
AppWorker.send({
|
|
94
|
-
to: 'master',
|
|
95
|
-
action: 'realport',
|
|
96
|
-
data: {
|
|
97
|
-
port,
|
|
98
|
-
protocol,
|
|
99
|
-
},
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
app.removeListener('startTimeout', startTimeoutHandler);
|
|
103
|
-
|
|
104
|
-
let server: Server;
|
|
105
|
-
let debugPortServer: Server | undefined;
|
|
106
|
-
|
|
107
|
-
// https config
|
|
108
|
-
if (protocol === 'https') {
|
|
109
|
-
httpsOptions.key = fs.readFileSync(httpsOptions.key);
|
|
110
|
-
httpsOptions.cert = fs.readFileSync(httpsOptions.cert);
|
|
111
|
-
httpsOptions.ca = httpsOptions.ca && fs.readFileSync(httpsOptions.ca);
|
|
112
|
-
server = createHttpsServer(httpsOptions, app.callback());
|
|
113
|
-
if (debugPort) {
|
|
114
|
-
debugPortServer = createHttpServer(app.callback());
|
|
115
|
-
}
|
|
116
|
-
} else {
|
|
117
|
-
server = createHttpServer(app.callback());
|
|
118
|
-
if (debugPort) {
|
|
119
|
-
debugPortServer = server;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
server.once('error', (err: any) => {
|
|
124
|
-
consoleLogger.error('[app_worker] server got error: %s, code: %s', err.message, err.code);
|
|
125
|
-
exitProcess();
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// emit `server` event in app
|
|
129
|
-
app.emit('server', server);
|
|
130
|
-
|
|
131
|
-
if (options.sticky && options.stickyWorkerPort) {
|
|
132
|
-
// only allow connection from localhost
|
|
133
|
-
server.listen(options.stickyWorkerPort, '127.0.0.1');
|
|
134
|
-
// Listen to messages was sent from the master. Ignore everything else.
|
|
135
|
-
AppWorker.on('message', (message: string, connection: Socket) => {
|
|
136
|
-
if (message !== 'sticky-session:connection') {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
// Emulate a connection event on the server by emitting the
|
|
140
|
-
// event with the connection the master sent us.
|
|
141
|
-
server.emit('connection', connection);
|
|
142
|
-
connection.resume();
|
|
143
|
-
});
|
|
144
|
-
} else {
|
|
145
|
-
if (listenConfig.path) {
|
|
146
|
-
server.listen(listenConfig.path);
|
|
147
|
-
} else {
|
|
148
|
-
if (typeof port !== 'number') {
|
|
149
|
-
consoleLogger.error('[app_worker:%s] port should be number, but got %s(%s)',
|
|
150
|
-
process.pid, port, typeof port);
|
|
151
|
-
exitProcess();
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const args = [ port ];
|
|
155
|
-
if (listenConfig.hostname) {
|
|
156
|
-
args.push(listenConfig.hostname);
|
|
157
|
-
}
|
|
158
|
-
debug('listen options %j', args);
|
|
159
|
-
server.listen(...args);
|
|
160
|
-
}
|
|
161
|
-
if (debugPortServer) {
|
|
162
|
-
debug('listen on debug port: %s', debugPort);
|
|
163
|
-
debugPortServer.listen(debugPort);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
server.once('listening', () => {
|
|
168
|
-
let address: any = server.address() || { port };
|
|
169
|
-
if (typeof address === 'string') {
|
|
170
|
-
// https://nodejs.org/api/cluster.html#cluster_event_listening_1
|
|
171
|
-
// Unix domain socket
|
|
172
|
-
address = {
|
|
173
|
-
address,
|
|
174
|
-
addressType: -1,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
debug('[app_worker:%s] listening at %j', process.pid, address);
|
|
178
|
-
AppWorker.send({
|
|
179
|
-
to: 'master',
|
|
180
|
-
action: 'app-start',
|
|
181
|
-
data: {
|
|
182
|
-
address,
|
|
183
|
-
workerId: AppWorker.workerId,
|
|
184
|
-
},
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
AppWorker.gracefulExit({
|
|
190
|
-
logger: consoleLogger,
|
|
191
|
-
label: 'app_worker',
|
|
192
|
-
beforeExit: () => app.close(),
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
main();
|
package/src/dirname.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath } from 'node:url';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
export function getSrcDirname() {
|
|
5
|
-
if (typeof __dirname !== 'undefined') {
|
|
6
|
-
return __dirname;
|
|
7
|
-
}
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
9
|
-
// @ts-ignore
|
|
10
|
-
return path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export class ClusterAgentWorkerError extends Error {
|
|
2
|
-
id: number;
|
|
3
|
-
/**
|
|
4
|
-
* pid in process mode
|
|
5
|
-
* tid in worker_threads mode
|
|
6
|
-
*/
|
|
7
|
-
workerId: number;
|
|
8
|
-
status: string;
|
|
9
|
-
|
|
10
|
-
constructor(id: number, workerId: number, status: string, error: 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
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export class ClusterWorkerExceptionError extends Error {
|
|
2
|
-
count: {
|
|
3
|
-
agent: number;
|
|
4
|
-
worker: number;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
constructor(agent: number, worker: number) {
|
|
8
|
-
const message = `[master] ${agent} agent and ${worker} worker(s) alive, exit to avoid unknown state`;
|
|
9
|
-
super(message);
|
|
10
|
-
this.name = this.constructor.name;
|
|
11
|
-
this.count = {
|
|
12
|
-
agent,
|
|
13
|
-
worker,
|
|
14
|
-
};
|
|
15
|
-
Error.captureStackTrace(this, this.constructor);
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/error/index.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Master, MasterOptions } from './master.js';
|
|
2
|
-
import { ClusterOptions, ClusterHTTPSSecureOptions, ClusterStartMode } from './utils/options.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* cluster start flow:
|
|
6
|
-
*
|
|
7
|
-
* [startCluster] -> master -> agent_worker -> new [Agent] -> agentWorkerLoader
|
|
8
|
-
* `-> app_worker -> new [Application] -> appWorkerLoader
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* start egg app
|
|
14
|
-
* @function Egg#startCluster
|
|
15
|
-
* @param {Object} options {@link Master}
|
|
16
|
-
*/
|
|
17
|
-
export async function startCluster(options: ClusterOptions) {
|
|
18
|
-
await new Master(options).ready();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
Master, MasterOptions,
|
|
23
|
-
ClusterOptions, ClusterHTTPSSecureOptions, ClusterStartMode,
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export * from './error/index.js';
|