Cron Jobs
Hey everyone. I'm moving from Vercel to Railway so this is my first Railway project. I'm trying to implement a replacement to Vercel Cron jobs, ie: I have an API I need to call every so often as part of a timer. This is a NextJS app. Under Project Settings > General there is the "Cron Schedule" section, but as best I can tell that's going to redeploy my app on that schedule, not execute a piece of code on that schedule. Am I missing something silly here? I suppose I could build a completely separate service to run my cron jobs but that seems like overkill.
To the extent that it matters, I haven't upgraded to Pro yet, I was just doing the dev on the Hobby plan (in case I need a feature behind the paywall of Pro for this to work).
25 Replies
Project ID:
c87c74c6-772c-4bd2-9d34-0fc2fc173ea8
c87c74c6-772c-4bd2-9d34-0fc2fc173ea8
you would want to make a new service, set the start command to what you want to run on the schedule and then set the cron expression
gotcha... ok let me play around with that and see if I can get that working
just make sure whatever you run does it's thing and exits as soon as it's done
how does the infrastructure handle if it's still running when the next execution time comes up?
I think it skips it
at least that's what a changelog has said, there has been some debate on that though
I assume I can share environment variables across multiple services in that one project so that I'm not duplicating places where the same keys exist?
nevermind, I see
you can do shared environment variables, or you can do reference variables
I just have to move the variables from the actual service where they live today to the Shared Variables thing
nah I recommend using reference variables
that?
you set this variable on your cron service and when the cron task is ran, the environment variable
VAR_1
will be rendered to the same variable name as the variable it references
VAR_1=${{<service name>.VAR_1}}
and yeah those are the docs for this type of thing, so definitely give them a readawesome, ok thanks!
I'll leave this open for a little bit in case I've got follow up questions, thanks for the push in the right direction!
absolutely! come back if you have any more questions!
I think I have this working, but there's some odd behavior... it appears to run my code only when the cron schedule triggers it...
the 13:45 it triggered, and it "completed"
but if I click Run Now or trigger a new build through Github, it shows "Success" but it doesn't actually run anything
what makes you think it didnt run anything when you trigger it manually?
there's nothing in the logs
it's literally just a one-line script that does a console log... the log message shows up on the 15 min executions, but not if I click Run Now
good news is all the environment variable stuff worked just as you said, super easy
π
yeah, it just ran again at the top of the hour and that completed and logged my message... it's almost like clicking "Run now" does something different than when the cron schedule triggers it.
but at least I've got it working π
okay that seems like a bug, it still may be running it, just logs arent getting piped through
Thread has been flagged to Railway team by @Brody.
@gschier - no logs when triggering a manual run of a cron job
Hmm, that's strange. And it happens every time? I added a task to take a look this week
I just fixed a couple bugs around crons. You may have to issue a redeploy to kickstart it