Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Inappropriately accesses the database during initialization #81

Closed
stouset opened this issue Nov 6, 2011 · 12 comments
Closed

Inappropriately accesses the database during initialization #81

stouset opened this issue Nov 6, 2011 · 12 comments
Milestone

Comments

@stouset
Copy link

stouset commented Nov 6, 2011

In Rails 3, when running rake assets:precompile, Vanity tries to access the production Redis database. It also tries to access the SQL database if model is specified in a a metric. Unfortunately, in an app that runs on Heroku, the production database is never specified and/or accessible from the local console.

I'm not sure exactly why it would be accessing the databases when even ActiveRecord doesn't, but it needs to somehow access the information lazily rather than actively.

@tkharris
Copy link

I have this problem as well, and it's a pain. Have to pull vanity out, precompile, then put it back in again :(

@tkharris
Copy link

Have a workaround of sorts. see commit https://github.com/tkharris/vanity/commit/03eb846e8fc09b4a3d27f7e26b282e13ae76f1fd in tkharris/vanity where we conditionally init based on an environment variable, then I set that variable in the assets:precompile with a file in lib/tasks:

namespace :assets do
  Rake::Task[:precompile].enhance [:novanity]

  desc "Don't load vanity during assets:precompile"
  task :novanity do
    ENV['NOVANITY'] = '1'
  end
end

This is not a fix really, vanity shouldn't depend on a db connection to assets:precompile, but is convenient for the time being.

@tkharris
Copy link

tkharris commented Mar 3, 2012

It's interesting that New Relic apparently has the exact same problem, and and very similar solution to their problem was proposed, using ENV['NEWRELIC_ENABLE'] = 'false' in the rakefile.

@eugenijusr
Copy link

This worked for me:
config.assets.initialize_on_precompile = false

@phillbaker
Copy link
Collaborator

Hi @tkharris and @stouset thanks for reporting this bug. I know that this is quite an old thread, but I've recently joined the contributors to Vanity, and I wanted to let you know that I'll be looking at this bug. Has there been any further movement on workarounds?

Thanks for using Vanity!

@stouset
Copy link
Author

stouset commented Oct 6, 2013

Not from my end. As you said, this is quite old. :)

@phillbaker
Copy link
Collaborator

I think I'm going to close this issue. As @eugenijusr points out, config.assets.initialize_on_precompile = false is the recommended setting for deploying apps to Heroku: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets. Does that solve the issue?

@stouset
Copy link
Author

stouset commented Oct 7, 2013

I believe you've misunderstood the problem.

When you precompile assets, you run it in the production environment (regardless of where it is run). Regardless of the Heroku issue, if you do not have a functioning production database configured in database.yml, Vanity will fail as it attempts to access the database.

@phillbaker
Copy link
Collaborator

@stouset thanks for following up. I think you're right, I'll reopen.

@phillbaker phillbaker reopened this Oct 8, 2013
@phillbaker
Copy link
Collaborator

@tkharris thanks for pointing out the new relic example, some links from their repo:

@phillbaker
Copy link
Collaborator

Sorry, I just want to clarify some thoughts:

  1. I definitely see the value in being able to init the vanity gem without needing a datastore connection (redis/db/mongo/etc.). (This also ties somewhat to Graceful operation if redis is down? #11?) I think the New Relic approach of a singleton that can be extended to black list rake tasks and environment variables is a sensible approach, and will probably move forward with that.
  2. @stouset I do think the config.assets.initialize_on_precompile = false flag in application.rb is a workaround. Using a config/vanity.yml (thus forcing a db connection), commenting out the production hash in database.yml, config.assets.initialize_on_precompile = false flag in application.rb and running RAILS_ENV=production be rake assets:precompile works for me. Under what circumstances does this workaround fail? Thanks!

phillbaker added a commit that referenced this issue Oct 10, 2013
* Refactor Playground#initialize to use #autoconnect.
* Closes #81.
phillbaker added a commit that referenced this issue Oct 11, 2013
* Refactor Playground#initialize to use #autoconnect.
* Closes #81.
@phillbaker
Copy link
Collaborator

A config/vanity.yml that turns off collection will allow production servers to run without any persistence at all [1].
/cc @chadwpry

E.g.:

production:
  collecting: false

Alternatively, the mock adapter will allow Vanity to be used with in-memory persistence, so the mock adapter could be used if track! is not called.

production:
  adapter: mock

[1] http://vanity.labnotes.org/configuring.html

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants