Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Production Rake Tasks Don't Recognize My Models

When I was running Heroku Bamboo, this was never a problem. Now, on Cedar, I get errors whenever I try to access my models from within a rake task on the server. This happens with rake db:seed, a standard rake task, as well as my own custom built tasks that explicitly include :environment. I even do so redundantly:

namespace :db do
  desc "Update db"
  task :new_seed => :environment do
    require './Scraped_Data/Games/code/column-headers.rb'
    require 'csv'
    require 'net/http'
    require './config/environment.rb'

    # code here...

  end
end

I can't find any mention of this issue elsewhere, and all of these tasks run perfectly in development. Thanks for any insights, and here is the full error message rake tasks spit out as soon as they encounter a model of mine on Heroku:

rake aborted!
uninitialized constant Object::Movie
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing'
/app/lib/tasks/new_seed.rake:187:in `block in load_scraped_data'
/app/vendor/bundle/ruby/1.9.1/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:37:in `open'
/app/vendor/bundle/ruby/1.9.1/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:37:in `open'
/app/lib/tasks/new_seed.rake:148:in `load_scraped_data'
/app/lib/tasks/new_seed.rake:550:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/app/vendor/bundle/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/app/vendor/bundle/ruby/1.9.1/bin/rake:19:in `load'
/app/vendor/bundle/ruby/1.9.1/bin/rake:19:in `<main>'
Tasks: TOP => db:new_seed
like image 828
Trespassers W Avatar asked Mar 22 '12 19:03

Trespassers W


People also ask

Where to put Rake tasks?

How to Write a Rake Task. You can put this code inside a file named Rakefile , or if you're using Rails, you can save this under lib/tasks/apple. rake .

How to create Rake task Rails?

rake extension and are placed in Rails. root/lib/tasks . You can create these custom rake tasks with the bin/rails generate task command. If your need to interact with your application models, perform database queries and so on, your task should depend on the environment task, which will load your application code.

Where are Rails rake tasks defined?

How to create a new rake task. User defined rake tasks live inside the lib/tasks folder. Any file ending in ". rake" will be automatically picked up and loaded by Rails.

What is environment rake task?

Including => :environment will tell Rake to load full the application environment, giving the relevant task access to things like classes, helpers, etc. Without the :environment , you won't have access to any of those extras.


2 Answers

By default threadsafe set dependency_loading = false
If you want to enable threadsafe in your application and access to your models in your task, you'll need to load it.

# Enable threaded mode
config.threadsafe!
config.dependency_loading = true if $rails_rake_task


Ref.: http://nowhereman.github.com/how-to/rails_thread_safe/

Hope this help!

like image 62
Olivier Grimard Avatar answered Sep 23 '22 10:09

Olivier Grimard


I had threadsafe! = true configured in my production environment at config/environments/production.rb

Disabling this solves the problem.

The answer found here: rake aborted! uninitialized constant Object::Country, why can't see model? gives some more explanation and other workaround options (particularly the last link)

like image 34
Trespassers W Avatar answered Sep 22 '22 10:09

Trespassers W