title: Redis connection refused through ruby daemon worker on Heroku
body: <p>A Rails 3.2.6 application running as a 'web' Heroku process is connecting to Redis using the ENV["REDISTOGO_URL"] environment variable:</p>
  
  <pre><code>irb(main):002:0&gt; Redis.current
  =&gt; #&lt;Redis client v2.2.2 connected to redis://xxx.redistogo.com:1234/0 (Redis v2.4.11)&gt;
  </code></pre>
  
  <p>----- /initializers/redis.rb</p>
  
  <pre><code>if Rails.env.development?
    Redis.current = Redis.new
  elsif Rails.env.test?
    Redis.current = Redis.new
  elsif Rails.env.production?
    uri = URI.parse(ENV["REDISTOGO_URL"])
    Redis.current = Redis.new(:host =&gt; uri.host, :port =&gt; uri.port, :password =&gt; uri.password)
  end
  </code></pre>
  
  <p>A ruby daemon 'streaming' process runs as a secondary worker:</p>
  
  <p>----- Procfile</p>
  
  <pre><code>web: bundle exec rails server thin -p $PORT -e $RACK_ENV
  worker: bundle exec rake jobs:work
  streaming: RAILS_ENV=production ruby bin/streaming.rb start
  </code></pre>
  
  <p>However, the streaming process is crashing when it calls methods in the main Rails application that connect with Redis - even though the streaming process supposed to be loading the same redis.rb initializer as the 'web' Rails application process.</p>
  
  <p>----- /bin/streaming_ctl.rb</p>
  
  <pre><code># encoding: UTF-8
  
  require "rubygems"
  require "bundler/setup"
  require "daemons"
  
  Daemons.run(File.expand_path("../streaming.rb", __FILE__))
  </code></pre>
  
  <p>----- /bin/streaming.rb</p>
  
  <pre><code># encoding: UTF-8
  
  # TODO: set rails env in init script
  ENV["RAILS_ENV"] ||= "production"
  
  # load rails environment
  require File.expand_path('../../config/environment', __FILE__)
  
  logger = ActiveSupport::BufferedLogger.new(File.expand_path("./../../log/streaming.log", __FILE__))
  
  Streaming.start
  logger.info("\nStarting streaming in #{Rails.env.to_s} mode.")
  </code></pre>
  
  <p>Why is the streaming process/worker using the default Redis host and port?</p>
  
  <pre><code>[streaming.1]: /app/vendor/bundle/ruby/1.9.1/gems/redis-2.2.2/lib/redis/client.rb:236:in `rescue in establish_connection': Connection refused - Unable to connect to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
  </code></pre>