R
Railwayβ€’9mo ago
n131mg

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
Percy
Percyβ€’9mo ago
Project ID: c87c74c6-772c-4bd2-9d34-0fc2fc173ea8
n131mg
n131mgβ€’9mo ago
c87c74c6-772c-4bd2-9d34-0fc2fc173ea8
Brody
Brodyβ€’9mo ago
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
n131mg
n131mgβ€’9mo ago
gotcha... ok let me play around with that and see if I can get that working
Brody
Brodyβ€’9mo ago
just make sure whatever you run does it's thing and exits as soon as it's done
n131mg
n131mgβ€’9mo ago
how does the infrastructure handle if it's still running when the next execution time comes up?
Brody
Brodyβ€’9mo ago
I think it skips it at least that's what a changelog has said, there has been some debate on that though
n131mg
n131mgβ€’9mo ago
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
Brody
Brodyβ€’9mo ago
you can do shared environment variables, or you can do reference variables
n131mg
n131mgβ€’9mo ago
I just have to move the variables from the actual service where they live today to the Shared Variables thing
Brody
Brodyβ€’9mo ago
nah I recommend using reference variables
n131mg
n131mgβ€’9mo ago
that?
Brody
Brodyβ€’9mo ago
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 read
n131mg
n131mgβ€’9mo ago
awesome, 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!
Brody
Brodyβ€’9mo ago
absolutely! come back if you have any more questions!
n131mg
n131mgβ€’9mo ago
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...
n131mg
n131mgβ€’9mo ago
No description
n131mg
n131mgβ€’9mo ago
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
Brody
Brodyβ€’9mo ago
what makes you think it didnt run anything when you trigger it manually?
n131mg
n131mgβ€’9mo ago
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 πŸ™‚
Brody
Brodyβ€’9mo ago
okay that seems like a bug, it still may be running it, just logs arent getting piped through
Duchess
Duchessβ€’9mo ago
Thread has been flagged to Railway team by @Brody.
Brody
Brodyβ€’9mo ago
@gschier - no logs when triggering a manual run of a cron job
Greg Schier
Greg Schierβ€’9mo ago
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