10 months ago
In a recent thread related to a problem during deployment I discovered something strange.
Here is the thread: https://station.railway.com/questions/service-deployment-clear-my-redis-queue-d1a5b6e7
I have a Laravel application, so I created a pre-deployment script as suggested by the documentation. I customized the script a bit to avoid running the cache:clear command during deployment. It's also a practice recommended in the Laravel documentation.
The problem is that I noticed cache:clear command being executed after every deployment anyway.
Looking at the deployment logs I found something is running commads autonomously, out of the pre-deployment script.
Look at the thread I linked above for a detailed analysis. I also attach the logs to this message.
It seems something runs like two start/stop container sessions, and during the second one it runs a bunch of Laravel commands.
Am I missing something? If something is running commands autonomously why I need a deployment script in the first place? How can I get control of this process? The cache:clear command can be killer for my application because it's at risk of a cache stampede against the database, due for the huge traffic it handle.
The logs refers to this deplyment: e29fc22d-18b3-42aa-97d9-c8c92913aa7b
7 Replies
10 months ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open Railway • 10 months ago
a month ago
Those second-container commands match Railpack's generated Laravel startup script. Your pre-deploy script and the service's startup script are separate phases; removing cache:clear from pre-deploy does not remove Railpack's startup behavior.
The upstream start-container.sh explicitly runs php artisan optimize:clear before php artisan optimize:
https://github.com/railwayapp/railpack/blob/main/core/providers/php/start-container.sh
Laravel's OptimizeClearCommand includes cache:clear among its subcommands, which explains the application-cache flush:
Railpack documents the override: place your own start-container.sh in the project root:
https://railpack.com/languages/php/#startup-process
Copy the startup script for your deployed Railpack version into that file and remove its php artisan optimize:clear line. Preserve the server startup and any storage-link step you need. If migrations already run in pre-deploy, also remove the duplicate startup migration block from your custom file. RAILPACK_SKIP_MIGRATIONS alone only disables migrations; it does not skip optimize:clear.
For the cache commands, use only the individual config/event/route/view caching commands your application needs, or optimize if that is appropriate for your Laravel version. Check that your custom startup file has executable permissions and is the one used by the built image, then test a deployment in staging while watching for the unwanted “Clearing cached bootstrap files” / cache-clear output.
The separate Redis/Dragonfly databases from your other thread protect the queue. This startup override addresses the remaining issue you raised: controlling whether application cache is cleared at all.
AI-assisted reply; the linked source code, your other thread's logs and Railpack documentation were reviewed. I have not deployed this change to your application.
a month ago
In my case the content to copy into /start-container.sh script should be: https://github.com/railwayapp/railpack/blob/main/core/providers/php/start-container.laravel.sh
Now the problem is that I run three services for the same Laravel app repository: Web App, Cron Scheduler, Background worker - This is a Railway suggested topology: https://docs.railway.com/guides/laravel#set-up-database-migrations-crons-and-workers
**Are all three services running migrations on every deploy? **
I think I should set RAILPACK_SKIP_MIGRATIONS=false in two of these three services. So migration run only once from one of them. Is it correct, or Railway does something under the hood I don't catch?
a month ago
I tried a deploy with a custom /start-container.sh commenting the optimize:clear command, and it worked. But it has effect only on the Web App service. It seems Worker and Cron Scheduler serivces completely ignore this script. They don't run migration, don't run cache commands, storage link, nothing. Why? How can I have control of the deploy for these two services?
a month ago
What you are seeing is expected for that three-service topology. The custom start-container.sh is Railpack's default PHP/web start command. Your Worker and Cron services have their own Railway Custom Start Commands (the guide uses railway/run-worker.sh and railway/run-cron.sh), so they intentionally bypass /start-container.sh. Control those services by editing those two scripts and their per-service Custom Start Command; do not add web startup, storage:link, optimize, or migrations to them unless you deliberately need that behavior.
Also, the flag is the opposite of what you wrote: RAILPACK_SKIP_MIGRATIONS=true means skip migrations. false (and the default) allows the generated Laravel web startup to run them. In your current Worker/Cron setup the flag is effectively irrelevant because their custom start commands bypass the Railpack web startup script.
For a deterministic setup, run php artisan migrate --force exactly once, preferably as the App service's Pre-deploy Command (or its single init script), and remove migration execution from the runtime startup script. Set RAILPACK_SKIP_MIGRATIONS=true on the App service so Railpack does not run a second copy at startup. You can also set it on Worker/Cron defensively, although their current custom commands already skip that path.
Recommended separation:
- App pre-deploy: php artisan migrate --force
- App start: your custom start-container.sh without optimize:clear or migrations
- Worker start: chmod +x ./railway/run-worker.sh && sh ./railway/run-worker.sh
- Cron start: chmod +x ./railway/run-cron.sh && sh ./railway/run-cron.sh
This matches Railway's Laravel guide and Railpack's documented startup behavior. Test one staging deploy and confirm the logs contain one migration run, one web startup, and no migration/cache commands in Worker or Cron.
Sources: https://docs.railway.com/guides/laravel#set-up-database-migrations-crons-and-workers and https://railpack.com/languages/php/#startup-process
AI-assisted reply; the flag semantics and service start-command behavior were checked against the current official Railway and Railpack documentation.
a month ago
Basically I don't need the "pre-deploy" command in the app service. I can use three start scripts to make it clear.
I have to say that it's impossible to understand how to configure this process from the Railway documentation.
This link in particular: https://docs.railway.com/guides/laravel#set-up-database-migrations-crons-and-workers seems totally wrong. The init-app.sh script with migrations and cache commands is configured as "Pre-Deploy Command". When this script runs, migrations and cache commands have already been executed by the start script. This is the reason why I created this thread and what your suggestion clarify. The documentation is reporting misleading information.
a month ago
Keep migrations in one App-service Pre-Deploy Command, not in any of the three Start Commands.
Railway runs build → Pre-Deploy in a separate container → starts the new deployment only after Pre-Deploy succeeds. Start Commands can rerun after restarts, redeploys or scale-out, so migrations there can repeat or race.
The duplicate came from using the guide’s init-app.sh as Pre-Deploy while Railpack’s Laravel start-container.sh also runs migrate unless RAILPACK_SKIP_MIGRATIONS=true, plus optimize:clear/optimize.
Use:
- App Pre-Deploy: php artisan migrate --force
- App Start: custom start-container.sh without migrations or optimize:clear
- Worker Start: only railway/run-worker.sh
- Cron Start: only railway/run-cron.sh
- App variable: RAILPACK_SKIP_MIGRATIONS=true
- Worker/Cron: no migration or cache commands
So the guide’s lifecycle placement is right, but it is misleading because it does not mention current Railpack startup duplicates migrations/cache optimization.
Sources:
https://docs.railway.com/deployments/pre-deploy-command
https://docs.railway.com/guides/laravel#set-up-database-migrations-crons-and-workers
https://github.com/railwayapp/railpack/blob/main/core/providers/php/start-container.sh
AI-assisted; verified against the linked official docs/source.
a month ago
I'm not sure your description is correct. The problem of this thread was because start script is executed before pre-deploy. In this case the first two points don't hold.