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