cron not running as scheduled

Hi there and thanks in advance for help I've seen various posts regarding this but can't find an answer within them. The most recent run was manual 45 minutes after the scheduled run. Cron is 0 21 * * *, should be running every night at 10pm my local time I had this running perfectly hourly when i was at my PC, but when my PC was shut down overnight the job hadnt run. Any advice on how to ensure the schedule persists and i dont need to keep checking it would be great ProjectID: 1953b972-5402-489d-86ae-ce6957da4da8
No description
30 Replies
Percy
Percy2mo ago
Project ID: 1953b972-5402-489d-86ae-ce6957da4da8
Brody
Brody2mo ago
You mention running hourly but your cron runs daily at 21:00 UTC? can you provide a little more clarity here?
buddyswift
buddyswift2mo ago
sorry i changed the schedule after it was running successfully hourly
Brody
Brody2mo ago
okay and have you had it skip a run at 21:00 UTC?
buddyswift
buddyswift2mo ago
yeah it didnt run last night so i ran it manually 45 minutes late which is why it appears successful ive also ran it manually several times today
Brody
Brody2mo ago
hmmm 21:00 utc should not be a congested time of day
buddyswift
buddyswift2mo ago
i had this exact problem on a previous deployment
Brody
Brody2mo ago
maybe it was a fluke, if it happens once more, report back and I will escalate this to the team
buddyswift
buddyswift2mo ago
i cant find my previous ticket unfortunately, but i expect it isnt a fluke as the behaviour is identical to a 6hourly cron i deployed a few months ago if you're easily able to see my previous tickets you might find a similar ticket it would have been on 5510af11-7ac6-4c46-b964-e91edd728850
Brody
Brody2mo ago
as a community member i have no such access
Brody
Brody2mo ago
are you still trying to run a bot on a cron?
buddyswift
buddyswift2mo ago
yeah i am :catPeek: is that really bad
Brody
Brody2mo ago
why??
buddyswift
buddyswift2mo ago
im not a dev so probably through ignorance it just seemed that i wanted to trigger the bot to run as i would manually at specific times through the day
Brody
Brody2mo ago
bots need to run 24/7 if you want to limit when your bot responds that needs to be done through code, absolutely do not use cron for this
buddyswift
buddyswift2mo ago
ok, can you tldr why that is please? ill look into coding this properly based on the advice given previously, thanks for your time
Brody
Brody2mo ago
bots are long lived processes, what kind of bot is this
buddyswift
buddyswift2mo ago
it posts a random announcement based on the contents of a postgres db
Brody
Brody2mo ago
how is this content put into the database?
buddyswift
buddyswift2mo ago
its static but a js script in github updates that the announcement has been made to avoid duplicate announcements and the js script calls the bot too
Brody
Brody2mo ago
does the bot do its thing and then exit?
buddyswift
buddyswift2mo ago
yes, after completing the posting task, i use client.destroy()
Brody
Brody2mo ago
does it exit though?
buddyswift
buddyswift2mo ago
in discord i see the bot go offline, but im not sure how else i would test what youre asking
Brody
Brody2mo ago
i mean you wouldnt need to test anything, you wrote the code right?
buddyswift
buddyswift2mo ago
well i didnt 'exit' explicitly, but im not sure of the difference between exiting and destroying the discord client and resources client.once('ready', async () => { console.log('Bot is ready!'); await fetchAndPost(); client.destroy(); process.exit(0); }); would this work better? or should i just go back to the drawing board and figure out continuous running of the bot
Brody
Brody2mo ago
i think this would be best for an in code schedular
buddyswift
buddyswift2mo ago
ok brody, thank you implemented this in my code, seems to work great after removing the external cron let lastRunDate = null; client.once('ready', () => { console.log('Bot is ready!'); setInterval(checkAndRun, 60 * 1000); // Check every minute }); client.login(BOT_TOKEN); async function checkAndRun() { const now = new Date(); const ninePM = new Date(now); ninePM.setUTCHours(21, 0, 0, 0); // Set to 9pm UTC if (now > ninePM && (!lastRunDate || lastRunDate.getDate() !== now.getDate())) { lastRunDate = now; await fetchAndPost(); } }
Brody
Brody2mo ago
awesome!