Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Symfony2 Application to AWS Elastic Beanstalk - Post Deployment Cache Clearing

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):

  • Is there a way to run the cache clear command automatically AFTER the code has been moved into place?
  • Alternatively, is there some way to get Symfony2 to let you specify a different "base path" for the cache generation? That way it could be set up to point to the correct final location.

Thanks everyone in advance :-)

like image 559
Chris Kozlowski Avatar asked Jan 02 '13 15:01

Chris Kozlowski


1 Answers

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      
like image 135
rhunwicks Avatar answered Sep 25 '22 01:09

rhunwicks