1

I have added two cron jobs to run every 15 mins and every 3 hours during this time I'm unable to access API's. I'm getting a 502 bad gateway errors.

import Agenda from 'agenda';
import { updateCinemaData } from './updateCinemaData';
import { updateStreamingData } from './updateStreamingData';
import dotenv from 'dotenv';

dotenv.config();

const uri = process.env.MONGO_URI;
const MAX_CONCURRENCY_JOBS = 4;

const agenda = new Agenda({
    db: {
        address: uri,
        options: { useNewUrlParser: true },
        collection: 'schedule-jobs'
    },
    processEvery: '30 seconds',
});

const graceful = async () => {
    await agenda.stop();
    process.exit(0);
};

process.on('SIGTERM', graceful);
process.on('SIGINT', graceful);

agenda.define('get cinema data', { priority: 1 }, async (job, done) => {
    console.log('get cinema data')
    await updateCinemaData();
    done();
});

agenda.define('get streaming data', { priority: 2 }, async (job, done) => {
    console.log('get streaming data')
    await updateStreamingData();
    done();
});


(async function () {
    const getCinemaData = agenda.create('get cinema data', {});
    const getStreamingData = agenda.create('get streaming data', {});

    await agenda.start();
    agenda.maxConcurrency(MAX_CONCURRENCY_JOBS);

    const jobNames = [
        'get cinema data',
        'get streaming data'
    ];

    for (let jobName of jobNames) {
        try {
            let jobs = await agenda.jobs({ name: jobName });
            for (let job of jobs) {
                job.remove();
            }
        } catch (error) {
            console.log(error);
        }
    }

    await getCinemaData.repeatEvery('*/15 * * * *').save(); //Runs every 15 mins.
    await getStreamingData.repeatEvery('* */2 * * *').save(); //Runs every 3 hours.
})();

export default agenda;

To catch all the exceptions I have added the below code in my server.js

process.on('uncaughtException', function (exception) {
    log.error(exception); 
});

with this also I'm not able to get the errors and this issue is only on the production. Also checked pm2 logs and Nginx logs but no use. Can anyone explain to me what is the issue I'm facing? I'm a beginner in Nodejs and not able to resolve this.

Megha BM
  • 21
  • 1
  • 4

0 Answers0