diff --git a/examples/signals/http.js b/examples/signals/http.js new file mode 100644 index 00000000..371500b5 --- /dev/null +++ b/examples/signals/http.js @@ -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); +}); diff --git a/lib/God/ClusterMode.js b/lib/God/ClusterMode.js index 9ca7f3fb..017b1084 100644 --- a/lib/God/ClusterMode.js +++ b/lib/God/ClusterMode.js @@ -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', { diff --git a/lib/God/ForkMode.js b/lib/God/ForkMode.js index 2a9e52f2..c0fbe987 100644 --- a/lib/God/ForkMode.js +++ b/lib/God/ForkMode.js @@ -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', { diff --git a/test/bash/cli-actions-1.sh b/test/bash/cli-actions-1.sh index 6e0b1b71..5bebceb2 100644 --- a/test/bash/cli-actions-1.sh +++ b/test/bash/cli-actions-1.sh @@ -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" diff --git a/test/bash/cron-system.sh b/test/bash/cron-system.sh new file mode 100644 index 00000000..b94d28d2 --- /dev/null +++ b/test/bash/cron-system.sh @@ -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 diff --git a/test/fixtures/cron/mock-cron-no-exit.js b/test/fixtures/cron/mock-cron-no-exit.js new file mode 100644 index 00000000..00ec3cec --- /dev/null +++ b/test/fixtures/cron/mock-cron-no-exit.js @@ -0,0 +1,14 @@ + + +setTimeout(function() { + process.send({ + 'cron_restart' : 1 + }); +}, 1000); + +process.on('SIGINT', function() { + console.log('SIGINT signal received'); +}); + +setInterval(function() { +}, 100); diff --git a/test/fixtures/cron/mock-cron.js b/test/fixtures/cron/mock-cron.js new file mode 100644 index 00000000..a9f19a77 --- /dev/null +++ b/test/fixtures/cron/mock-cron.js @@ -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); diff --git a/test/pm2_behavior_tests.sh b/test/pm2_behavior_tests.sh index 3ce906df..cce383a8 100644 --- a/test/pm2_behavior_tests.sh +++ b/test/pm2_behavior_tests.sh @@ -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