Search code examples
crondiscord.js

Cron Job stops running for no reason


The below cron job is set to run every min updating timers and according to the log just stops when someone joins the server. It does not throw any error codes. The bot doesn't crash. I'm hoping someone can tell me why its doing that.

Here is the cron job:

cron.schedule('* * * * *', function () {
    bot.channels.cache.filter((channel) => channel.name.endsWith('events')).forEach((channel) => {
        channel.messages.fetch({
            limit: 100
        }).then(messages => {
            messages.forEach(message => {
                if (message.embeds.length > 0) {
                    const embed = message.embeds[0]

                    let messageID;
                    let event_date;
                    let event_time;

                    if (embed.color === 65280) {
                        messageID = message.id
                        event_date = message.embeds[0].fields[0].value
                        event_time = message.embeds[0].fields[1].value.slice(0, -4)

                        let now = Date.now()
                        const targetDate = new Date(`${event_date} ${event_time}:00`)
                        var difference = (targetDate - now) / 1000;
                        var days = Math.floor(difference / (3600 * 24));
                        var hours = Math.floor((difference - (days * (3600 * 24))) / 3600);
                        var minutes = Math.floor((difference - (days * (3600 * 24)) - (hours * 3600)) / 60);

                        let timeTill;

                        if (days > 0) {
                            timeTill = `${days} Days ${hours} Hours ${minutes} Min`
                        } else if (days === 0 && hours > 0) {
                            timeTill = `${hours} Hours ${minutes} Min`
                        } else if (days === 0 && hours === 0) {
                            timeTill = `${minutes} Min`
                        } else if (difference <= 0) {
                            timeTill = `Event Started - get with <@!${row.hostID}> to join up in-game.`
                        }

                        embed.fields[9] = {
                            name: 'Starts in:',
                            value: `${timeTill}`,
                            inline: false
                        }
                        message.edit(new Discord.MessageEmbed(embed)).catch(error => {
                            console.error(error);
                        })
                        console.log(`Event ${message.embeds[0].author.name} has been edited. New start time is ${timeTill}.`)
                    } else {
                        return
                    }
                } else {
                    return
                }
            })
        })
    })
})

Here is a log of it stopping when someone joins

[July 17 2021 01:38:01] Event 6 has been edited. New start time is 16 Hours 21 Min.
[July 17 2021 01:39:00] Event 6 has been edited. New start time is 16 Hours 20 Min.
[July 17 2021 01:40:00] Event 6 has been edited. New start time is 16 Hours 19 Min.
[July 17 2021 01:41:00] Event 6 has been edited. New start time is 16 Hours 18 Min.
[July 17 2021 01:41:14] The code tj25ttthFA was just used by Thx Meat Cat.
[July 17 2021 01:41:14] User Thx Meat Cat has joined the server! There are now 49 members in the community.
[July 17 2021 01:46:07] User Thx Meat Cat has left the server or was kicked!
[July 17 2021 01:46:07] There are now 48 members in the community.
[July 17 2021 01:49:22] The code tj25ttthFA was just used by SlayerZXY96.

and again here

[July 17 2021 00:03:01] Event 6 has been edited. New start time is 17 Hours 56 Min.
[July 17 2021 00:04:01] Event 6 has been edited. New start time is 17 Hours 55 Min.
[July 17 2021 00:05:00] Event 6 has been edited. New start time is 17 Hours 54 Min.
[July 17 2021 00:06:00] Event 6 has been edited. New start time is 17 Hours 53 Min.
[July 17 2021 00:06:50] The code tj25ttthFA was just used by 『Tã€ã€Žiã€ã€Žnã€ã€Žyã€.
[July 17 2021 00:06:50] User 『Tã€ã€Žiã€ã€Žnã€ã€Žy〠has joined the server! There are now 48 members in the community.
[July 17 2021 00:07:53] User 『Tã€ã€Žiã€ã€Žnã€ã€Žy〠has left the server or was kicked!
[July 17 2021 00:07:53] There are now 47 members in the community.
[July 17 2021 00:14:03] The code tj25ttthFA was just used by ChAoS.

EDIT

Following the suggestion below seemed to work, will test for 24 hours.

[July 19 2021 00:57:41] Event 8 has been edited. New start time is 65 Days 17 Ho                       urs 2 Min.
[July 19 2021 00:57:47] The code tj25ttthFA was just used by ChAoS.
[July 19 2021 00:57:47] User ChAoS has joined the server! There are now 73 membe                       rs in the community.
[July 19 2021 00:58:41] Event 8 has been edited. New start time is 65 Days 17 Ho                       urs 1 Min.
[July 19 2021 00:58:55] User ChAoS has left the server or was kicked!
[July 19 2021 00:58:55] There are now 72 members in the community.
[July 19 2021 00:59:41] Event 8 has been edited. New start time is 65 Days 17 Ho                       urs 0 Min.
[July 19 2021 00:59:46] The code tj25ttthFA was just used by ChAoS.
[July 19 2021 00:59:46] User ChAoS has joined the server! There are now 73 membe                       rs in the community.
[July 19 2021 01:00:41] Event 8 has been edited. New start time is 65 Days 16 Hours 59 Min.

Solution

  • Not sure why, but the comment above solved the issue.

    "You could try to replace cron.schedule() with setInterval(). That way you know if it is an issue with cron. – Skulaurun Mrusal"