@eggjs/cluster 3.0.0-beta.2 → 3.0.0-beta.4
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 +23 -9
- package/dist/commonjs/package-lock.json +5618 -0
- package/dist/commonjs/utils/messenger.js +2 -1
- 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/app.d.ts +1 -0
- package/dist/commonjs/utils/mode/impl/process/app.js +5 -15
- package/dist/commonjs/utils/mode/impl/worker_threads/app.d.ts +1 -0
- package/dist/commonjs/utils/mode/impl/worker_threads/app.js +5 -20
- package/dist/esm/app_worker.js +14 -7
- package/dist/esm/master.js +23 -9
- package/dist/esm/package-lock.json +5618 -0
- package/dist/esm/utils/messenger.js +2 -1
- 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/app.d.ts +1 -0
- package/dist/esm/utils/mode/impl/process/app.js +5 -15
- package/dist/esm/utils/mode/impl/worker_threads/app.d.ts +1 -0
- package/dist/esm/utils/mode/impl/worker_threads/app.js +5 -20
- package/dist/package.json +1 -1
- package/package.json +4 -4
- package/src/app_worker.ts +14 -6
- package/src/master.ts +23 -10
- package/src/package-lock.json +5618 -0
- package/src/utils/messenger.ts +1 -0
- package/src/utils/mode/base/app.ts +6 -0
- package/src/utils/mode/impl/process/app.ts +6 -15
- package/src/utils/mode/impl/worker_threads/app.ts +6 -23
package/src/utils/messenger.ts
CHANGED
|
@@ -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,6 +31,12 @@ 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
|
}
|
|
@@ -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
|
|
|
@@ -46,6 +46,12 @@ 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
|
}
|
|
@@ -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';
|