@eggjs/cluster 3.0.0-beta.1 → 3.0.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 +4 -4
- package/dist/commonjs/app_worker.js +14 -7
- package/dist/commonjs/master.js +13 -2
- package/dist/commonjs/package-lock.json +5618 -0
- package/dist/commonjs/utils/messenger.d.ts +5 -1
- package/dist/commonjs/utils/messenger.js +10 -7
- package/dist/commonjs/utils/mode/base/app.d.ts +1 -0
- package/dist/commonjs/utils/mode/base/app.js +5 -1
- package/dist/commonjs/utils/mode/impl/process/agent.js +2 -2
- package/dist/commonjs/utils/mode/impl/process/app.d.ts +1 -0
- package/dist/commonjs/utils/mode/impl/process/app.js +6 -16
- package/dist/commonjs/utils/mode/impl/worker_threads/agent.js +2 -2
- package/dist/commonjs/utils/mode/impl/worker_threads/app.d.ts +1 -0
- package/dist/commonjs/utils/mode/impl/worker_threads/app.js +6 -21
- package/dist/esm/app_worker.js +14 -7
- package/dist/esm/master.js +13 -2
- package/dist/esm/package-lock.json +5618 -0
- package/dist/esm/utils/messenger.d.ts +5 -1
- package/dist/esm/utils/messenger.js +10 -7
- package/dist/esm/utils/mode/base/app.d.ts +1 -0
- package/dist/esm/utils/mode/base/app.js +5 -1
- package/dist/esm/utils/mode/impl/process/agent.js +2 -2
- package/dist/esm/utils/mode/impl/process/app.d.ts +1 -0
- package/dist/esm/utils/mode/impl/process/app.js +6 -16
- package/dist/esm/utils/mode/impl/worker_threads/agent.js +2 -2
- package/dist/esm/utils/mode/impl/worker_threads/app.d.ts +1 -0
- package/dist/esm/utils/mode/impl/worker_threads/app.js +6 -21
- package/dist/package.json +1 -1
- package/package.json +4 -4
- package/src/app_worker.ts +14 -6
- package/src/master.ts +13 -3
- package/src/package-lock.json +5618 -0
- package/src/utils/messenger.ts +14 -8
- package/src/utils/mode/base/app.ts +6 -0
- package/src/utils/mode/impl/process/agent.ts +1 -1
- package/src/utils/mode/impl/process/app.ts +7 -16
- package/src/utils/mode/impl/worker_threads/agent.ts +1 -1
- package/src/utils/mode/impl/worker_threads/app.ts +7 -24
package/src/utils/messenger.ts
CHANGED
|
@@ -12,9 +12,12 @@ export interface MessageBody {
|
|
|
12
12
|
data?: unknown;
|
|
13
13
|
to?: MessageCharacter;
|
|
14
14
|
from?: MessageCharacter;
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Keep compatible, please use receiverWorkerId instead
|
|
17
|
+
*/
|
|
15
18
|
receiverPid?: string;
|
|
16
|
-
|
|
17
|
-
senderWorkerId?:
|
|
19
|
+
receiverWorkerId?: string;
|
|
20
|
+
senderWorkerId?: string;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
/**
|
|
@@ -93,11 +96,12 @@ export class Messenger {
|
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
// https://github.com/eggjs/egg/blob/b6861f1c7548f05a281386050dfeaeb30f236558/lib/core/messenger/ipc.js#L56
|
|
96
|
-
// recognize
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
// recognize receiverWorkerId is to who
|
|
100
|
+
const receiverWorkerId = data.receiverWorkerId ?? data.receiverPid;
|
|
101
|
+
if (receiverWorkerId) {
|
|
102
|
+
if (receiverWorkerId === String(process.pid)) {
|
|
99
103
|
data.to = 'master';
|
|
100
|
-
} else if (
|
|
104
|
+
} else if (receiverWorkerId === String(this.#workerManager.getAgent()!.workerId)) {
|
|
101
105
|
data.to = 'agent';
|
|
102
106
|
} else {
|
|
103
107
|
data.to = 'app';
|
|
@@ -157,6 +161,7 @@ export class Messenger {
|
|
|
157
161
|
* @param {Object} data message body
|
|
158
162
|
*/
|
|
159
163
|
sendToMaster(data: MessageBody) {
|
|
164
|
+
// e.g: master.on('app-start', data => {})
|
|
160
165
|
this.#master.emit(data.action, data.data);
|
|
161
166
|
}
|
|
162
167
|
|
|
@@ -180,8 +185,9 @@ export class Messenger {
|
|
|
180
185
|
if (worker.state === 'disconnected') {
|
|
181
186
|
continue;
|
|
182
187
|
}
|
|
183
|
-
// check
|
|
184
|
-
|
|
188
|
+
// check receiverWorkerId
|
|
189
|
+
const receiverWorkerId = data.receiverWorkerId ?? data.receiverPid;
|
|
190
|
+
if (receiverWorkerId && receiverWorkerId !== String(worker.workerId)) {
|
|
185
191
|
continue;
|
|
186
192
|
}
|
|
187
193
|
worker.send(data);
|
|
@@ -52,6 +52,12 @@ export abstract class BaseAppWorker<T = ThreadWorker | ClusterProcessWorker> {
|
|
|
52
52
|
throw new Error('BaseAppWorker should implement clean.');
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
// static methods use on src/app_worker.ts
|
|
56
|
+
|
|
57
|
+
static get workerId(): number {
|
|
58
|
+
throw new Error('BaseAppWorker should implement workerId.');
|
|
59
|
+
}
|
|
60
|
+
|
|
55
61
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
56
62
|
static on(..._args: any[]) {
|
|
57
63
|
throw new Error('BaseAppWorker should implement on.');
|
|
@@ -31,12 +31,18 @@ export class AppProcessWorker extends BaseAppWorker<ClusterProcessWorker> {
|
|
|
31
31
|
this.instance.removeAllListeners();
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
// static methods use on src/app_worker.ts
|
|
35
|
+
|
|
36
|
+
static get workerId() {
|
|
37
|
+
return process.pid;
|
|
38
|
+
}
|
|
39
|
+
|
|
34
40
|
static on(event: string, listener: (...args: any[]) => void) {
|
|
35
41
|
process.on(event, listener);
|
|
36
42
|
}
|
|
37
43
|
|
|
38
44
|
static send(message: MessageBody) {
|
|
39
|
-
message.senderWorkerId = process.pid;
|
|
45
|
+
message.senderWorkerId = String(process.pid);
|
|
40
46
|
process.send!(message);
|
|
41
47
|
}
|
|
42
48
|
|
|
@@ -121,21 +127,6 @@ export class AppProcessUtils extends BaseAppUtils {
|
|
|
121
127
|
from: 'app',
|
|
122
128
|
});
|
|
123
129
|
});
|
|
124
|
-
cluster.on('listening', (worker, address) => {
|
|
125
|
-
const appWorker = new AppProcessWorker(worker);
|
|
126
|
-
appWorker.state = 'listening';
|
|
127
|
-
this.log('[master] app_worker#%s:%s listening at %j', appWorker.id, appWorker.workerId, address);
|
|
128
|
-
this.messenger.send({
|
|
129
|
-
action: 'app-start',
|
|
130
|
-
data: {
|
|
131
|
-
workerId: appWorker.workerId,
|
|
132
|
-
address,
|
|
133
|
-
},
|
|
134
|
-
to: 'master',
|
|
135
|
-
from: 'app',
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
|
|
139
130
|
return this;
|
|
140
131
|
}
|
|
141
132
|
|
|
@@ -14,7 +14,7 @@ export class AgentThreadWorker extends BaseAgentWorker<Worker> {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
static send(message: MessageBody) {
|
|
17
|
-
message.senderWorkerId = workerThreads.threadId;
|
|
17
|
+
message.senderWorkerId = String(workerThreads.threadId);
|
|
18
18
|
workerThreads.parentPort!.postMessage(message);
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -46,12 +46,18 @@ export class AppThreadWorker extends BaseAppWorker<ThreadWorker> {
|
|
|
46
46
|
this.instance.removeAllListeners();
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
// static methods use on src/app_worker.ts
|
|
50
|
+
|
|
51
|
+
static get workerId() {
|
|
52
|
+
return threadId;
|
|
53
|
+
}
|
|
54
|
+
|
|
49
55
|
static on(event: string, listener: (...args: any[]) => void) {
|
|
50
56
|
parentPort!.on(event, listener);
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
static send(message: MessageBody) {
|
|
54
|
-
message.senderWorkerId = threadId;
|
|
60
|
+
message.senderWorkerId = String(threadId);
|
|
55
61
|
parentPort!.postMessage(message);
|
|
56
62
|
}
|
|
57
63
|
|
|
@@ -109,29 +115,6 @@ export class AppThreadUtils extends BaseAppUtils {
|
|
|
109
115
|
});
|
|
110
116
|
}
|
|
111
117
|
|
|
112
|
-
// handle worker listening
|
|
113
|
-
worker.on('message', ({ action, data: address }) => {
|
|
114
|
-
if (action !== 'listening') {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (!address) {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
appWorker.state = 'listening';
|
|
123
|
-
this.messenger.send({
|
|
124
|
-
action: 'app-start',
|
|
125
|
-
data: {
|
|
126
|
-
workerId: appWorker.workerId,
|
|
127
|
-
address,
|
|
128
|
-
},
|
|
129
|
-
to: 'master',
|
|
130
|
-
from: 'app',
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
});
|
|
134
|
-
|
|
135
118
|
// handle worker exit
|
|
136
119
|
worker.on('exit', async code => {
|
|
137
120
|
appWorker.state = 'dead';
|