Migrate from PM2 to railway?

jdheeterPRO

3 months ago

I'm interested in migrating my PM2 express server + cronjobs project to Railway. Normally I have everything in a vps, with pm2, and all the "services" defined in the pm2 ecosystem file, the server and cronjobs, and in the ecosystem I configure the cron restarts. The cronjobs are each an executable .js file in the dist folder.

Running an express server is simple on Railway, but how do you manage a single repo with dozens of executable cronjobs? Ideally I like to keep them isolated, so each job has a dedicated node instance managed by PM2. I like the idea of using the native cron/restart capabilities of Railway, so each cronjob would be a service? I don't see a simple way to define multiple services from a single repo root. The documentation around monorepo assumes that each service is in a dedicated folder.

I know I can run PM2 inside Railway, but it doesn't seem like that is the native way to do it. Any ideas?

Awaiting User Response

9 Replies

3 months ago

Hello,

Yes each CRON job would be its own service, just with a different start command set (in the service settings) to start whatever is applicable for that specific service.

> I don't see a simple way to define multiple services from a single repo root

Just deploy your repo into the same project again, it's going to use the root unless you specify otherwise!

You can also right-click -> duplicate an already existing service!

So in summary -

  • Each CRON would be a duplicate service they deploys from the same repo, just with a custom start command.

  • Anything else that you had PM2 running would be a separate service, same concept, with a custom start command.

Please let me know if you have any extra questions!

Best,
Brody


Status changed to Awaiting User Response railway[bot] 3 months ago


jdheeterPRO

3 months ago

how would you specify multiple custom start commands? I'm looking for a replacement for the pm2 ecosystem file where you have a list of services and their restart intervals. in railway.json you can only specify a single start command/service. I would prefer to be able to do it all using config files without touching the railway dashboard. Additionally, let's say I have 20 cronjobs in a single repo, and each job is a separate service in a railway project, I update my repo and this triggers 20 new deployments in my project? Does that mean I'm now building my project 20 times every time I need to make a change? What if there is an issue and I need to roll things back to a previous deployment? I need to manually rollback 20 services? For example in pm2 I would just say "pm2 restart all" to restart all services, and I just build the repo once and all the cronjobs are now updated.


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

> how would you specify multiple custom start commands?

That would be done on a per-service basis, each service you would set the start command in the service settings, or you could set it in custom railway.json files, like cron1.railway.json, cron2.railway.json -- and then in the service settings, set the desired config file for that service.

> I'm looking for a replacement for the pm2 ecosystem file where you have a list of services and their restart intervals.

You could go with option two of my previous example since you can set the CRON schedule in the railway.json file.

> I would prefer to be able to do it all using config files without touching the railway dashboard.

No matter what, you would still have to touch the dashboard, we do not have support for project-as-code, only config-as-code at the service level, for a single service.

> let's say I have 20 cronjobs in a single repo, and each job is a separate service in a railway project, I update my repo and this triggers 20 new deployments in my project?

It would yes, but I think watch paths can help here -

https://docs.railway.com/guides/monorepo#watch-paths

> What if there is an issue and I need to roll things back to a previous deployment? I need to manually rollback 20 services?

Correct, you would have to roll back any service you need to individually

---

Perhaps you could go for the best of both worlds here? a single Railway service that would encompass all CRON jobs, that service would just run PM2 to handle only the CRON tasks.


Status changed to Awaiting User Response railway[bot] 3 months ago


jdheeterPRO

3 months ago

Yeah I'm thinking that just having two services, one for all cronjobs running pm2 and one that just runs the express server would make sense. It seems like railway is not really designed for large numbers of micro services.


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

I think we are very well designed for a large number of microservers, we may not have a way to roll back 20 services all at once, but with service groups you can very easily organize the project!

https://x.com/RemsItIs/status/1889112386629403115


Status changed to Awaiting User Response railway[bot] 3 months ago


jdheeterPRO

3 months ago

What is a service group, I searched on the docs but don't see any information about that. Would that solve the issue?


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

More info here -

https://help.railway.com/feedback/service-groups-c1faed81

Though they are long since out of beta.


brody

More info here -https://help.railway.com/feedback/service-groups-c1faed81Though they are long since out of beta.

jdheeterPRO

3 months ago

I'm confused how would I run 20 crown job services


3 months ago

You would run 20 Cron jobs as 20 Railway services, or a single service that runs PM2 for just the Cron jobs.

Service Groups is just a nice way to stay organized.


Status changed to Awaiting User Response railway[bot] 3 months ago


Migrate from PM2 to railway? - Railway Help Station