72·最佳实践进阶

优雅关闭

优雅关闭

核心概念

# Docker 发送 SIGTERM
# 等待 10 秒
# 发送 SIGKILL

应用处理信号

// Node.js
process.on('SIGTERM', () => {
    console.log('Received SIGTERM, shutting down...');
    server.close(() => {
        process.exit(0);
    });
});
# Python
import signal
import sys

def handler(signum, frame):
    print('Received SIGTERM')
    sys.exit(0)

signal.signal(signal.SIGTERM, handler)

设置超时

# 等待 30 秒再 SIGKILL
docker stop -t 30 my-app

小结

概念说明
SIGTERM优雅关闭信号
SIGKILL强制关闭
超时默认 10 秒
应用需处理信号完成正在处理的请求

练习编辑器

bash
Loading...