Railway cron schedule skipped

Project ID: 5510af11-7ac6-4c46-b964-e91edd728850 This morning my cron schedule didnt run at 10 as planned (see attached) The logs show the below message and the cron job is shown as "active". The error message is typical and doesnt normally stop the git repo from running. Could anyone suggest why the code didnt execute? Quite new to this
No description
No description
16 Replies
Percy
Percy8mo ago
Project ID: 5510af11-7ac6-4c46-b964-e91edd728850
buddyswift
buddyswift8mo ago
No description
buddyswift
buddyswift8mo ago
update: this cron task just completed but it took 3 hours to run instead of 30 seconds. any ideas why this could happen?
buddyswift
buddyswift8mo ago
No description
Brody
Brody8mo ago
you're running a bot in a cron schedule?
buddyswift
buddyswift8mo ago
Yeah it’s just a custom discord bot that posts some information at fixed intervals
Floris
Floris8mo ago
bots are usually made to run in a constant event loop, if you do insist on closing it each time over just an internal cron in your code make sure you close your discord event loop i use this myself py to cron every day at 6 AM, be wary of railways timezone in UTC, if you are on a different timezone it will cron on its correct time but incorrect for your timezone
import asyncio
from datetime import datetime, timedelta

async def run_cron_scheduler():
print("cron scheduler started")

target_time = datetime.now().replace(hour=6, minute=0)
if target_time < datetime.now():
target_time += timedelta(days=1)
print(target_time)

while True:
now = datetime.now()

if did_event_happen(True):
# you can asyncio sleep for your interval to avoid busy waiting
# i use this to cache the last unix run time to verify if my cron is behaving
# correctly to error handle if it is not
continue

if target_time <= now:

do_something() # call your function here

did_event_happen(False) # i mark here that the event has happened

if now >= target_time:
target_time += timedelta(days=1)
print(target_time)

await asyncio.sleep((target_time - now).seconds)
import asyncio
from datetime import datetime, timedelta

async def run_cron_scheduler():
print("cron scheduler started")

target_time = datetime.now().replace(hour=6, minute=0)
if target_time < datetime.now():
target_time += timedelta(days=1)
print(target_time)

while True:
now = datetime.now()

if did_event_happen(True):
# you can asyncio sleep for your interval to avoid busy waiting
# i use this to cache the last unix run time to verify if my cron is behaving
# correctly to error handle if it is not
continue

if target_time <= now:

do_something() # call your function here

did_event_happen(False) # i mark here that the event has happened

if now >= target_time:
target_time += timedelta(days=1)
print(target_time)

await asyncio.sleep((target_time - now).seconds)
oh i see you're on .js, my bad if you want me to write a nodejs version let me know but its pretty similair
buddyswift
buddyswift8mo ago
@Morpheus thanks for taking the time bud i've attached my git code which seems to run pretty well when its triggered within railway and im using Railways native cron scheduler to execute the js from my github repo what i dont understand is what triggers the code to run, is it permanently active once deployed? i believed i needed to use an external scheduler to run the script. very new to this as you can tell and first time coding in js
buddyswift
buddyswift8mo ago
this is the cron schedule in railway
No description
buddyswift
buddyswift8mo ago
sorry for all the debugging being included, the js is much simpler than it appears
Brody
Brody8mo ago
can you tell us why you are running your bot at those specfic times and not 24/7?
buddyswift
buddyswift8mo ago
The bot is hosting a competition where new challenges need to be posted at discrete intervals. I wasn’t aware of how to keep the script running 24/7 so opted to schedule it to run every 6 hours posting some corresponding data from a postgres db If it was running 24/7, what would the trigger be to post at the correct times?
Floris
Floris8mo ago
I have sent you The code in Py of how i handle this In simple english, you initialize an event loop that just runs infinitely and you define 4 CRON times now in this event loop you just constantly compare the current time with one of the 4 CRON times, when your CRON is triggered you can cache or save the time of triggering somewhere so now in the beginning of your script you just check if the last run time was <24 hrs ago to avoid firing the CRON multiple times and you just do this for all 4 of your times im sure NodeJs has libraries for this too but this is a high level overview .
buddyswift
buddyswift8mo ago
Thanks dude
Floris
Floris8mo ago
if you need help with the code in nodejs let me know ill code it for u
buddyswift
buddyswift8mo ago
It’s good practice for me in js