@eggjs/cluster 3.0.0-beta.2 → 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.
@@ -161,6 +161,7 @@ export class Messenger {
161
161
  * @param {Object} data message body
162
162
  */
163
163
  sendToMaster(data: MessageBody) {
164
+ // e.g: master.on('app-start', data => {})
164
165
  this.#master.emit(data.action, data.data);
165
166
  }
166
167
 
@@ -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';