Set up cron on existing NodeJS project
I have a project where I need to run a few functions on a schedule, at the moment I'm hosted on Heroku so I run this using their scheduler which basically creates a new instance and runs "node myScript.js" when needed.
I understand Heroku doesn't have a similar feature but that I should use something like "node-cron" to do this on the application level, I found this article which explains pretty well:
https://blog.railway.app/p/cron-jobs
But I can't just create a new app that runs the cron and calls "myScript.js" because it has a few dependencies in my current project that'd have to copy and keep up to date across two apps.
I was thinking about creating a new file (eg:
cron.js
) that would handle all the scheduling login in my current project, adding a new node script that calls this file instead of my default start command (eg: node cron.js
) and deploying the same project as a new service but changing the start command to the node cron.js
one.
Would this be a good idea?Railway Blog
Cron Jobs on Railway
A brief guide on deploying and running cron jobs on Railway using JavaScript.
6 Replies
Project ID:
7c5f1144-97bf-4b98-a119-2c5febfb6f02
No search results found.
⚠️ experimental feature
7c5f1144-97bf-4b98-a119-2c5febfb6f02
sounds like it would work
try it and find out
What you described is the way to set up Cron on Railway, that'll work! You could also set up a new subdirectory call (eg. 'yourURL.com/cron') that begins a new run and create a new service that just contains the cron job
Personally that's how I would do it if your app is not user facing
That sounds like a good idea as well, thanks!