3 months ago
I have added multiple background worker scripts (email queue, token refresh, reply checker, payment retry, etc.) in package.json for a Next.js application. These workers are defined using tsx for development and compiled node scripts for production.
While the scripts are correctly configured in package.json, Railway appears to only run the default start command (npm run start) and does not automatically execute these worker processes.
I need clarification on:
Whether Railway will run these worker scripts automatically
If separate Railway services are required for each worker
Or if a process manager (PM2) or Railway cron/jobs should be used instead
package.json
"scripts": { "build": "next build", "dev": "next dev", "lint": "next lint", "start": "next start", "build:workers": "tsc --project tsconfig.workers.json", "worker": "tsx workers/email-queue-worker.ts", "worker:prod": "node dist/workers/email-queue-worker.js", "token-refresh": "tsx workers/token-refresh-worker.ts", "token-refresh:prod": "node dist/workers/token-refresh-worker.js", "reply-checker": "tsx workers/reply-checker-worker.ts", "reply-checker:prod": "node dist/workers/reply-checker-worker.js", "payment-retry": "tsx workers/payment-retry-worker.ts", "payment-retry:prod": "node dist/workers/payment-retry-worker.js", "pm2:start": "pm2 start ecosystem.config.js", "pm2:stop": "pm2 stop ecosystem.config.js", "pm2:restart": "pm2 restart ecosystem.config.js", "pm2:logs": "pm2 logs", "pm2:status": "pm2 status" }
The goal is to ensure all background workers run reliably in production alongside the main Next.js application.
1 Replies
3 months ago
yo @bhaveshtalati
railway containers are basically designed to run one main process per service. by default it just grabs the start script and runs it. it doesn't know about the other scripts automatically.
you got two main ways to handle this:
1. separate services (recommended)
deploy the same repo again as a new service in your project.
go to new -> github repo -> select this same repo.
then in the settings for that new service go to the Deploy section and change the Start Command to npm run worker:prod.
do this for your heavy workers like email-queue. this is usually better cause:
you get separate logs for the worker
if the worker crashes it doesn't kill your web app
you can scale them independently
2. process manager
since you already have pm2 in your scripts you could change your main start command to npm run pm2:start and have pm2 manage both nextjs and the workers inside one container.
just make sure your ecosystem.config.js is set up right and pm2 is in your dependencies.
honestly though managing pm2 inside docker can be annoying with logs and restarts.
also for stuff like token-refresh if its just running on a specific schedule check out the Cron Schedule tab in your service settings. you can set it to run that script every hour or whatever without needing a continuously running worker.
i would stick to option 1 for the queue stuff. keeps it clean.