Skip to content

Commit 8551bae

Browse files
atian25fengmk2
andauthored
feat: support timeout && drop node 14 support (#6)
egg-cluster 里面的 beforeExit 是 agent.close,有可能存在某些插件的监听了 lifecycle 的 beforeClose 然后一直没返回的情况 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new CI workflow for Node.js projects, supporting multiple operating systems and Node.js versions. - Added new badges in the README for NPM version, quality, downloads, and CI status. - **Bug Fixes** - Enhanced error handling and control flow for exit functionality with timeout management. - **Documentation** - Updated README.md to improve visibility and presentation of project metrics. - **Chores** - Removed outdated CI configuration files for Travis CI and AppVeyor. - Updated project dependencies in package.json. - **Tests** - Transitioned tests to use async/await syntax for better readability and maintainability. - Added a new test case to handle timeout scenarios. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: fengmk2 <[email protected]>
1 parent fcc6c87 commit 8551bae

File tree

11 files changed

+125
-145
lines changed

11 files changed

+125
-145
lines changed

.autod.conf.js

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

.github/workflows/nodejs.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
Job:
11+
name: Node.js
12+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
13+
with:
14+
os: 'ubuntu-latest, macos-latest'
15+
version: '14, 16, 18, 20, 22'
16+
secrets:
17+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.
File renamed without changes.

README.md

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@
22

33
graceful exit process even parent exit on SIGKILL.
44

5-
[![NPM version][npm-image]][npm-url]
6-
[![build status][travis-image]][travis-url]
7-
[![Test coverage][codecov-image]][codecov-url]
8-
[![David deps][david-image]][david-url]
9-
[![Known Vulnerabilities][snyk-image]][snyk-url]
10-
[![NPM download][download-image]][download-url]
11-
12-
[npm-image]: https://img.shields.io/npm/v/graceful-process.svg?style=flat-square
13-
[npm-url]: https://npmjs.org/package/graceful-process
14-
[travis-image]: https://img.shields.io/travis/node-modules/graceful-process.svg?style=flat-square
15-
[travis-url]: https://travis-ci.org/node-modules/graceful-process
16-
[codecov-image]: https://codecov.io/gh/node-modules/graceful-process/branch/master/graph/badge.svg
17-
[codecov-url]: https://codecov.io/gh/node-modules/graceful-process
18-
[david-image]: https://img.shields.io/david/node-modules/graceful-process.svg?style=flat-square
19-
[david-url]: https://david-dm.org/{{org}}/graceful-process
20-
[snyk-image]: https://snyk.io/test/npm/graceful-process/badge.svg?style=flat-square
21-
[snyk-url]: https://snyk.io/test/npm/graceful-process
22-
[download-image]: https://img.shields.io/npm/dm/graceful-process.svg?style=flat-square
23-
[download-url]: https://npmjs.org/package/graceful-process
5+
[![NPM version](https://img.shields.io/npm/v/graceful-process.svg?style=flat-square)](https://npmjs.org/package/graceful-process)
6+
[![NPM quality](http://npm.packagequality.com/shield/graceful-process.svg?style=flat-square)](http://packagequality.com/#?package=graceful-process)
7+
[![NPM download](https://img.shields.io/npm/dm/graceful-process.svg?style=flat-square)](https://npmjs.org/package/graceful-process)
8+
9+
[![Continuous Integration](https://github.com/node-modules/graceful-process/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/graceful-process/actions/workflows/nodejs.yml)
10+
[![Test coverage](https://img.shields.io/codecov/c/github/node-modules/graceful-process.svg?style=flat-square)](https://codecov.io/gh/node-modules/graceful-process)
11+
2412

2513
## Install
2614

appveyor.yml

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

exit.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
'use strict';
22

33
const assert = require('assert');
4-
const is = require('is-type-of');
54
const once = require('once');
5+
const pt = require('promise-timeout');
66

7-
module.exports = getExitFunction;
7+
module.exports = function getExitFunction(beforeExit, logger, label, timeout) {
8+
assert(!beforeExit || typeof beforeExit === 'function', 'beforeExit only support function');
89

9-
function getExitFunction(beforeExit, logger, label) {
10-
if (beforeExit) assert(is.function(beforeExit), 'beforeExit only support function');
10+
// only call beforeExit once
11+
const handler = once(() => {
12+
return new Promise(resolve => {
13+
resolve(beforeExit && beforeExit());
14+
});
15+
});
1116

12-
return once(code => {
13-
if (!beforeExit) process.exit(code);
14-
Promise.resolve()
15-
.then(() => {
16-
return beforeExit();
17-
})
17+
return function exitFunction(code) {
18+
pt.timeout(handler(), timeout)
1819
.then(() => {
1920
logger.info('[%s] beforeExit success', label);
2021
process.exit(code);
@@ -23,6 +24,5 @@ function getExitFunction(beforeExit, logger, label) {
2324
logger.error('[%s] beforeExit fail, error: %s', label, err.message);
2425
process.exit(code);
2526
});
26-
});
27-
28-
}
27+
};
28+
};

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ module.exports = (options = {}) => {
2424
printLogLevels.warn = false;
2525
}
2626
const label = options.label || `graceful-process#${process.pid}`;
27+
const timeout = options.timeout || parseInt(process.env.GRACEFUL_TIMEOUT) || 5000;
2728

2829
if (process[init]) {
2930
printLogLevels.warn && logger.warn('[%s] graceful-process init already', label);
3031
return;
3132
}
3233
process[init] = true;
3334

34-
const exit = getExitFunction(options.beforeExit, logger, label);
35+
const exit = getExitFunction(options.beforeExit, logger, label, timeout);
3536

3637
// https://github.com/eggjs/egg-cluster/blob/master/lib/agent_worker.js#L35
3738
// exit gracefully

package.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
"test": "npm run lint && npm run test-local",
1212
"test-local": "egg-bin test",
1313
"cov": "cross-env COV=true egg-bin cov",
14-
"autod": "autod",
15-
"pkgfiles": "egg-bin pkgfiles",
1614
"ci": "npm run lint && npm run cov"
1715
},
1816
"repository": {
@@ -25,22 +23,18 @@
2523
"author": "fengmk2",
2624
"license": "MIT",
2725
"dependencies": {
28-
"is-type-of": "^1.2.0",
29-
"once": "^1.4.0"
26+
"once": "^1.4.0",
27+
"promise-timeout": "^1.3.0"
3028
},
3129
"devDependencies": {
32-
"autod": "^3.0.1",
3330
"coffee": "^4.1.0",
3431
"cross-env": "^5.0.1",
35-
"egg-bin": "^4.3.7",
36-
"egg-ci": "^1.8.0",
32+
"egg-bin": "^6.13.0",
3733
"eslint": "^4.18.1",
3834
"eslint-config-egg": "^7.0.0",
35+
"fkill": "^7.2.1",
3936
"mm": "^2.2.0",
4037
"mz-modules": "^2.1.0",
4138
"urllib": "^2.26.0"
42-
},
43-
"ci": {
44-
"version": "6, 8, 9"
4539
}
4640
}

test/fixtures/before-exit.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ switch (process.env.MODE) {
2222
return sleep(1000).then(() => console.log('process exited'));
2323
};
2424
break;
25+
case 'timeout':
26+
beforeExit = () => {
27+
console.log('process exiting');
28+
return sleep(5000).then(() => {
29+
throw new Error('should no run here');
30+
});
31+
};
32+
break;
2533
case 'function-error':
2634
beforeExit = () => {
2735
throw new Error('process exit');

0 commit comments

Comments
 (0)