2

I've got the following configuration line in my production.rb environment file, as instructed in this article:

  config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }

But when I try to deploy, I'll get an error:

Running: rake assets:precompile rake aborted!
undefined method split' for nil:NilClass
/tmp/build_abdc.../config/environments/production.rb:107:in
block in '

This is because config vars are not available during compilation. There's a Heroku labs add-on that you can supposedly use to remedy this, but it comes with a warning from Heroku that "Using this labs feature is considered counter to Heroku best practices."

So then what is the best practice when it comes to using ENV vars in production config? Should they just all be wrapped in rescue handlers so that Heroku ignores them during compilation?

Yarin
  • 173,523
  • 149
  • 402
  • 512
  • Maybe you can try moving the setting to an initializer: http://stackoverflow.com/questions/5810289/setting-the-cache-store-in-an-initializer – steakchaser Feb 09 '14 at 18:26
  • @steakchaser - thanks but that didn't change anything- the line still gets hit in compilation and raises same error – Yarin Feb 09 '14 at 19:12
  • muha! i love the asset pipeline! did you try to set `initialize_on_precompile`? i don't know if that's still a thing in rails4 but it saved my ass several times on heroku http://stackoverflow.com/questions/11889131/precompile-failing-on-heroku-with-initialize-on-precompile-set-to-false – phoet Feb 09 '14 at 21:25
  • are you sure that MEMCACHEDCLOUD_SERVERS exists in your environment? – rhernando Feb 09 '14 at 21:30
  • @phoet - interesting article- so it says "Your app’s config vars are not present in the environment during slug compilation, so you should take steps to handle the nil case for config vars". And then it says about `initialize_on_precompile`: "In Rails 4.x this option has been removed and is no longer needed". Huh? – Yarin Feb 09 '14 at 21:31
  • @rhernando - Yes, that's not the problem- the problem is that the environment doesn't exist during slug compilation. See [here](https://devcenter.heroku.com/articles/rails-asset-pipeline#failures-in-the-assets-precompile-task) – Yarin Feb 09 '14 at 21:33
  • @phoet, you live up to your name :) – Itamar Haber Feb 10 '14 at 00:01

1 Answers1

1

We ended up just checking for the ENV var before assigment. It looks like that's the pattern you need whenever you use ENV vars in config/initializers on Heroku:

  # NOTE: ENV vars aren't available during slug compilation, so we must check if they exist:
  if ENV["MEMCACHEDCLOUD_SERVERS"]
    config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }
  end

See also: https://devcenter.heroku.com/articles/rails-asset-pipeline#failures-in-the-assets-precompile-task

StephenA
  • 61
  • 6
Yarin
  • 173,523
  • 149
  • 402
  • 512