Execute a cron job every Sunday
tarkbyte
FREEOP

2 years ago

Hello, I need to execute a cron job for my app which will modify something in the DB, I didn't find any examples or documentation regarding this.

I am using Next.js, Postgress and DrizzleORM

Any ideas?

37 Replies

tarkbyte
FREEOP

2 years ago

N/A


2 years ago

do you already have a script to do your desired task?


tarkbyte
FREEOP

2 years ago

Do I need to create a new project and deploy it? Can I use my existing Next.js app and use a file as a cron-job?


2 years ago

before we continue, please read this docs page so you can get your terminology correct.


tarkbyte
FREEOP

2 years ago

If I understand correctly, I need to add a new service which will be the cron-job, right?


2 years ago

correct, there you go!


tarkbyte
FREEOP

2 years ago

So I can create a new repo, add something like node-cron and deploy this service?


2 years ago

not quite, you would want to use railways cron scheduler


2 years ago

you can just have a script in your current repo to do your task, and have the cron service run it


tarkbyte
FREEOP

2 years ago

1280330371950444500


tarkbyte
FREEOP

2 years ago

Is this the cron service you are talking about? I don't see where to add my script


tarkbyte
FREEOP

2 years ago

To point to my file which contains the script


2 years ago

correct, and you would be looking for the start command, the start command is how you set the command that starts the script, for example node task.js


tarkbyte
FREEOP

2 years ago

1280336225844072400


tarkbyte
FREEOP

2 years ago

this one?


2 years ago

that's it


2 years ago

of course as you mentioned, you would want to set this up on a duplicate service


tarkbyte
FREEOP

2 years ago

This is what I dont understand, this new service. Is there a template or something I need to use?


2 years ago

nope, right click duplicate, then set your cron schedule, and then set the start command to run whatever script you want ran on a schedule


tarkbyte
FREEOP

2 years ago

Alright, going to test this method. Thanks a lot for your help and patience 😆


tarkbyte
FREEOP

2 years ago

Btw, why it must be duplicated?


2 years ago

because you need two services, one service for nextjs, another service to run the cron job


tarkbyte
FREEOP

2 years ago

oh okay


tarkbyte
FREEOP

2 years ago

Makes sense


2 years ago

a service can't do two different things


2 years ago

one service for one thing, hence the need for two


tarkbyte
FREEOP

2 years ago

Okay I think I got it now. Thanks!


2 years ago

do you have a script that does your task?


2 years ago

or have you yet to write that code?


tarkbyte
FREEOP

2 years ago

Need to write that code


tarkbyte
FREEOP

2 years ago

Don't have it now


2 years ago

okay then that's the next thing you need to do before setting up cron on railway


tarkbyte
FREEOP

2 years ago

Yeah yeah


tarkbyte
FREEOP

2 years ago

Lets say I have this script in a file src/cron/script.js

Can I acces this in the start command like so? node src/cron/script.js


2 years ago

correct


tarkbyte
FREEOP

2 years ago

Alright


tarkbyte
FREEOP

2 years ago

Perfect


Loading...