Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

ruby on rails - Re-source .bashrc when restarting unicorn?

I have some ENV variables that are sourced for the deploy user. (Similar to what Heroku recommends, but without using Heroku.)

My rails app depends on these for certain functions, for example, in application.rb:

config.action_mailer.default_url_options = { host: ENV['MY_HOST'] }

This is necessary because we have several staging hosts. Each host has MY_HOST defined to its correct hostname in .bashrc like so:

export MY_HOST="staging3.example.com"

This allows us to only use one rails staging environment, but still have each host's correct hostname used for testing, sending email, etc since this can be set on a per-machine basis.

Unfortunately it looks like when I restart Unicorn using USR2 it doesn't pick up changes to those variables. Doing a hard-stop and start will correctly load any changes.

I'm using preload_app = true which may I'm guessing has something to do with it. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the end I went away from this approach altogether in favor of loading my app config from an app_config.yml file. Ryan Bates covers this approach in Railscast #226.

The only thing I did differently is that I load a shared app_config.yml for each server I use. Since I'm using capistrano, I just symlink the file on deploy.

For example, on staging2 my shared/configs/app_config.yml looks like this:

staging:
  host: "staging2.example.com"

... whereas on staging3 it looks like this:

staging:
  host: "staging3.example.com"

Now my application.rb has this line instead:

config.action_mailer.default_url_options = { host: APP_CONFIG[:host] }

I removed the actual config/app_config.yml from git so that it's not included on deploy. (I moved it to config/app_config.yml.template.) Then on deploy I use a capistrano task to symlink shared/configs/app_config.yml to config/app_config.yml:

namespace :deploy do
  desc "Symlinks the app_config.yml"
  task :symlink_app_config, :roles => [:web, :app, :db] do
    run "ln -nfs #{deploy_to}/shared/config/app_config.yml #{release_path}/config/app_config.yml"
  end
end

This strategy has these benefits over using ENV vars:

  • Deployed to all nodes via capistrano
  • We can do conditional hosts by simply changing the file on the appropriate server
  • Unicorn will get changes with USR2 since everything's done inside of rails
  • Everything's kept in one place, and the environment isn't affected by some other variables outside of the codebase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...