NodeJS Server with node-cron not executing script on interval after a while, only console.log

40 views Asked by At

I am developing a Node.js server that utilizes scheduled tasks with the node-cron library to perform certain functions. These functions are defined as asynchronous and involve database operations. The issue I'm facing is that the server initially executes these functions as expected, but after a certain period (between 8 and 10 minutes), the scheduled tasks no longer run.

Here is meine Scheduler Code:

const CronJob = require('node-cron');
const check_expiration_time_jobs = require('../folien_manager/checkExpirationTime').check_expiration_time_jobs;
const check_expiration_time_archives = require('../folien_manager/checkExpirationTime').check_expiration_time_archives;
const archive_job = require('../folien_manager//archiveFolien').archive_job;
const load_folien = require('../folien_manager/loadFolien').load_folien;
const delete_archive = require('../folien_manager/deleteArchive').delete_archive;
const { pool } = require('../../models/db'); // Import your database connection pool

// create task for checking if there is a new dataset added to the database (content)(each hour)
const check_new_content = CronJob.schedule('5 */1 * * * *', () => {
    console.log("\nChecking for new content ...");
    try {
        load_folien();
    } catch (error) {
        console.error('Error checking for new content:', error);
    }
});

// Create task for checking if there is a dataset in the cronjob table that has expired
const check_job_status = CronJob.schedule('10 */1 * * * *', () => {
    console.log("\nChecking for expired Jobs ...");
    try {
        check_expiration_time_jobs();
    } catch (error) {
        console.error('Error checking for expired Jobs:', error);
    }
});

// Create task to archive the dataset with the status times_up
const check_job_to_archive = CronJob.schedule('15 */1 * * * *', () => {
    console.log("\nChecking for jobs to be archived ...");
    try {
        archive_job();
    } catch (error) {
        console.error('Error checking for jobs to be archived:', error);
    }
});

// Create task for checking if there is an archive in the archive table that has expired
const check_archive_status = CronJob.schedule('20 */1 * * * *', () => {
    console.log("\nChecking for expired archives ...");
    try {
        check_expiration_time_archives();
    } catch (error) {
        console.error('Error checking for expired archives:', error);
    }
});

// Create task for checking each 6 months if there is an archive to be deleted
const check_archive_to_delete = CronJob.schedule('25 */1 * * * *', () => {
    console.log("\nChecking for archives to be deleted ...");
    try {
        delete_archive();
    } catch (error) {
        console.error('Error checking for archives to be deleted:', error);
    }
});

// Create a task for printing the actual day and time
exports.initScheduledJobs = () => {
    check_new_content.start();
    check_job_status.start();
    check_job_to_archive.start();
    check_archive_status.start();
    check_archive_to_delete.start();
};

I use the nodes alpine and my container appears to have sufficient resources. I'm not receiving any error messages, so it's challenging to pinpoint the issue.

I already tried using the setIntervall function as change but it still does the same

I've included comprehensive logging for debugging purposes, but I haven't been able to identify the problem. I'm seeking advice on how to diagnose and resolve this issue.

0

There are 0 answers