Skip to content

Using ENV variables in config

Lloyd Philbrook edited this page Mar 30, 2015 · 2 revisions

Using ENV variables in config

Some people love to use ENV to pass dynamic variables to the eye config, but this is not a good approach.

For example:

Eye.app :some do
  working_dir "/project1"
  (ENV['WORKERS'] || 5).to_i.times do |i|
    process "worker-#{i}" do    
      # ...
    end
  end
end

When you call eye for the first time. Let's say you start eye with: WORKERS=10 eye load config.eye, and than call it again with: WORKERS=15 eye load config.eye. The workers count will not change because the eye server started with local ENV variables during the first eye load. Any subsequent load command just reevaluates the config on the server and does not reload the ENV variables.

How to solve this:

Use configs (yml for example), which updated for example by capistrano or something.

Eye.app :some do
  working_dir "/project1"
  config = YAML.load_file(File.join(self.working_dir, %w{config config.yml}))
  workers_count = config[:workers_count] || 5
  workers_count.times do |i|
    process "worker-#{i}" do    
      # ...
    end
  end
end

To manage env variables for spawned process in one place:

  1. Write all env variables by hand in the eye config.
Eye.app :some do
   env 'A' => 'B', 'C' => 'D'
end
  1. Use a dotenv file:
Eye.app :some do
  working_dir "/project1/"
  load_env ".env"
end

This will load env variables from file /project1/.env (on every load)

/project1/.env

A=1
B=2
C=SOMETHING

to load the dotenv file in bash

export $(cat /project1/.env | xargs)
  1. you can place env variables in some other place, like a .yml file and load it by hand
Eye.app :some do
  YAML.load_file("some_file").each do |k, v|
    env k => v
  end
end