Skip to content

Commit

Permalink
cron restarts improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Sep 24, 2021
1 parent 8fec746 commit 120e31a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/API/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@
"docDescription": "Time in ms before forcing a reload if app is still not listening/has still note sent ready"
},
"cron_restart": {
"type": "string",
"type": [
"string",
"number"
],
"alias": "cron",
"docDescription": "A cron pattern to restart your app"
},
Expand Down
5 changes: 5 additions & 0 deletions lib/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ Common.sink = {};
Common.sink.determineCron = function(app) {
var cronJob = require('cron').CronJob;

if (app.cron_restart == 0 || app.cron_restart == '0') {
Common.printOut(cst.PREFIX_MSG + 'disabling cron restart');
return
}

if (app.cron_restart) {
try {
Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart);
Expand Down
1 change: 1 addition & 0 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ module.exports = function(God) {
var proc = God.clusters_db[id];

God.resetState(proc.pm2_env);
God.deleteCron(id);

/**
* Merge new application configuration on restart
Expand Down
1 change: 1 addition & 0 deletions lib/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function(God) {
if (!pm2_env ||
pm2_env.pm_id === undefined ||
!pm2_env.cron_restart ||
pm2_env.cron_restart == '0' ||
God.CronJobs.has(God.getCronID(pm2_env.pm_id)))
return;

Expand Down
17 changes: 17 additions & 0 deletions test/e2e/misc/cron-system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,20 @@ sleep 4
should 'should app been restarted' 'restart_time: 0' 0

$pm2 delete all

#
# Cron every sec
#
$pm2 start cron.js -c "* * * * * *"
sleep 4
should 'should app been restarted' 'restart_time: 0' 0

#
# Delete cron
#
$pm2 restart cron --cron-restart 0
$pm2 reset all
sleep 2
should 'app stop be restarted' 'restart_time: 0' 1

$pm2 delete all

0 comments on commit 120e31a

Please sign in to comment.