Skip to content

Commit

Permalink
use croner instead of node-schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Feb 9, 2022
1 parent cbf5c0a commit af3a1f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
5 changes: 2 additions & 3 deletions lib/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,14 @@ Common.retErr = function(e) {
Common.sink = {};

Common.sink.determineCron = function(app) {
const cronParser = require('cron-parser')
const scheduleJob = require('node-schedule').scheduleJob;

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

if (app.cron_restart) {
const cronParser = require('cron-parser')

This comment has been minimized.

Copy link
@Hexagon

Hexagon Feb 14, 2022

@Unitech If you don't need cron-parser anywhere else, and just use it for this sanity check - you can remove the dependency completely and just use Cron(expression); from croner. It will throw on any kind of syntax error. 👍

It will throw Error or TypeError (example at https://github.com/Hexagon/croner/blob/c7ca44a26878157abec91fc7989239c14117382e/src/pattern.js#L234)

This comment has been minimized.

Copy link
@Unitech

Unitech Feb 15, 2022

Author Owner

Great thanks for the information, will do that


try {
Common.printOut(cst.PREFIX_MSG + 'cron restart at ' + app.cron_restart);
cronParser.parseExpression(app.cron_restart);
Expand Down
19 changes: 10 additions & 9 deletions lib/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var vizion = require('vizion');
var cst = require('../constants.js');
var eachLimit = require('async/eachLimit');
var debug = require('debug')('pm2:worker');
var domain = require('domain');
const scheduleJob = require('node-schedule').scheduleJob;
const vizion = require('vizion');
const eachLimit = require('async/eachLimit');
const debug = require('debug')('pm2:worker');
const domain = require('domain');
const Cron = require('croner');
const pkg = require('../package.json');

var cst = require('../constants.js');
var vCheck = require('./VersionCheck.js')
var pkg = require('../package.json')

module.exports = function(God) {
var timer = null;
Expand All @@ -34,7 +35,7 @@ module.exports = function(God) {
var pm_id = pm2_env.pm_id
console.log('[PM2][WORKER] Registering a cron job on:', pm_id);

var job = scheduleJob(pm2_env.cron_restart, function() {
var job = Cron(pm2_env.cron_restart, function() {
God.restartProcessId({id: pm_id}, function(err, data) {
if (err)
console.error(err.stack || err);
Expand All @@ -56,7 +57,7 @@ module.exports = function(God) {
var job = God.CronJobs.get(God.getCronID(id));

if (job)
job.cancel();
job.stop();

God.CronJobs.delete(God.getCronID(id));
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
"chokidar": "^3.5.1",
"cli-tableau": "^2.0.0",
"commander": "2.15.1",
"node-schedule": "^2.0.0",
"croner": "~4.1.92",
"cron-parser": "~3.5.0",
"dayjs": "~1.8.25",
"debug": "^4.3.1",
Expand Down

0 comments on commit af3a1f7

Please sign in to comment.