When deploying a Symfony2 app to Elastic Beanstalk I need to perform a manual cache clear in order for the app to begin functioning. So, I added a container command to clear the prod cache during deployment. The command runs, but I still had to manually clear in order for the app to work.
After some digging around, I found that there are absolute path strings in Symfony2 cache files themselves. The command I added runs "pre-deployment" or before the app files get moved from their staging folder (called '/var/app/ondeck') to their final resting place ('/var/app/current'). As a result, the absolute path strings in the cache files are wrong and the app fails to load.
Also, the dev environment works fine right away because it rebuilds its own cache automatically. The prod environment is the only one that is affected.
My question(s):
Thanks everyone in advance :-)
The problem occurs because the cache is filled in the ondeck environment when the various Symfony commands run at the end of the composer install process and/or your own commands (e.g. assetic:dump).
The solution is to clear the cache as the last command of the deployment and specify --no-warmup to stop Symfony automatically refilling it, so that the cache is empty when the environment is moved from ondeck to current. In my .ebextensions/symfony.config I have:
container_commands:
01_migrate:
command: php app/console doctrine:migrations:migrate --env=prod --no-debug --no-interaction
leader_only: true
02_dumpassets:
command: php app/console assetic:dump --env=prod --no-debug
99_clearcache:
command: php app/console cache:clear --env=prod --no-debug --no-warmup
It is not very well documented, but you can also use a post-deployment hook to warm the cache after the environment has been moved to current. Also in .ebextensions/symfony.config:
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/01-cachewarm.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
cd $EB_CONFIG_APP_CURRENT
php app/console cache:warmup --env=prod --no-debug
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With