'Crone Task runs multiple times in a single interval NodeJS

I have the problem that in the server the Crone Task is running X4 number of times for each scheduled interval, not only the logs are shown x4 but also the API is called 4 times.

    const taskCron = nodeCron.schedule('*/1 * * * *', function () {
    const timeZone = moment.tz('America/Mexico_City').format();
    const currentDate = splitDate(timeZone);
    const currentTime = splitTime(timeZone);

    console.info(`EXECUTING CRON TASK ${currentDate} ${currentTime}`);
    campaign.find(
            { hour_to_send: currentTime, date_to_send: { $gte: currentDate, $lte: currentDate}}
    ).exec().then(data => {
        console.log('number of campaigns programmed: ' + data.length);

        const campaignsMapped = data.map(campaign => {
            return {
                id: campaign._id
            };
        });

        const secretBulk = 'secret-bulk';
        const bodyRequest = {
            campaignsList: campaignsMapped,
            key: secretBulk
        };

        if (campaignsMapped.length > 0) {
            axios.post(url_bulk, bodyRequest, {})
            .then(data => {
                console.info(data.status);
            })
            .catch(error => {
                console.error(error.code);
            });
        }

    }).catch(error => {console.log(error)});
    function splitDate(date) {
        return date.slice(0, 10);
    }
    function splitTime(date) {
        return date.slice(11, 16) + ':00';
    }
}, {scheduled: false});

taskCron.start();

enter image description here

Use the library node-cron npm

The server is derived from centos Oracle version, the program runs in a docker container official Node JS image version 10, docker Docker version 19.03.11, build 42e35e61f3, It is worth mentioning that on my local machine with windows 10 this behavior does not happen.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source