vona-core 5.0.106 → 5.0.107

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/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import '@cabloy/set';
2
2
  import '@cabloy/json5';
3
- import { combineApiPathControllerAndActionRaw, combineApiPathControllerAndAction, combineApiPath, combineResourceName, sleep, isUndefined, isClass, isNilOrEmptyString, evaluateExpressions, catchErrorSync, isEmptyObject, hashkey } from '@cabloy/utils';
3
+ import { combineApiPathControllerAndActionRaw, combineApiPathControllerAndAction, combineApiPath, combineResourceName, sleep, isUndefined, isClass, isNilOrEmptyString, evaluateExpressions, catchErrorSync, isEmptyObject, catchError, hashkey } from '@cabloy/utils';
4
4
  import { toLowerCaseFirstChar, splitWords } from '@cabloy/word-utils';
5
5
  import path from 'node:path';
6
6
  import crypto from 'node:crypto';
@@ -348,7 +348,7 @@ async function saveJSONFile(fileName, json) {
348
348
  function useApp() {
349
349
  return globalThis.__app__;
350
350
  }
351
- async function closeApp(terminate) {
351
+ async function closeApp() {
352
352
  while (globalThis.__closing__) {
353
353
  await sleep(50);
354
354
  }
@@ -361,9 +361,6 @@ async function closeApp(terminate) {
361
361
  } finally {
362
362
  globalThis.__closing__ = false;
363
363
  }
364
- if (terminate) {
365
- process.kill(process.pid, 'SIGTERM');
366
- }
367
364
  }
368
365
  async function createGeneralApp(projectPath, envRuntime) {
369
366
  if (envRuntime) {
@@ -2434,8 +2431,8 @@ class VonaApplication extends KoaApplication {
2434
2431
  const response = res ?? new ResponseMock();
2435
2432
  return this.createContext(request, response);
2436
2433
  }
2437
- async close(terminate) {
2438
- await closeApp(terminate);
2434
+ async close() {
2435
+ await closeApp();
2439
2436
  }
2440
2437
  }
2441
2438
 
@@ -2969,20 +2966,38 @@ function prepareAppInfo(env) {
2969
2966
  };
2970
2967
  }
2971
2968
 
2972
- function handleProcessWork() {
2973
- process.once('SIGUSR2', async () => {
2974
- // console.log('------------SIGUSR2');
2975
- await closeApp(true);
2969
+ let __sigintHandled = false;
2970
+ async function _closeAppInner() {
2971
+ const timeout = setTimeout(() => {
2972
+ // eslint-disable-next-line no-console
2973
+ console.log('Cleanup timed out. Forcing termination...');
2974
+ process.exit(1);
2975
+ }, 5000);
2976
+ const [_, err] = await catchError(() => {
2977
+ return closeApp();
2976
2978
  });
2977
- process.once('SIGINT', async () => {
2979
+ if (err) {
2980
+ console.error(err);
2981
+ }
2982
+ clearTimeout(timeout);
2983
+ process.exit(err ? 1 : 0);
2984
+ }
2985
+ function handleProcessWork() {
2986
+ process.on('SIGINT', async () => {
2987
+ if (__sigintHandled) return;
2988
+ __sigintHandled = true;
2978
2989
  // console.log('------------SIGINT');
2979
- await closeApp(true);
2990
+ await _closeAppInner();
2991
+ });
2992
+ process.on('SIGUSR2', async () => {
2993
+ // console.log('------------SIGUSR2');
2994
+ await _closeAppInner();
2980
2995
  });
2981
2996
  process.on('uncaughtException', async err => {
2982
2997
  const app = useApp();
2983
2998
  if (!app) {
2984
2999
  console.error(err);
2985
- process.kill(process.pid, 'SIGTERM');
3000
+ process.exit(1);
2986
3001
  } else {
2987
3002
  const [logger] = catchErrorSync(() => {
2988
3003
  return app.meta.logger.get();
@@ -2994,51 +3009,42 @@ function handleProcessWork() {
2994
3009
  }
2995
3010
  if (!app.meta.appStarted) {
2996
3011
  await app.meta.logger.dispose();
2997
- process.kill(process.pid, 'SIGTERM');
3012
+ process.exit(1);
2998
3013
  }
2999
3014
  }
3000
3015
  });
3001
3016
  }
3002
3017
  function handleProcessMaster() {
3003
- process.once('SIGUSR2', () => {
3004
- // should not kill master self by manual
3005
- // process.kill(process.pid, 'SIGTERM');
3018
+ process.on('SIGINT', () => {
3019
+ // donothing
3020
+ });
3021
+ process.on('SIGUSR2', () => {
3022
+ // donothing
3006
3023
  });
3007
- // should not kill master self by manual
3008
- // process.once('SIGINT', () => {
3009
- // process.kill(process.pid, 'SIGTERM');
3010
- // });
3011
3024
  }
3012
3025
 
3013
3026
  async function startCluster(workers, bootstrapOptions) {
3014
3027
  if (cluster.isPrimary) {
3015
3028
  handleProcessMaster();
3016
3029
  createAppMaster(bootstrapOptions);
3017
-
3018
- // console.log(`Primary ${process.pid} is running`);
3019
-
3020
- // Fork workers.
3021
3030
  for (let i = 0; i < workers; i++) {
3022
3031
  cluster.fork();
3023
3032
  }
3024
3033
  cluster.on('message', (worker, message) => {
3025
3034
  if (message === 'reload-worker') {
3026
- worker.process.kill('SIGTERM');
3027
3035
  cluster.fork();
3036
+ worker.process.kill('SIGTERM');
3028
3037
  }
3029
3038
  });
3030
3039
  cluster.on('exit', (_worker, _code, _signal) => {
3031
- // console.log(`worker ${worker.process.pid} died`, code, signal);
3032
- // should not kill master self by manual
3033
- // master -> worker, rather than worker -> master
3034
- // if (cluster.workers && Object.keys(cluster.workers).length === 0) {
3035
- // process.kill(process.pid, 'SIGTERM');
3036
- // }
3040
+ // console.log(`----------------- worker ${_worker.process.pid} died`, _code, _signal);
3041
+ if (cluster.workers && Object.keys(cluster.workers).length === 0) {
3042
+ process.exit(0);
3043
+ }
3037
3044
  });
3038
3045
  } else {
3039
3046
  handleProcessWork();
3040
3047
  await createApp(bootstrapOptions);
3041
- // console.log(`Worker ${process.pid} started`);
3042
3048
  }
3043
3049
  }
3044
3050
 
@@ -27,5 +27,5 @@ export declare class VonaApplication extends KoaApplication {
27
27
  /** get specific module's scope */
28
28
  scope<K extends TypeBeanScopeRecordKeys>(moduleScope: K): IBeanScopeRecord[K];
29
29
  createAnonymousContext(req?: any, res?: any): VonaContext;
30
- close(terminate?: boolean): Promise<void>;
30
+ close(): Promise<void>;
31
31
  }
@@ -1,4 +1,4 @@
1
1
  import type { VonaApplication } from '../core/application.ts';
2
2
  export declare function useApp(): VonaApplication;
3
- export declare function closeApp(terminate?: boolean): Promise<void>;
3
+ export declare function closeApp(): Promise<void>;
4
4
  export declare function createGeneralApp(projectPath: string, envRuntime?: Partial<NodeJS.ProcessEnv>): Promise<any>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-core",
3
3
  "type": "module",
4
- "version": "5.0.106",
4
+ "version": "5.0.107",
5
5
  "description": "vona",
6
6
  "publishConfig": {
7
7
  "access": "public"