【发布时间】:2021-01-30 06:24:43
【问题描述】:
我正在用 typescript 编写电子应用测试。
在应用程序内部有 SIGTERM 的注册监听器
process.on('SIGTERM', async () => {
console.log('before exit');
await this.exit(); //some inner function can't reach this statement anyway
});
本地一切正常,但在 CI 上,当应用程序在 docker 容器内运行时,它看起来没有收到 SIGTERM。
对于启动应用程序,我使用的是child_process.spawn
import type { ChildProcess } from 'child_process';
let yarnStart: ChildProcess = spawn('yarn', 'start', { shell: true });
// 'start' is just script in package.json
我尝试以三种不同的方式杀死应用程序,但它们都不起作用。应用程序没有收到 SIGTERM no before exit 并且在最后一步手动停止 ci-build 后 ps aux 显示我的过程。
// 1-way
yarnStart.kill('SIGTERM');
// 2-way
process.kill(yarnStart.pid, 'SIGTERM');
// 3-way
import { execSync } from 'child_process';
execSync(`kill -15 ${yarnStart.pid}`);
为什么 nodejs 不能在 docker 容器内正确发送SIGTERM?
唯一的区别 - 在本地我有 debian-9(stretch) 和基于 debian-10(buster) 的图像。相同的 nodejs 版本 12.14.1。我将尝试构建具有拉伸功能的容器,看看它会如何表现,但我怀疑这是否会有所帮助。
UPD
进程启动存在一定差异(由于在容器中的 CI 上运行脚本,任何指令都以/bin/sh -c 运行)
当你执行ps aux你会看到
//locally
myuser 101457 1.3 0.1 883544 58968 pts/8 Sl+ 10:32 0:00 /usr/bin/node /usr/share/yarn/bin/yarn.js start
myuser 101468 1.6 0.2 829316 69456 pts/8 Sl+ 10:32 0:00 /usr/bin/node /usr/share/yarn/lib/cli.js start
myuser 101479 1.6 0.2 829576 69296 pts/8 Sl+ 10:32 0:00 /usr/bin/node /usr/share/yarn/lib/cli.js start:debug
myuser 101490 0.2 0.0 564292 31140 pts/8 Sl+ 10:32 0:00 /usr/bin/node /home/myuser/myrepo/electron-app/node_modules/.bin/electron -r ts-node/register ./src/main.ts
myuser 101497 143 1.4 9215596 485132 pts/8 Sl+ 10:32 0:35 /home/myuser/myrepo/node_modules/electron/dist/electron -r ts-node/register ./src/main.ts
//container
root 495 0.0 0.0 2392 776 ? S 09:05 0:00 /bin/sh -c yarn start
root 496 1.0 0.2 893240 74336 ? Sl 09:05 0:00 /usr/local/bin/node /opt/yarn-v1.22.5/bin/yarn.js start
root 507 1.7 0.2 885588 68652 ? Sl 09:05 0:00 /usr/local/bin/node /opt/yarn-v1.22.5/lib/cli.js start
root 518 0.0 0.0 2396 712 ? S 09:05 0:00 /bin/sh -c yarn start:debug
root 519 1.7 0.2 885336 68608 ? Sl 09:05 0:00 /usr/local/bin/node /opt/yarn-v1.22.5/lib/cli.js start:debug
root 530 0.0 0.0 2396 780 ? S 09:05 0:00 /bin/sh -c electron -r ts-node/register ./src/main.ts
root 531 0.3 0.0 554764 32080 ? Sl 09:05 0:00 /usr/local/bin/node /opt/ci/jobfolder/job_id_423/electron-app/node_modules/.bin/electron -r ts-node/register ./src/main.ts
root 538 140 1.5 9072388 520824 ? Sl 09:05 0:26 /opt/ci/jobfolder/job_id_423/node_modules/electron/dist/electron -r ts-node/register ./src/main.ts
实际上杀死进程
// 1-way
yarnStart.kill('SIGTERM');
有效,但它只会杀死 /bin/sh -c yarn start 和他的 child_process /usr/local/bin/node /opt/yarn-v1.22.5/bin/yarn.js start 实际产生的应用程序仍然挂起
【问题讨论】:
标签: node.js docker process child-process sigterm