(cron) #2395 harden cron external restart

This commit is contained in:
Unitech 2016-09-24 16:37:59 +02:00
parent e60fd77003
commit e62c439caa
8 changed files with 115 additions and 12 deletions

14
examples/signals/http.js Normal file
View File

@ -0,0 +1,14 @@
process.on('SIGINT', function() {
console.log('Exiting');
process.exit(0);
});
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('hey');
}).listen(process.env.PORT || 8000, function() {
console.log('App listening on port %d in env %s', process.env.PORT || 8000, process.env.NODE_ENV);
});

View File

@ -82,8 +82,11 @@ module.exports = function ClusterMode(God) {
clu.pm2_env.node_version = msg.node_version;
return false;
} else if (typeof msg == 'object' && 'cron_restart' in msg) {
// cron onTick is invoked in the process
clu.kill('SIGINT');
return God.restartProcessId({
id : clu.pm2_env.pm_id
}, function() {
console.log('Application %s has been restarted via CRON', clu.pm2_env.name);
});
}
return God.bus.emit('process:msg', {

View File

@ -175,7 +175,11 @@ module.exports = function ForkMode(God) {
return false;
} else if (typeof msg == 'object' && 'cron_restart' in msg) {
// cron onTick is invoked in the process
cspr.kill('SIGINT');
return God.restartProcessId({
id : cspr.pm2_env.pm_id
}, function() {
console.log('Application %s has been restarted via CRON', cspr.pm2_env.name);
});
}
return God.bus.emit('process:msg', {

View File

@ -247,15 +247,6 @@ spec "Should delete all processes"
sleep 0.5
should 'should have deleted process' 'restart_time' 0
#
# Cron
#
$pm2 start cron.js -c "* * * asdasd"
ispec "Cron should throw error when pattern invalid"
$pm2 start cron.js -c "* * * * * *"
spec "Should cron restart echo.js"
$pm2 kill test
ispec "Should not kill with extra args"

58
test/bash/cron-system.sh Normal file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env bash
SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/include.sh"
cd $file_path
#
# Cron
#
$pm2 start cron.js -c "* * * asdasd"
ispec "Cron should throw error when pattern invalid"
$pm2 start cron.js -c "* * * * * *"
spec "Should cron restart echo.js"
$pm2 delete all
> mock-0.log
$pm2 start cron/mock-cron.js -o mock.log
sleep 3
should 'should app been restarted when cron in fork mode' 'restart_time: 0' 0
cat mock-0.log | grep "SIGINT"
spec "Should cron exit call SIGINT handler"
$pm2 delete all
$pm2 start cron/mock-cron.js -o mock.log -i 1
sleep 3
should 'should app been restarted when cron in cluster mode' 'restart_time: 0' 0
cat mock-0.log | grep "SIGINT"
spec "Should cron exit call SIGINT handler"
$pm2 delete all
## No exit
$pm2 start cron/mock-cron-no-exit.js -o mock.log
sleep 3
should 'should app been restarted' 'restart_time: 0' 0
cat mock-0.log | grep "SIGINT"
spec "Should cron exit call SIGINT handler"
exit
#
# Slow test
#
$pm2 start signals/delayed_sigint.js -c "1 * * * * *" -o cron.log
spec "Should cron restart delayed sigint"
sleep 100
cat cron-0.log | grep "SIGINT cb called"
spec "Should cron exit call SIGINT handler"
should 'should app been restarted' 'restart_time: 1' 1

14
test/fixtures/cron/mock-cron-no-exit.js vendored Normal file
View File

@ -0,0 +1,14 @@
setTimeout(function() {
process.send({
'cron_restart' : 1
});
}, 1000);
process.on('SIGINT', function() {
console.log('SIGINT signal received');
});
setInterval(function() {
}, 100);

16
test/fixtures/cron/mock-cron.js vendored Normal file
View File

@ -0,0 +1,16 @@
setTimeout(function() {
process.send({
'cron_restart' : 1
});
}, 1000);
process.on('SIGINT', function() {
console.log('SIGINT signal received');
setTimeout(function() {
process.exit(0);
}, 1000);
});
setInterval(function() {
}, 100);

View File

@ -23,6 +23,9 @@ spec "pm2-dev"
bash ./test/bash/docker.sh
spec "Docker tests"
bash ./test/bash/cron-system.sh
spec "Cron system tests"
# bash ./test/bash/log-timestamp.sh
# spec "timestamp prefix of pm2.log"
bash ./test/bash/watch.sh