[英]Capturing docker-compose shutdown events in node.js
我一直在嘗試在我的 node.js 應用程序中捕獲關閉命令,以便在終止應用程序之前整理連接。
在生產中,我的應用程序使用 docker swarm,我可以看到 SIGTERM 通過我的:
process.on('SIGTERM', function() {});
但是,在我的開發環境中,我正在使用docker-compose run
和docker-compose down
,這似乎不會觸發我的SIG*捕獲(我有一個本地版本的 rabbitMQ 正在運行, SIG事件也在關閉之前發布)。
我正在我的 Dockerfile 和我的 docker-compose 文件中運行一個ubuntu:bionic圖像,我有init: true 。 我的 Docker 引擎是18.09.2 ,compose 是1.23.2版本:“3.7”在我的 compose 文件頂部。
這些是我在 index.js 中的所有捕獲事件:
process.on('exit', () => {
reporter.ossEvent('APP STOPPED exit');
shutdown();
});
process.on('SIGINT', () => {
reporter.ossEvent('APP STOPPED SIGINT');
shutdown();
});
process.on('SIGTERM', function() {
reporter.ossEvent('APP STOPPED SIGTERM');
shutdown();
});
process.on('SIGUSR1', () => {
reporter.ossEvent('APP STOPPED SIGUSR2');
shutdown();
});
process.on('SIGUSR2', () => {
reporter.ossEvent('APP STOPPED SIGUSR2');
shutdown();
})
#!/bin/bash
set -e
_kill_procs() {
kill -TERM $node
}
# Relay quit commands to processes
trap _kill_procs SIGTERM SIGINT
dumb-init -- node ./build/index.js $@ &
node=$!
#sleep infinity
echo 'Starting node process now'
wait $node
上面這段代碼是監聽SIGTERM AND SIGKILL並殺死節點
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.