Skip to content

Commit e199945

Browse files
committed
feat: support timeout
1 parent fcc6c87 commit e199945

File tree

9 files changed

+131
-106
lines changed

9 files changed

+131
-106
lines changed

.github/workflows/nodejs.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches:
9+
- main
10+
- master
11+
pull_request:
12+
branches:
13+
- main
14+
- master
15+
schedule:
16+
- cron: '0 2 * * *'
17+
18+
jobs:
19+
build:
20+
runs-on: ${{ matrix.os }}
21+
22+
strategy:
23+
fail-fast: false
24+
matrix:
25+
node-version: [8, 10, 12, 14, 16, 18]
26+
os: [ubuntu-latest, windows-latest, macos-latest]
27+
28+
steps:
29+
- name: Checkout Git Source
30+
uses: actions/checkout@v2
31+
32+
- name: Use Node.js ${{ matrix.node-version }}
33+
uses: actions/setup-node@v1
34+
with:
35+
node-version: ${{ matrix.node-version }}
36+
37+
- name: Install Dependencies
38+
run: npm i -g npminstall@5 && npminstall
39+
40+
- name: Continuous Integration
41+
run: npm run ci
42+
43+
- name: Code Coverage
44+
uses: codecov/codecov-action@v1
45+
with:
46+
token: ${{ secrets.CODECOV_TOKEN }}

.travis.yml

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

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: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
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;
8-
9-
function getExitFunction(beforeExit, logger, label) {
10-
if (beforeExit) assert(is.function(beforeExit), 'beforeExit only support function');
7+
module.exports = function getExitFunction(beforeExit, logger, label, timeout) {
8+
assert(!beforeExit || typeof beforeExit === 'function', 'beforeExit only support function');
119

1210
return once(code => {
1311
if (!beforeExit) process.exit(code);
14-
Promise.resolve()
15-
.then(() => {
16-
return beforeExit();
17-
})
12+
13+
pt.timeout(new Promise(resolve => resolve(beforeExit())), timeout)
1814
.then(() => {
1915
logger.info('[%s] beforeExit success', label);
2016
process.exit(code);
@@ -24,5 +20,4 @@ function getExitFunction(beforeExit, logger, label) {
2420
process.exit(code);
2521
});
2622
});
27-
28-
}
23+
};

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"author": "fengmk2",
2626
"license": "MIT",
2727
"dependencies": {
28-
"is-type-of": "^1.2.0",
29-
"once": "^1.4.0"
28+
"once": "^1.4.0",
29+
"promise-timeout": "^1.3.0"
3030
},
3131
"devDependencies": {
3232
"autod": "^3.0.1",
@@ -41,6 +41,7 @@
4141
"urllib": "^2.26.0"
4242
},
4343
"ci": {
44-
"version": "6, 8, 9"
44+
"version": "8, 10, 12, 14, 16, 18",
45+
"type": "github"
4546
}
4647
}

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)