Search code examples
ruby-on-railsbundlerpassengermonit

How to update ENV variable on Passenger standalone restart


I'm using Capistrano to deploy my application. Application runs on Passenger standalone. When I redeploy the application the Passenger still uses the Gemfile from the the old release because BUNDLE_GEMFILE environment variable has not been updated.

Where I should put the updated path to Gemfile so that Passenger would pick it up on restart?

The server startup command is in monit and I just call monit scripts from Capistrano tasks except for restart where I just touch the restart.txt.

namespace :deploy do

  task :stop do
    run("sudo /usr/bin/monit stop my_app_#{rails_env}")
  end

  task :restart do
    run("cd #{current_path} && touch tmp/restart.txt")
  end

  task :start do
    run("sudo /usr/bin/monit start my_app_#{rails_env}")
  end

The startup command in monit is:

start program = "/bin/su - app_user -l -c 'cd /home/app_user/current && bundle exec passenger start -d -p 8504 -e production  --pid-file=/home/app_user/current/tmp/pids/passenger.8504.pid  /home/app_user/current'"

I already tried to add the BUNDLE_GEMFILE into the startup command like this:

start program = "/bin/su - app_user -l -c 'cd /home/app_user/current && BUNDLE_GEMFILE=/home/app_user/current/Gemfile bundle exec passenger start -d -p 8504 -e production  --pid-file=/home/app_user/current/tmp/pids/passenger.8504.pid  /home/app_user/current'"

But it didn't work since the path /home/app_user/current is a symlink to a release path and that release path was picked up instead.


Solution

  • Simple solution.

    Define the Gemfile to be used in the server start command. For example:

    BUNDLE_GEMFILE=/home/app_user/current/Gemfile bundle exec passenger start -d -p 9999 -e production  --pid-file=/home/app_user/current/tmp/pids/passenger.9999.pid  /home/app_user/current
    

    The earlier solution (setting the BUNDLE_GEMFILE env variable in .profile) is not good. When you are deploying a new version of your application and there is a new gem in the bundle the migrations etc. will fail because it will still use the Gemfile defined in the env variable.