From dc19d89efc270a5de3a2fd7eecc96b64d9f1f3fd Mon Sep 17 00:00:00 2001 From: Marco Giovannini Date: Wed, 31 Jul 2024 19:35:46 +0200 Subject: [PATCH 1/3] Add optional cron_timezone to be used together with cron_restart --- lib/API/UX/pm2-describe.js | 3 +++ lib/API/schema.json | 4 ++++ lib/Common.js | 3 ++- lib/Worker.js | 7 ++++++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/API/UX/pm2-describe.js b/lib/API/UX/pm2-describe.js index 3c3c5b68e..9b5d07f8a 100644 --- a/lib/API/UX/pm2-describe.js +++ b/lib/API/UX/pm2-describe.js @@ -80,6 +80,9 @@ module.exports = function(proc) { if ('cron_restart' in pm2_env){ table.splice(5, 0, {'cron restart': pm2_env.cron_restart}) + if ('cron_timezone' in pm2_env){ + table.splice(5, 0, {'cron timezone': pm2_env.cron_timezone}) + } } console.log(table.toString()) diff --git a/lib/API/schema.json b/lib/API/schema.json index 7debd78d0..7a27b0842 100644 --- a/lib/API/schema.json +++ b/lib/API/schema.json @@ -175,6 +175,10 @@ "alias": "cron", "docDescription": "A cron pattern to restart your app" }, + "cron_timezone": { + "type": "string", + "docDescription": "Timezone in Europe/Stockholm format" + }, "merge_logs": { "type": "boolean", "alias" : "combine_logs", diff --git a/lib/Common.js b/lib/Common.js index 3123252e5..e824d95ba 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -352,7 +352,8 @@ Common.sink.determineCron = function(app) { const Croner = require('croner'); try { - Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart); + Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart + + (app.cron_timezone ? app.cron_timezone : "default timezone")); Croner(app.cron_restart); } catch(ex) { return new Error(`Cron pattern error: ${ex.message}`); diff --git a/lib/Worker.js b/lib/Worker.js index 265fc6892..90a29be7d 100644 --- a/lib/Worker.js +++ b/lib/Worker.js @@ -35,7 +35,12 @@ module.exports = function(God) { var pm_id = pm2_env.pm_id console.log('[PM2][WORKER] Registering a cron job on:', pm_id); - var job = Cron(pm2_env.cron_restart, function() { + let croner_options; + if (pm2_env.cron_timezone) { + croner_options = { timezone: pm2_env.cron_timezone }; + } + + var job = Cron(pm2_env.cron_restart, croner_options, function() { God.restartProcessId({id: pm_id}, function(err, data) { if (err) console.error(err.stack || err); From b05f61aff4733b08adc70a4cf19559863a054612 Mon Sep 17 00:00:00 2001 From: Marco Giovannini Date: Wed, 31 Jul 2024 20:20:32 +0200 Subject: [PATCH 2/3] improve cron_timezone logs --- lib/API/UX/pm2-describe.js | 2 +- lib/Common.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/API/UX/pm2-describe.js b/lib/API/UX/pm2-describe.js index 9b5d07f8a..9b473ea14 100644 --- a/lib/API/UX/pm2-describe.js +++ b/lib/API/UX/pm2-describe.js @@ -79,10 +79,10 @@ module.exports = function(proc) { } if ('cron_restart' in pm2_env){ - table.splice(5, 0, {'cron restart': pm2_env.cron_restart}) if ('cron_timezone' in pm2_env){ table.splice(5, 0, {'cron timezone': pm2_env.cron_timezone}) } + table.splice(5, 0, {'cron restart': pm2_env.cron_restart}) } console.log(table.toString()) diff --git a/lib/Common.js b/lib/Common.js index e824d95ba..066ff6caf 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -352,7 +352,7 @@ Common.sink.determineCron = function(app) { const Croner = require('croner'); try { - Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart + + Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart + " in " + (app.cron_timezone ? app.cron_timezone : "default timezone")); Croner(app.cron_restart); } catch(ex) { From 87daa4e40f06a3e9768f68162ba6ed4a8c6fa596 Mon Sep 17 00:00:00 2001 From: Marco Giovannini Date: Wed, 31 Jul 2024 20:32:50 +0200 Subject: [PATCH 3/3] pass cron_timezone to croner when validating the cron expression to check if it's valid --- lib/Common.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Common.js b/lib/Common.js index 066ff6caf..e20a52873 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -354,7 +354,13 @@ Common.sink.determineCron = function(app) { try { Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart + " in " + (app.cron_timezone ? app.cron_timezone : "default timezone")); - Croner(app.cron_restart); + + let croner_options; + if (app.cron_timezone) { + croner_options = { timezone: app.cron_timezone }; + } + + Croner(app.cron_restart, croner_options); } catch(ex) { return new Error(`Cron pattern error: ${ex.message}`); }