Skip to content

Commit

Permalink
fix: trigger child SIGINT under windows (#3)
Browse files Browse the repository at this point in the history
* fix: trigger child SIGINT under windows

* chore: add github action

* chore: remove node 12

* chore: remove node 12
  • Loading branch information
czy88840616 authored Jan 6, 2024
1 parent a8e046a commit cc30522
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Node.js CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
timeout-minutes: 60
strategy:
matrix:
include:
- os: ubuntu-latest
node-version: 16
- os: ubuntu-latest
node-version: 20
- os: windows-latest
node-version: 20
runs-on: ${{ matrix.os }}
steps:
- name: Git checkout
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- run: npm install && npm install codecov
- run: npm run cov
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function run() {
runChild && runChild.kill();
process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}
}
Expand Down
31 changes: 24 additions & 7 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => {

function innerFork(isFirstFork = false) {
const startTime = Date.now();
runChild = fork(runCmdPath, runArgs, {
runChild = fork('wrap.js', runArgs, {
stdio: 'inherit',
cwd: options.cwd,
cwd: __dirname,
env: {
CHILD_CMD_PATH: runCmdPath,
CHILD_CWD: options.cwd || process.cwd(),
...process.env,
},
// execArgv: process.execArgv.concat(['-r', 'source-map-support/register'])
});

const onServerReady = data => {
if (data.title === 'server-ready') {89
if (data.title === 'server-ready') {
onServerReadyCallback &&
onServerReadyCallback(data, isFirstFork, Date.now() - startTime);
runChild.removeListener('message', onServerReady);
Expand All @@ -75,19 +79,32 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => {

innerFork(true);

return {
kill: signal => {
const killRunningChild = signal => {
if (isWin) {
runChild.send({
title: 'server-kill',
});
} else {
runChild.kill(signal);
}
};

return {
kill(signal) {
killRunningChild(signal);
},
restart() {
// 杀进程
runChild.kill();
killRunningChild();
// 重新拉起来
innerFork();
},
onServerReady(readyCallback) {
onServerReadyCallback = readyCallback;
},
getRealChild() {
return runChild;
},
};
};

Expand Down
12 changes: 12 additions & 0 deletions lib/wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 拿到执行路径,以及执行文件
const childPath = process.env.CHILD_CMD_PATH;
const childCwd = process.env.CHILD_CWD;

process.on('message', data => {
if (data.title === 'server-kill') {
process.emit('SIGINT');
}
});

process.chdir(childCwd);
require(childPath);
13 changes: 13 additions & 0 deletions test/fixtures/custom-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
process.on('SIGINT', () => {
process.send('server-kill-complete');
// process.exit(0);
});

// process.on('SIGTERM', () => {
// process.send('server-kill-complete');
// process.exit(0);
// });

process.send({
title: 'server-ready',
});
22 changes: 19 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const execa = require('execa');
const { join } = require('path');
const { join, resolve } = require('path');
const { existsSync, unlinkSync } = require('fs');
const { forkRun } = require('../lib/process');

const mtscPath = join(__dirname, '../bin/mwtsc.js');

Expand Down Expand Up @@ -63,6 +64,21 @@ describe('/test/index.js', () => {
setTimeout(() => {
cp.kill();
}, 3000);
})
});
});

it('should send server-kill event to child process and receive response', (done) => {
const childProcess = forkRun(resolve(__dirname, './fixtures/custom-event.js'));
childProcess.getRealChild().on('message', (data) => {
if (data === 'server-kill-complete') {
childProcess.kill();
done();
}
});
childProcess.onServerReady(() => {
childProcess.getRealChild().send({
title: 'server-kill',
});
});
});
});
});

0 comments on commit cc30522

Please sign in to comment.