node-cron icon indicating copy to clipboard operation
node-cron copied to clipboard

I am trying to setup a daily every hour cron in node using node-cron, node-schedule, cron. But it keeps hanging and doesn't seem to be working properly

Open nehasinghal3107 opened this issue 5 years ago • 3 comments

nehasinghal3107 avatar Aug 05 '20 17:08 nehasinghal3107

Can you provide some info like the syntax you're using or a code sample?

ncb000gt avatar Aug 05 '20 18:08 ncb000gt

Sure, PFB the syntax below

// jobs.js 'use strict'; const cron = require('node-cron'); const fth_api = require('../apis/fth_controller.js'); const tc_api = require('../apis/tc_controller.js'); const zf_api = require('../apis/zf_controller.js'); const ns_api = require('../apis/ns_controller.js'); const cronJob = require('cron').CronJob;

exports.start_fth_scrapper = async function(req, res) {

console.log(' FTH report cron will start at 03:32 AM  GMT / 09:02 AM Local every day');

const job = new cronJob('0 3 */1 * * *', function() {
  fth_api.run_fth_scrapper(); 
}, null, true);

job.start();

}

exports.start_tc_scrapper = async function(req, res) { console.log(' TC report cron will start at 02:32 AM GMT / 08:02 AM Local every day');

const job = new cronJob('0 2 */1 * * *', function() {
  tc_api.run_tc_scrapper();
}, null, true);

job.start();

}

exports.start_zf_scrapper = async function(req, res) { console.log(' ZF report cron will start at 02:32 AM GMT / 08:02 AM Local every day');

const job = new cronJob('0 4 */1 * * *', function() {
  zf_api.run_zf_scrapper(); 
}, null, true);

job.start();

}

exports.start_ns_scrapper = async function(req, res) { console.log(' Nandus report cron will start at 02:32 AM GMT / 08:02 AM Local every day');

const job = new cronJob('0 5 */1 * * *', function() {
  ns_api.run_ns_scrapper(); 
}, null, true);

job.start();

}

// server.js

'use strict';

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

const express = require('express'), app = express(), port = process.env.PORT || 3000;

const http = require('http'); const bodyParser = require('body-parser'); const cookieParser = require('cookie-parser'); const errorHandler = require('errorhandler'); const debug = require('debug')('app'); const path = require('path'); const env = app.get('env'); const jobs = require('./jobs/job.js');

app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(cookieParser());

const server = http.createServer(app);

server.listen(port, '0.0.0.0', () => { console.log(Server running); });

const today = new Date();

jobs.start_fth_scrapper(); jobs.start_tc_scrapper(); jobs.start_zf_scrapper(); jobs.start_ns_scrapper();

exports = module.exports = app;

nehasinghal3107 avatar Aug 05 '20 19:08 nehasinghal3107

Also, my requirement is such that my cron should run every hour, even if my local server is up and running I have to again n again restart the server just before 2-3 minutes of cron execution timing, otherwise it hangs. Can't see any issues/errors though.

Have googled online and found many people facing similar issues with both node-cron and node-schedule. I really appreciate if you can help me with a fix.

nehasinghal3107 avatar Aug 05 '20 19:08 nehasinghal3107