@midwayjs/bootstrap 3.9.0 → 3.9.1

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.
@@ -10,6 +10,7 @@ export declare abstract class AbstractForkManager<T, ClusterOptions extends Fork
10
10
  protected workers: Map<string, T>;
11
11
  protected eventBus: IEventBus<T>;
12
12
  private isClosing;
13
+ private exitListener;
13
14
  protected constructor(options: ClusterOptions);
14
15
  start(): Promise<void>;
15
16
  protected tryToRefork(oldWorker: T): void;
@@ -30,10 +31,22 @@ export declare abstract class AbstractForkManager<T, ClusterOptions extends Fork
30
31
  */
31
32
  protected onReachReforkLimit(): void;
32
33
  protected killWorker(worker: any, timeout: any): Promise<void>;
33
- close(timeout?: number): Promise<void>;
34
+ stop(timeout?: number): Promise<void>;
34
35
  hasWorker(workerId: string): boolean;
35
36
  getWorker(workerId: string): T;
36
37
  getWorkerIds(): string[];
38
+ onStop(exitListener: any): void;
39
+ protected bindClose(): void;
40
+ /**
41
+ * on bootstrap receive a exit signal
42
+ * @param signal
43
+ */
44
+ private onSignal;
45
+ /**
46
+ * on bootstrap process exit
47
+ * @param code
48
+ */
49
+ private onMasterExit;
37
50
  abstract createWorker(oldWorker?: T): T;
38
51
  abstract bindWorkerDisconnect(listener: (worker: T) => void): void;
39
52
  abstract bindWorkerExit(listener: (worker: T, code: any, signal: any) => void): void;
@@ -72,6 +72,7 @@ class AbstractForkManager {
72
72
  this.tryToRefork(worker);
73
73
  this.onUnexpected(worker, code, signal);
74
74
  });
75
+ this.bindClose();
75
76
  this.hub.on('reachReforkLimit', this.onReachReforkLimit.bind(this));
76
77
  // defer to set the listeners
77
78
  // so you can listen this by your own
@@ -159,7 +160,7 @@ class AbstractForkManager {
159
160
  // subProcess.kill is wrapped to subProcess.destroy, it will wait to disconnected.
160
161
  (worker.process || worker).kill('SIGKILL');
161
162
  }
162
- async close(timeout = 2000) {
163
+ async stop(timeout = 2000) {
163
164
  debug('run close');
164
165
  this.isClosing = true;
165
166
  await this.eventBus.stop();
@@ -167,6 +168,9 @@ class AbstractForkManager {
167
168
  worker['disableRefork'] = true;
168
169
  await this.killWorker(worker, timeout);
169
170
  }
171
+ if (this.exitListener) {
172
+ await this.exitListener();
173
+ }
170
174
  }
171
175
  hasWorker(workerId) {
172
176
  return this.workers.has(workerId);
@@ -177,6 +181,43 @@ class AbstractForkManager {
177
181
  getWorkerIds() {
178
182
  return Array.from(this.workers.keys());
179
183
  }
184
+ onStop(exitListener) {
185
+ this.exitListener = exitListener;
186
+ }
187
+ bindClose() {
188
+ // kill(2) Ctrl-C
189
+ process.once('SIGINT', this.onSignal.bind(this, 'SIGINT'));
190
+ // kill(3) Ctrl-\
191
+ process.once('SIGQUIT', this.onSignal.bind(this, 'SIGQUIT'));
192
+ // kill(15) default
193
+ process.once('SIGTERM', this.onSignal.bind(this, 'SIGTERM'));
194
+ process.once('exit', this.onMasterExit.bind(this));
195
+ }
196
+ /**
197
+ * on bootstrap receive a exit signal
198
+ * @param signal
199
+ */
200
+ async onSignal(signal) {
201
+ if (!this.isClosing) {
202
+ this.options.logger.info('[bootstrap:master] receive signal %s, closing', signal);
203
+ try {
204
+ await this.stop();
205
+ this.options.logger.info('[bootstrap:master] close done, exiting with code:0');
206
+ process.exit(0);
207
+ }
208
+ catch (err) {
209
+ this.options.logger.error('[midway:master] close with error: ', err);
210
+ process.exit(1);
211
+ }
212
+ }
213
+ }
214
+ /**
215
+ * on bootstrap process exit
216
+ * @param code
217
+ */
218
+ onMasterExit(code) {
219
+ this.options.logger.info('[bootstrap:master] exit with code:%s', code);
220
+ }
180
221
  }
181
222
  exports.AbstractForkManager = AbstractForkManager;
182
223
  //# sourceMappingURL=base.js.map
@@ -4,7 +4,7 @@ import { AbstractForkManager } from './base';
4
4
  import { Worker } from 'worker_threads';
5
5
  export declare class ThreadManager extends AbstractForkManager<Worker, ThreadOptions> {
6
6
  readonly options: ThreadOptions;
7
- private exitListener;
7
+ private workerExitListener;
8
8
  constructor(options?: ThreadOptions);
9
9
  createWorker(): Worker;
10
10
  bindWorkerDisconnect(listener: (worker: Worker) => void): void;
@@ -33,7 +33,7 @@ class ThreadManager extends base_1.AbstractForkManager {
33
33
  this.options.logger.info('new worker thread, threadId = %s.', this.getWorkerId(w));
34
34
  }
35
35
  w.on('exit', code => {
36
- this.exitListener(w, code);
36
+ this.workerExitListener(w, code);
37
37
  });
38
38
  return w;
39
39
  }
@@ -41,7 +41,7 @@ class ThreadManager extends base_1.AbstractForkManager {
41
41
  // this.disconnectListener = listener;
42
42
  }
43
43
  bindWorkerExit(listener) {
44
- this.exitListener = listener;
44
+ this.workerExitListener = listener;
45
45
  }
46
46
  getWorkerId(worker) {
47
47
  return worker['_originThreadId'] || String(worker.threadId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/bootstrap",
3
- "version": "3.9.0",
3
+ "version": "3.9.1",
4
4
  "description": "midwayjs bootstrap",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -38,5 +38,5 @@
38
38
  "engines": {
39
39
  "node": ">=12.11.0"
40
40
  },
41
- "gitHead": "5f6603d2c9606fc6fc7ece71f956e89e394efeee"
41
+ "gitHead": "83204ee2b6c0a1e9a1f50b504869b1d48fc18895"
42
42
  }