Graceful Stop优雅停服
To allow graceful restart/reload/stop processes, make sure you intercept the SIGINT signal and clear everything needed (like database connections, processing jobs…) before letting your application exit.要允许正常的重新启动/重新加载/停止进程,请确保在让应用程序退出之前拦截SIGINT信号并清除所有需要的内容(如数据库连接、处理作业)。
process.on('SIGINT', function() {
db.stop(function(err) {
process.exit(err ? 1 : 0)
})
})
Now 现在pm2 reload
will become a gracefulReload.pm2 reload
将成为一个优雅的重新加载。
Configure the kill timeout配置终止超时
Via CLI, this will lengthen the timeout to 3000ms:通过CLI,这会将超时延长到3000ms:
pm2 start app.js --kill-timeout 3000
Via application declaration use the 通过应用程序声明使用kill_timeout
attribute:kill_timeout
属性:
module.exports = {
apps : [{
name: 'app',
script: './app.js',
kill_timeout : 3000
}]
}
Graceful start优雅开服
Sometimes you might need to wait for your application to have etablished connections with your DBs/caches/workers/whatever. 有时,您可能需要等待应用程序使用您的DBs/caches/workers/whatever建立eTablied连接。PM2 needs to wait, before considering your application as PM2需要等待,然后才能将您的应用程序视为online
. online
应用程序。To do this, you need to provide 为此,需要在进程文件中为CLI提供--wait-ready
to the CLI or provide wait_ready: true
in a process file. --wait-ready
或提供wait_ready: true
。This will make PM2 listen for that event. 这将使PM2监听该事件。In your application you will need to add 在您的应用程序中,您需要添加process.send('ready')
when you want your application to be considered as ready.process.send('ready')
,当您希望您的应用程序被视为就绪时。
var http = require('http')
var app = http.createServer(function(req, res) {
res.writeHead(200)
res.end('hey')
})
var listener = app.listen(0, function() {
console.log('Listening on port ' + listener.address().port)
// Here we send the ready signal to PM2
process.send('ready')
})
Then start the application:然后启动应用程序:
pm2 start app.js --wait-ready
Configure the ready timeout配置就绪超时
By default, PM2 wait 3000ms for the 默认情况下,PM2等待3000ms等待ready
signal.ready
信号。
Via CLI, this will lengthen the timeout to 10000ms:通过CLI,这会将超时延长到10000ms:
pm2 start app.js --wait-ready --listen-timeout 10000
Via application declaration use the 通过应用程序声明使用listen_timeout
and wait_ready
attribute:listen_timeout
和wait_ready
属性:
module.exports = {
apps : [{
name: 'app',
script: './app.js',
wait_ready: true,
listen_timeout: 10000
}]
}
Graceful start using http.Server.listen使用http.Server.listen优雅开始
There is still the default system that hooks into 仍然存在挂接到http.Server.listen
method. http.Server.listen
方法的默认系统。When your http server accepts a connection, it will automatically state your application as ready. 当您的http服务器接受连接时,它会自动将您的应用程序声明为就绪。You can increase the PM2 waiting time the listen using the same variable as 您可以使用与进程文件中的--wait-ready
graceful start : listen_timeout
entry in process file or --listen-timeout=XXXX
via CLI.--wait-ready
优雅启动:listen_timeout
条目或通过CLI的--listen-timeout=XXXX
相同的变量来增加侦听的PM2等待时间。
Explanation: Signals flow说明:信号流
When a process is stopped/restarted by PM2, some system signals are sent to your process in a given order.当一个进程被PM2停止/重新启动时,一些系统信号会按给定顺序发送到您的进程。
First a SIGINT a signal is sent to your processes, signal you can catch to know that your process is going to be stopped. 首先,一个SIGINT被发送到您的进程,您可以捕捉到这个信号来知道您的进程将被停止。If your application does not exit by itself before 1.6s (customizable) it will receive a SIGKILL signal to force the process exit.如果您的应用程序在1.6s(可自定义)之前未自行退出,它将收到SIGKILL信号以强制进程退出。
The signal SIGINT can be replaced on any other signal (e.g. SIGTERM) by setting environment variable PM2_KILL_SIGNAL.通过设置环境变量PM2_KILL_SIGNAL,可以在任何其他信号(例如SIGTERM
)上替换信号SIGINT
。
Windows graceful stopWindows优雅停服
When signals are not available your process gets killed. 当信号不可用时,进程将被终止。In that case you have to use 在这种情况下,您必须通过CLI使用--shutdown-with-message
via CLI or shutdown_with_message
in Ecosystem File and listen for shutdown
events.--shutdown-with-message
或在生态系统文件中使用shutdown_with_message
并侦听关机事件。
Via CLI:通过CLI:
pm2 start app.js --shutdown-with-message
Via application declaration use the 通过应用程序声明使用listen_timeout
and wait_ready
attribute:listen_timeout
和wait_ready
属性:
module.exports = {
apps : [{
name: 'app',
script: './app.js',
shutdown_with_message: true
}]
}
Listen for 侦听shutdown
eventsshutdown
事件
process.on('message', function(msg) {
if (msg == 'shutdown') {
console.log('Closing all connections...')
setTimeout(function() {
console.log('Finished closing connections')
process.exit(0)
}, 1500)
}
})