'Ensure That Node Cron Job is Only Configured Once

I'm writing a Discord bot and having it schedule a cron job when it starts up using node-cron. I've defined it as follows (very basic so far):

    cron.schedule('15 1,3,5,7,9,11,13,15,17,19,21,23 * * *', () => {
        GameUpdater.sendGameUpdatesToLeagueChannels(this.guilds);
    });

The problem is, Discord bots will often reconnect which will rerun the portion of the code where this has to be. I'm considering having a check of an external value, perhaps in my database, that manages whether or not it's run recently or is already running or something like that, but I was wondering if there was a proper way to make a cron job unique using node-cron?



Solution 1:[1]

if the cron schedule is fired on startup from Client.on('ready') have your cronjob to be scheduled at a separate event Client.once('ready'), this way it won't fire the routine again when the bot reconnects. Only if the entire shard/cluster process is terminated and restarted.

Solution 2:[2]

You can you use logger to keep track of restaring or starting time of your server. You can use winston Click HERE

It will help to generate your server log so that you can read it before starting of your server, so that you can take your action. Like say adjust the time etc

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Lucas Flicky
Solution 2 Tanjin Alam