@eggjs/cluster 4.0.0-beta.18 → 4.0.0-beta.20
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/dist/agent-griHEaCW.js +246 -0
- package/dist/agent_worker.js +2 -2
- package/dist/app-5Was1vub.js +315 -0
- package/dist/app_worker.js +2 -2
- package/dist/index.d.ts +440 -5
- package/dist/index.js +692 -4
- package/dist/{utils/terminate.js → terminate-w3g0oQgq.js} +10 -1
- package/package.json +7 -7
- package/dist/dirname.js +0 -11
- package/dist/error/ClusterAgentWorkerError.d.ts +0 -13
- package/dist/error/ClusterAgentWorkerError.js +0 -22
- package/dist/error/ClusterWorkerExceptionError.d.ts +0 -10
- package/dist/error/ClusterWorkerExceptionError.js +0 -17
- package/dist/master.d.ts +0 -96
- package/dist/master.js +0 -426
- package/dist/utils/messenger.d.ts +0 -96
- package/dist/utils/messenger.js +0 -144
- package/dist/utils/mode/base/agent.d.ts +0 -45
- package/dist/utils/mode/base/agent.js +0 -63
- package/dist/utils/mode/base/app.d.ts +0 -56
- package/dist/utils/mode/base/app.js +0 -77
- package/dist/utils/mode/impl/process/agent.d.ts +0 -22
- package/dist/utils/mode/impl/process/agent.js +0 -93
- package/dist/utils/mode/impl/process/app.d.ts +0 -12
- package/dist/utils/mode/impl/process/app.js +0 -117
- package/dist/utils/mode/impl/worker_threads/agent.d.ts +0 -22
- package/dist/utils/mode/impl/worker_threads/agent.js +0 -79
- package/dist/utils/mode/impl/worker_threads/app.d.ts +0 -13
- package/dist/utils/mode/impl/worker_threads/app.js +0 -128
- package/dist/utils/options.d.ts +0 -83
- package/dist/utils/options.js +0 -56
- package/dist/utils/worker_manager.d.ts +0 -32
- package/dist/utils/worker_manager.js +0 -68
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { getSrcDirname, terminate } from "./terminate-w3g0oQgq.js";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { EventEmitter } from "node:events";
|
|
5
|
+
import workerThreads from "node:worker_threads";
|
|
6
|
+
import { fork } from "node:child_process";
|
|
7
|
+
import { sendmessage } from "sendmessage";
|
|
8
|
+
import { graceful } from "graceful-process";
|
|
9
|
+
|
|
10
|
+
//#region src/utils/mode/base/agent.ts
|
|
11
|
+
var BaseAgentWorker = class {
|
|
12
|
+
instance;
|
|
13
|
+
#instanceId;
|
|
14
|
+
#instanceStatus;
|
|
15
|
+
constructor(instance) {
|
|
16
|
+
this.instance = instance;
|
|
17
|
+
}
|
|
18
|
+
get id() {
|
|
19
|
+
return this.#instanceId;
|
|
20
|
+
}
|
|
21
|
+
set id(id) {
|
|
22
|
+
this.#instanceId = id;
|
|
23
|
+
}
|
|
24
|
+
get status() {
|
|
25
|
+
return this.#instanceStatus;
|
|
26
|
+
}
|
|
27
|
+
set status(status) {
|
|
28
|
+
this.#instanceStatus = status;
|
|
29
|
+
}
|
|
30
|
+
static send(_message) {
|
|
31
|
+
throw new Error("BaseAgentWorker should implement send.");
|
|
32
|
+
}
|
|
33
|
+
static kill() {
|
|
34
|
+
throw new Error("BaseAgentWorker should implement kill.");
|
|
35
|
+
}
|
|
36
|
+
static gracefulExit(_options) {
|
|
37
|
+
throw new Error("BaseAgentWorker should implement gracefulExit.");
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var BaseAgentUtils = class extends EventEmitter {
|
|
41
|
+
options;
|
|
42
|
+
messenger;
|
|
43
|
+
log;
|
|
44
|
+
logger;
|
|
45
|
+
startTime = 0;
|
|
46
|
+
constructor(options, { log, logger, messenger }) {
|
|
47
|
+
super();
|
|
48
|
+
this.options = options;
|
|
49
|
+
this.log = log;
|
|
50
|
+
this.logger = logger;
|
|
51
|
+
this.messenger = messenger;
|
|
52
|
+
}
|
|
53
|
+
getAgentWorkerFile() {
|
|
54
|
+
let agentWorkerFile = path.join(getSrcDirname(), "agent_worker.js");
|
|
55
|
+
if (!existsSync(agentWorkerFile)) agentWorkerFile = path.join(getSrcDirname(), "agent_worker.ts");
|
|
56
|
+
return agentWorkerFile;
|
|
57
|
+
}
|
|
58
|
+
fork() {
|
|
59
|
+
throw new Error("BaseAgent should implement fork.");
|
|
60
|
+
}
|
|
61
|
+
clean() {
|
|
62
|
+
throw new Error("BaseAgent should implement clean.");
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/error/ClusterAgentWorkerError.ts
|
|
68
|
+
var ClusterAgentWorkerError = class extends Error {
|
|
69
|
+
id;
|
|
70
|
+
/**
|
|
71
|
+
* pid in process mode
|
|
72
|
+
* tid in worker_threads mode
|
|
73
|
+
*/
|
|
74
|
+
workerId;
|
|
75
|
+
status;
|
|
76
|
+
constructor(id, workerId, status, error) {
|
|
77
|
+
const message = `Got agent worker error: ${error.message}`;
|
|
78
|
+
super(message, { cause: error });
|
|
79
|
+
this.name = this.constructor.name;
|
|
80
|
+
this.id = id;
|
|
81
|
+
this.workerId = workerId;
|
|
82
|
+
this.status = status;
|
|
83
|
+
Error.captureStackTrace(this, this.constructor);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/utils/mode/impl/process/agent.ts
|
|
89
|
+
var AgentProcessWorker = class extends BaseAgentWorker {
|
|
90
|
+
get workerId() {
|
|
91
|
+
return this.instance.pid;
|
|
92
|
+
}
|
|
93
|
+
send(message) {
|
|
94
|
+
sendmessage(this.instance, message);
|
|
95
|
+
}
|
|
96
|
+
static send(message) {
|
|
97
|
+
message.senderWorkerId = String(process.pid);
|
|
98
|
+
process.send(message);
|
|
99
|
+
}
|
|
100
|
+
static kill() {
|
|
101
|
+
process.exitCode = 1;
|
|
102
|
+
process.kill(process.pid);
|
|
103
|
+
}
|
|
104
|
+
static gracefulExit(options) {
|
|
105
|
+
graceful(options);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var AgentProcessUtils = class extends BaseAgentUtils {
|
|
109
|
+
#agentProcess;
|
|
110
|
+
#id = 0;
|
|
111
|
+
instance;
|
|
112
|
+
fork() {
|
|
113
|
+
this.startTime = Date.now();
|
|
114
|
+
const args = [JSON.stringify(this.options)];
|
|
115
|
+
const forkOptions = {};
|
|
116
|
+
if (process.platform === "win32") forkOptions.windowsHide = true;
|
|
117
|
+
const debugPort = process.env.EGG_AGENT_DEBUG_PORT ?? 5800;
|
|
118
|
+
if (this.options.isDebug) forkOptions.execArgv = process.execArgv.concat([`--inspect-port=${debugPort}`]);
|
|
119
|
+
const agentProcess = this.#agentProcess = fork(this.getAgentWorkerFile(), args, forkOptions);
|
|
120
|
+
const agentWorker = this.instance = new AgentProcessWorker(agentProcess);
|
|
121
|
+
agentWorker.status = "starting";
|
|
122
|
+
agentWorker.id = ++this.#id;
|
|
123
|
+
this.emit("agent_forked", agentWorker);
|
|
124
|
+
this.log("[master] agent_worker#%s:%s start with clusterPort:%s", agentWorker.id, agentWorker.workerId, this.options.clusterPort);
|
|
125
|
+
if (this.options.isDebug) this.messenger.send({
|
|
126
|
+
to: "parent",
|
|
127
|
+
from: "agent",
|
|
128
|
+
action: "debug",
|
|
129
|
+
data: {
|
|
130
|
+
debugPort,
|
|
131
|
+
pid: agentWorker.workerId,
|
|
132
|
+
workerId: agentWorker.workerId
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
agentProcess.on("message", (msg) => {
|
|
136
|
+
if (typeof msg === "string") msg = {
|
|
137
|
+
action: msg,
|
|
138
|
+
data: msg
|
|
139
|
+
};
|
|
140
|
+
msg.from = "agent";
|
|
141
|
+
this.messenger.send(msg);
|
|
142
|
+
});
|
|
143
|
+
agentProcess.on("error", (err) => {
|
|
144
|
+
err.name = "AgentWorkerError";
|
|
145
|
+
this.logger.error(new ClusterAgentWorkerError(agentWorker.id, agentWorker.workerId, agentWorker.status, err));
|
|
146
|
+
});
|
|
147
|
+
agentProcess.once("exit", (code, signal) => {
|
|
148
|
+
this.messenger.send({
|
|
149
|
+
action: "agent-exit",
|
|
150
|
+
data: {
|
|
151
|
+
code,
|
|
152
|
+
signal
|
|
153
|
+
},
|
|
154
|
+
to: "master",
|
|
155
|
+
from: "agent"
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
clean() {
|
|
161
|
+
this.#agentProcess.removeAllListeners();
|
|
162
|
+
}
|
|
163
|
+
async kill(timeout) {
|
|
164
|
+
if (this.#agentProcess) {
|
|
165
|
+
this.log("[master] kill agent worker with signal SIGTERM");
|
|
166
|
+
this.clean();
|
|
167
|
+
await terminate(this.#agentProcess, timeout);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/utils/mode/impl/worker_threads/agent.ts
|
|
174
|
+
var AgentThreadWorker = class extends BaseAgentWorker {
|
|
175
|
+
get workerId() {
|
|
176
|
+
return this.instance.threadId;
|
|
177
|
+
}
|
|
178
|
+
send(message) {
|
|
179
|
+
this.instance.postMessage(message);
|
|
180
|
+
}
|
|
181
|
+
static send(message) {
|
|
182
|
+
message.senderWorkerId = String(workerThreads.threadId);
|
|
183
|
+
workerThreads.parentPort.postMessage(message);
|
|
184
|
+
}
|
|
185
|
+
static kill() {
|
|
186
|
+
process.exit(1);
|
|
187
|
+
}
|
|
188
|
+
static gracefulExit(options) {
|
|
189
|
+
const { beforeExit } = options;
|
|
190
|
+
process.on("exit", async (code) => {
|
|
191
|
+
if (typeof beforeExit === "function") await beforeExit();
|
|
192
|
+
process.exit(code);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
var AgentThreadUtils = class extends BaseAgentUtils {
|
|
197
|
+
#worker;
|
|
198
|
+
#id = 0;
|
|
199
|
+
instance;
|
|
200
|
+
fork() {
|
|
201
|
+
this.startTime = Date.now();
|
|
202
|
+
const argv = [JSON.stringify(this.options)];
|
|
203
|
+
const agentPath = this.getAgentWorkerFile();
|
|
204
|
+
const worker = this.#worker = new workerThreads.Worker(agentPath, { argv });
|
|
205
|
+
const agentWorker = this.instance = new AgentThreadWorker(worker);
|
|
206
|
+
this.emit("agent_forked", agentWorker);
|
|
207
|
+
agentWorker.status = "starting";
|
|
208
|
+
agentWorker.id = ++this.#id;
|
|
209
|
+
this.log("[master] agent_worker#%s:%s start with worker_threads", agentWorker.id, agentWorker.workerId);
|
|
210
|
+
worker.on("message", (msg) => {
|
|
211
|
+
if (typeof msg === "string") msg = {
|
|
212
|
+
action: msg,
|
|
213
|
+
data: msg
|
|
214
|
+
};
|
|
215
|
+
msg.from = "agent";
|
|
216
|
+
this.messenger.send(msg);
|
|
217
|
+
});
|
|
218
|
+
worker.on("error", (err) => {
|
|
219
|
+
this.logger.error(new ClusterAgentWorkerError(agentWorker.id, agentWorker.workerId, agentWorker.status, err));
|
|
220
|
+
});
|
|
221
|
+
worker.once("exit", (code, signal) => {
|
|
222
|
+
this.messenger.send({
|
|
223
|
+
action: "agent-exit",
|
|
224
|
+
data: {
|
|
225
|
+
code,
|
|
226
|
+
signal
|
|
227
|
+
},
|
|
228
|
+
to: "master",
|
|
229
|
+
from: "agent"
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
clean() {
|
|
234
|
+
this.#worker.removeAllListeners();
|
|
235
|
+
}
|
|
236
|
+
async kill() {
|
|
237
|
+
if (this.#worker) {
|
|
238
|
+
this.log(`[master] kill agent worker#${this.#id} (worker_threads) by worker.terminate()`);
|
|
239
|
+
this.clean();
|
|
240
|
+
await this.#worker.terminate();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
//#endregion
|
|
246
|
+
export { AgentProcessUtils, AgentProcessWorker, AgentThreadUtils, AgentThreadWorker, ClusterAgentWorkerError };
|
package/dist/agent_worker.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { AgentThreadWorker } from "./
|
|
1
|
+
import "./terminate-w3g0oQgq.js";
|
|
2
|
+
import { AgentProcessWorker, AgentThreadWorker } from "./agent-griHEaCW.js";
|
|
3
3
|
import { debuglog } from "node:util";
|
|
4
4
|
import { EggConsoleLogger } from "egg-logger";
|
|
5
5
|
import { importModule } from "@eggjs/utils";
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { getSrcDirname, terminate } from "./terminate-w3g0oQgq.js";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { EventEmitter } from "node:events";
|
|
5
|
+
import { Worker, parentPort, threadId } from "node:worker_threads";
|
|
6
|
+
import { sendmessage } from "sendmessage";
|
|
7
|
+
import { graceful } from "graceful-process";
|
|
8
|
+
import { setTimeout } from "node:timers/promises";
|
|
9
|
+
import cluster from "node:cluster";
|
|
10
|
+
import { cfork } from "cfork";
|
|
11
|
+
|
|
12
|
+
//#region src/utils/mode/base/app.ts
|
|
13
|
+
var BaseAppWorker = class {
|
|
14
|
+
instance;
|
|
15
|
+
constructor(instance) {
|
|
16
|
+
this.instance = instance;
|
|
17
|
+
}
|
|
18
|
+
get state() {
|
|
19
|
+
return Reflect.get(this.instance, "state");
|
|
20
|
+
}
|
|
21
|
+
set state(state) {
|
|
22
|
+
Reflect.set(this.instance, "state", state);
|
|
23
|
+
}
|
|
24
|
+
get disableRefork() {
|
|
25
|
+
return Reflect.get(this.instance, "disableRefork");
|
|
26
|
+
}
|
|
27
|
+
set disableRefork(disableRefork) {
|
|
28
|
+
Reflect.set(this.instance, "disableRefork", disableRefork);
|
|
29
|
+
}
|
|
30
|
+
get isDevReload() {
|
|
31
|
+
return Reflect.get(this.instance, "isDevReload");
|
|
32
|
+
}
|
|
33
|
+
set isDevReload(isDevReload) {
|
|
34
|
+
Reflect.set(this.instance, "isDevReload", isDevReload);
|
|
35
|
+
}
|
|
36
|
+
clean() {
|
|
37
|
+
throw new Error("BaseAppWorker should implement clean.");
|
|
38
|
+
}
|
|
39
|
+
static get workerId() {
|
|
40
|
+
throw new Error("BaseAppWorker should implement workerId.");
|
|
41
|
+
}
|
|
42
|
+
static on(..._args) {
|
|
43
|
+
throw new Error("BaseAppWorker should implement on.");
|
|
44
|
+
}
|
|
45
|
+
static send(_message) {
|
|
46
|
+
throw new Error("BaseAgentWorker should implement send.");
|
|
47
|
+
}
|
|
48
|
+
static kill() {
|
|
49
|
+
throw new Error("BaseAppWorker should implement kill.");
|
|
50
|
+
}
|
|
51
|
+
static gracefulExit(_options) {
|
|
52
|
+
throw new Error("BaseAgentWorker should implement gracefulExit.");
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var BaseAppUtils = class extends EventEmitter {
|
|
56
|
+
options;
|
|
57
|
+
messenger;
|
|
58
|
+
log;
|
|
59
|
+
logger;
|
|
60
|
+
isProduction;
|
|
61
|
+
startTime = 0;
|
|
62
|
+
startSuccessCount = 0;
|
|
63
|
+
isAllWorkerStarted = false;
|
|
64
|
+
constructor(options, { log, logger, messenger, isProduction }) {
|
|
65
|
+
super();
|
|
66
|
+
this.options = options;
|
|
67
|
+
this.log = log;
|
|
68
|
+
this.logger = logger;
|
|
69
|
+
this.messenger = messenger;
|
|
70
|
+
this.isProduction = isProduction;
|
|
71
|
+
}
|
|
72
|
+
getAppWorkerFile() {
|
|
73
|
+
let appWorkerFile = path.join(getSrcDirname(), "app_worker.js");
|
|
74
|
+
if (!existsSync(appWorkerFile)) appWorkerFile = path.join(getSrcDirname(), "app_worker.ts");
|
|
75
|
+
return appWorkerFile;
|
|
76
|
+
}
|
|
77
|
+
fork() {
|
|
78
|
+
throw new Error("BaseApp should implement fork.");
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/utils/mode/impl/process/app.ts
|
|
84
|
+
var AppProcessWorker = class extends BaseAppWorker {
|
|
85
|
+
get id() {
|
|
86
|
+
return this.instance.id;
|
|
87
|
+
}
|
|
88
|
+
get workerId() {
|
|
89
|
+
return this.instance.process.pid;
|
|
90
|
+
}
|
|
91
|
+
get exitedAfterDisconnect() {
|
|
92
|
+
return this.instance.exitedAfterDisconnect;
|
|
93
|
+
}
|
|
94
|
+
get exitCode() {
|
|
95
|
+
return this.instance.process.exitCode;
|
|
96
|
+
}
|
|
97
|
+
send(message) {
|
|
98
|
+
sendmessage(this.instance, message);
|
|
99
|
+
}
|
|
100
|
+
clean() {
|
|
101
|
+
this.instance.removeAllListeners();
|
|
102
|
+
}
|
|
103
|
+
static get workerId() {
|
|
104
|
+
return process.pid;
|
|
105
|
+
}
|
|
106
|
+
static on(event, listener) {
|
|
107
|
+
process.on(event, listener);
|
|
108
|
+
}
|
|
109
|
+
static send(message) {
|
|
110
|
+
message.senderWorkerId = String(process.pid);
|
|
111
|
+
process.send(message);
|
|
112
|
+
}
|
|
113
|
+
static kill() {
|
|
114
|
+
process.exitCode = 1;
|
|
115
|
+
process.kill(process.pid);
|
|
116
|
+
}
|
|
117
|
+
static gracefulExit(options) {
|
|
118
|
+
graceful(options);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
var AppProcessUtils = class extends BaseAppUtils {
|
|
122
|
+
fork() {
|
|
123
|
+
this.startTime = Date.now();
|
|
124
|
+
this.startSuccessCount = 0;
|
|
125
|
+
const args = [JSON.stringify(this.options)];
|
|
126
|
+
this.log("[master] start appWorker with args %j (process)", args);
|
|
127
|
+
cfork({
|
|
128
|
+
exec: this.getAppWorkerFile(),
|
|
129
|
+
args,
|
|
130
|
+
silent: false,
|
|
131
|
+
count: this.options.workers,
|
|
132
|
+
refork: this.isProduction,
|
|
133
|
+
windowsHide: process.platform === "win32"
|
|
134
|
+
});
|
|
135
|
+
let debugPort = process.debugPort;
|
|
136
|
+
cluster.on("fork", (worker) => {
|
|
137
|
+
const appWorker = new AppProcessWorker(worker);
|
|
138
|
+
this.emit("worker_forked", appWorker);
|
|
139
|
+
appWorker.disableRefork = true;
|
|
140
|
+
worker.on("message", (msg) => {
|
|
141
|
+
if (typeof msg === "string") msg = {
|
|
142
|
+
action: msg,
|
|
143
|
+
data: msg
|
|
144
|
+
};
|
|
145
|
+
msg.from = "app";
|
|
146
|
+
this.messenger.send(msg);
|
|
147
|
+
});
|
|
148
|
+
this.log("[master] app_worker#%s:%s start, state: %s, current workers: %j", appWorker.id, appWorker.workerId, appWorker.state, Object.keys(cluster.workers));
|
|
149
|
+
if (this.options.isDebug) {
|
|
150
|
+
debugPort++;
|
|
151
|
+
this.messenger.send({
|
|
152
|
+
to: "parent",
|
|
153
|
+
from: "app",
|
|
154
|
+
action: "debug",
|
|
155
|
+
data: {
|
|
156
|
+
debugPort,
|
|
157
|
+
pid: appWorker.workerId,
|
|
158
|
+
workerId: appWorker.workerId
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
cluster.on("disconnect", (worker) => {
|
|
164
|
+
const appWorker = new AppProcessWorker(worker);
|
|
165
|
+
this.log("[master] app_worker#%s:%s disconnect, suicide: %s, state: %s, current workers: %j", appWorker.id, appWorker.workerId, appWorker.exitedAfterDisconnect, appWorker.state, Object.keys(cluster.workers));
|
|
166
|
+
});
|
|
167
|
+
cluster.on("exit", (worker, code, signal) => {
|
|
168
|
+
const appWorker = new AppProcessWorker(worker);
|
|
169
|
+
this.messenger.send({
|
|
170
|
+
action: "app-exit",
|
|
171
|
+
data: {
|
|
172
|
+
workerId: appWorker.workerId,
|
|
173
|
+
code,
|
|
174
|
+
signal
|
|
175
|
+
},
|
|
176
|
+
to: "master",
|
|
177
|
+
from: "app"
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
async kill(timeout) {
|
|
183
|
+
await Promise.all(Object.keys(cluster.workers).map((id) => {
|
|
184
|
+
const worker = cluster.workers[id];
|
|
185
|
+
Reflect.set(worker, "disableRefork", true);
|
|
186
|
+
return terminate(worker.process, timeout);
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/utils/mode/impl/worker_threads/app.ts
|
|
193
|
+
var AppThreadWorker = class extends BaseAppWorker {
|
|
194
|
+
#state = "none";
|
|
195
|
+
#id;
|
|
196
|
+
constructor(instance, id) {
|
|
197
|
+
super(instance);
|
|
198
|
+
this.#id = id;
|
|
199
|
+
}
|
|
200
|
+
get id() {
|
|
201
|
+
return this.#id;
|
|
202
|
+
}
|
|
203
|
+
get workerId() {
|
|
204
|
+
return this.instance.threadId;
|
|
205
|
+
}
|
|
206
|
+
get state() {
|
|
207
|
+
return this.#state;
|
|
208
|
+
}
|
|
209
|
+
set state(val) {
|
|
210
|
+
this.#state = val;
|
|
211
|
+
}
|
|
212
|
+
get exitedAfterDisconnect() {
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
get exitCode() {
|
|
216
|
+
return 0;
|
|
217
|
+
}
|
|
218
|
+
send(message) {
|
|
219
|
+
this.instance.postMessage(message);
|
|
220
|
+
}
|
|
221
|
+
clean() {
|
|
222
|
+
this.instance.removeAllListeners();
|
|
223
|
+
}
|
|
224
|
+
static get workerId() {
|
|
225
|
+
return threadId;
|
|
226
|
+
}
|
|
227
|
+
static on(event, listener) {
|
|
228
|
+
parentPort.on(event, listener);
|
|
229
|
+
}
|
|
230
|
+
static send(message) {
|
|
231
|
+
message.senderWorkerId = String(threadId);
|
|
232
|
+
parentPort.postMessage(message);
|
|
233
|
+
}
|
|
234
|
+
static kill() {
|
|
235
|
+
process.exit(1);
|
|
236
|
+
}
|
|
237
|
+
static gracefulExit(options) {
|
|
238
|
+
process.on("exit", async (code) => {
|
|
239
|
+
if (typeof options.beforeExit === "function") await options.beforeExit();
|
|
240
|
+
process.exit(code);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
var AppThreadUtils = class extends BaseAppUtils {
|
|
245
|
+
#workers = [];
|
|
246
|
+
#forkSingle(appPath, options, id) {
|
|
247
|
+
const worker = new Worker(appPath, options);
|
|
248
|
+
this.#workers.push(worker);
|
|
249
|
+
const appWorker = new AppThreadWorker(worker, id);
|
|
250
|
+
this.emit("worker_forked", appWorker);
|
|
251
|
+
appWorker.disableRefork = true;
|
|
252
|
+
worker.on("message", (msg) => {
|
|
253
|
+
if (typeof msg === "string") msg = {
|
|
254
|
+
action: msg,
|
|
255
|
+
data: msg
|
|
256
|
+
};
|
|
257
|
+
msg.from = "app";
|
|
258
|
+
this.messenger.send(msg);
|
|
259
|
+
});
|
|
260
|
+
this.log("[master] app_worker#%s (tid:%s) start", appWorker.id, appWorker.workerId);
|
|
261
|
+
let debugPort = process.debugPort;
|
|
262
|
+
if (this.options.isDebug) {
|
|
263
|
+
debugPort++;
|
|
264
|
+
this.messenger.send({
|
|
265
|
+
to: "parent",
|
|
266
|
+
from: "app",
|
|
267
|
+
action: "debug",
|
|
268
|
+
data: {
|
|
269
|
+
debugPort,
|
|
270
|
+
pid: appWorker.workerId,
|
|
271
|
+
workerId: appWorker.workerId
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
worker.on("exit", async (code) => {
|
|
276
|
+
appWorker.state = "dead";
|
|
277
|
+
this.messenger.send({
|
|
278
|
+
action: "app-exit",
|
|
279
|
+
data: {
|
|
280
|
+
workerId: appWorker.workerId,
|
|
281
|
+
code
|
|
282
|
+
},
|
|
283
|
+
to: "master",
|
|
284
|
+
from: "app"
|
|
285
|
+
});
|
|
286
|
+
await setTimeout(1e3);
|
|
287
|
+
this.#forkSingle(appPath, options, id);
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
fork() {
|
|
291
|
+
this.startTime = Date.now();
|
|
292
|
+
this.startSuccessCount = 0;
|
|
293
|
+
const ports = this.options.ports ?? [];
|
|
294
|
+
if (!ports.length) ports.push(this.options.port);
|
|
295
|
+
this.options.workers = ports.length;
|
|
296
|
+
let i = 0;
|
|
297
|
+
do {
|
|
298
|
+
const options = Object.assign({}, this.options, { port: ports[i] });
|
|
299
|
+
const argv = [JSON.stringify(options)];
|
|
300
|
+
this.#forkSingle(this.getAppWorkerFile(), { argv }, ++i);
|
|
301
|
+
} while (i < ports.length);
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
async kill() {
|
|
305
|
+
for (const worker of this.#workers) {
|
|
306
|
+
const id = Reflect.get(worker, "id");
|
|
307
|
+
this.log(`[master] kill app worker#${id} (worker_threads) by worker.terminate()`);
|
|
308
|
+
worker.removeAllListeners();
|
|
309
|
+
worker.terminate();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
//#endregion
|
|
315
|
+
export { AppProcessUtils, AppProcessWorker, AppThreadUtils, AppThreadWorker };
|
package/dist/app_worker.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { AppThreadWorker } from "./
|
|
1
|
+
import "./terminate-w3g0oQgq.js";
|
|
2
|
+
import { AppProcessWorker, AppThreadWorker } from "./app-5Was1vub.js";
|
|
3
3
|
import { debuglog } from "node:util";
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import { EggConsoleLogger } from "egg-logger";
|