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
Percy7mo ago
Project ID: 1953b972-5402-489d-86ae-ce6957da4da8
Brody
Brody7mo ago
You mention running hourly but your cron runs daily at 21:00 UTC? can you provide a little more clarity here?
buddyswift
buddyswift7mo ago
sorry i changed the schedule after it was running successfully hourly
Brody
Brody7mo ago
okay and have you had it skip a run at 21:00 UTC?
buddyswift
buddyswift7mo 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
Brody7mo ago
hmmm 21:00 utc should not be a congested time of day
buddyswift
buddyswift7mo ago
i had this exact problem on a previous deployment
Brody
Brody7mo ago
maybe it was a fluke, if it happens once more, report back and I will escalate this to the team
buddyswift
buddyswift7mo 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
Brody7mo ago
as a community member i have no such access
Brody
Brody7mo ago
are you still trying to run a bot on a cron?
buddyswift
buddyswift7mo ago
yeah i am :catPeek: is that really bad
Brody
Brody7mo ago
why??
buddyswift
buddyswift7mo 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
Brody7mo 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
buddyswift7mo 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
Brody7mo ago
bots are long lived processes, what kind of bot is this
buddyswift
buddyswift7mo ago
it posts a random announcement based on the contents of a postgres db
Brody
Brody7mo ago
how is this content put into the database?
buddyswift
buddyswift7mo 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
Brody7mo ago
does the bot do its thing and then exit?
buddyswift
buddyswift7mo ago
yes, after completing the posting task, i use client.destroy()
Brody
Brody7mo ago
does it exit though?
buddyswift
buddyswift7mo ago
in discord i see the bot go offline, but im not sure how else i would test what youre asking
Brody
Brody7mo ago
i mean you wouldnt need to test anything, you wrote the code right?
buddyswift
buddyswift7mo 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
Brody7mo ago
i think this would be best for an in code schedular
buddyswift
buddyswift7mo 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
Brody7mo ago
awesome!
Want results from more Discord servers?
Add your server