Skip to content

Commit 9c20444

Browse files
committed
f
1 parent 06e50cc commit 9c20444

File tree

12 files changed

+87
-83
lines changed

12 files changed

+87
-83
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@
1616
},
1717
"dependencies": {},
1818
"devDependencies": {
19-
"coffee": "^4.1.0",
20-
"cross-env": "^5.0.1",
21-
"fkill": "^7.2.1",
22-
"mm": "^2.2.0",
23-
"mz-modules": "^2.1.0",
24-
"urllib": "^2.26.0",
2519
"@arethetypeswrong/cli": "^0.17.1",
2620
"@eggjs/tsconfig": "1",
27-
"@types/node": "22",
2821
"@types/mocha": "10",
22+
"@types/node": "22",
23+
"coffee": "^5.5.1",
2924
"egg-bin": "6",
3025
"eslint": "8",
3126
"eslint-config-egg": "14",
27+
"fkill": "^7.2.1",
28+
"mm": "^3.4.0",
3229
"tshy": "3",
3330
"tshy-after": "1",
34-
"typescript": "5"
31+
"typescript": "5",
32+
"urllib": "^4.6.8"
3533
},
3634
"scripts": {
3735
"lint": "eslint --cache src test --ext .ts",
38-
"test": "npm run lint -- --fix && egg-bin test",
39-
"ci": "npm run lint && egg-bin cov && npm run prepublishOnly && attw --pack",
36+
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
37+
"test": "egg-bin test",
38+
"preci": "npm run lint && npm run prepublishOnly",
39+
"ci": "egg-bin cov && attw --pack",
4040
"prepublishOnly": "tshy && tshy-after"
4141
},
4242
"type": "module",

src/exit.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import assert from 'node:assert';
22
import { setTimeout } from 'node:timers/promises';
33
import type { Logger, BeforeExit } from './types.js';
44

5+
const TIMEOUT = Symbol('before exit timeout');
6+
57
export function getExitFunction(logger: Logger, label: string, timeout: number, beforeExit?: BeforeExit) {
68
if (beforeExit) {
79
assert(typeof beforeExit === 'function', 'beforeExit only support function');
@@ -19,14 +21,17 @@ export function getExitFunction(logger: Logger, label: string, timeout: number,
1921

2022
return async function exitFunction(code: number) {
2123
try {
22-
await Promise.race([
24+
const result = await Promise.race([
2325
handler(),
24-
setTimeout(timeout),
26+
setTimeout(timeout, TIMEOUT),
2527
]);
28+
if (result === TIMEOUT) {
29+
throw new Error(`Timeout ${timeout}ms`);
30+
}
2631
logger.info('[%s] beforeExit success', label);
2732
process.exit(code);
28-
} catch (err) {
29-
logger.error('[%s] beforeExit fail, error: %s', label, err);
33+
} catch (err: any) {
34+
logger.error('[%s] beforeExit fail, error: %s', label, err.message);
3035
process.exit(code);
3136
}
3237
};

test/fixtures/_async.js renamed to test/fixtures/_async.cjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
'use strict';
2-
3-
const sleep = require('mz-modules/sleep');
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const { setTimeout: sleep } = require('node:timers/promises');
43

54
module.exports = async () => {
65
console.log('process exiting');

test/fixtures/_async_error.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const { setTimeout: sleep } = require('node:timers/promises');
3+
4+
module.exports = async () => {
5+
await sleep(1000);
6+
throw new Error('reject');
7+
};

test/fixtures/_async_error.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/fixtures/before-exit.js renamed to test/fixtures/before-exit.cjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict';
2-
3-
const http = require('http');
4-
const sleep = require('mz-modules/sleep');
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const http = require('node:http');
3+
const { setTimeout: sleep } = require('node:timers/promises');
4+
const { graceful } = require('../..');
55

66
http.createServer((req, res) => {
77
res.writeHead(200);
@@ -11,10 +11,10 @@ http.createServer((req, res) => {
1111
let beforeExit;
1212
switch (process.env.MODE) {
1313
case 'async':
14-
beforeExit = require('./_async');
14+
beforeExit = require('./_async.cjs');
1515
break;
1616
case 'async-error':
17-
beforeExit = require('./_async_error');
17+
beforeExit = require('./_async_error.cjs');
1818
break;
1919
case 'promise':
2020
beforeExit = () => {
@@ -42,10 +42,10 @@ switch (process.env.MODE) {
4242
}
4343

4444
console.log(`Worker ${process.pid} started`);
45-
require('../..')({
45+
graceful({
4646
logger: console,
4747
beforeExit,
4848
});
4949

5050
// run again should work
51-
require('../..')();
51+
graceful();
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
'use strict';
2-
3-
const http = require('http');
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const http = require('node:http');
3+
const { graceful } = require('../..');
44

55
http.createServer((req, res) => {
66
res.writeHead(200);
77
res.end('hello world\n');
88
}).listen(8000);
99

1010
console.log(`Worker ${process.pid} started`);
11-
require('../..')({
11+
graceful({
1212
logger: console,
1313
label: 'test-child',
1414
logLevel: process.env.NODE_LOG_LEVEL,
1515
});
16+
1617
// run again should work
17-
require('../..')();
18+
graceful();

test/fixtures/cluster.js renamed to test/fixtures/cluster.cjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
'use strict';
2-
3-
const cluster = require('cluster');
4-
const http = require('http');
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const cluster = require('node:cluster');
3+
const http = require('node:http');
54

65
if (cluster.isMaster) {
76
console.log(`Master ${process.pid} is running`);
@@ -35,7 +34,8 @@ if (cluster.isMaster) {
3534
}).listen(8000);
3635

3736
console.log(`Worker ${process.pid} started`);
38-
require('../..')({
37+
const { graceful } = require('../..');
38+
graceful({
3939
label: 'app-worker-' + cluster.worker.id,
4040
logLevel: process.env.NODE_LOG_LEVEL,
4141
});

test/fixtures/master-spawn.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
'use strict';
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const childProcess = require('node:child_process');
3+
const path = require('node:path');
24

3-
const childProcess = require('child_process');
4-
const path = require('path');
5-
6-
const childFile = path.join(__dirname, 'child.js');
5+
const childFile = path.join(__dirname, 'child.cjs');
76

87
childProcess.spawn(process.execPath, [ childFile ], {
98
stdio: 'inherit',

test/fixtures/master.cjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const childProcess = require('node:child_process');
3+
const path = require('node:path');
4+
5+
const childFile = path.join(__dirname, 'child.cjs');
6+
7+
childProcess.fork(childFile);
8+
9+
console.log('master fork %s done', childFile);

0 commit comments

Comments
 (0)