rake aborted! LoadError: cannot load such file -- httparty - ruby-on-rails

I'm have the following in my seed.rb file:
require 'httparty'
require 'nokogiri'
require 'awesome_print'
BASE_URL = "http://www.legacy.com/funeral-homes/"
URL_PATH = "directory/"
def get_states(key, value)
arry = []
i = 1
while i <= value do
response = HTTParty.get(BASE_URL + URL_PATH + "#{key}" + "?page=#{i}")
page = Nokogiri::HTML(response.body)
rows = (page.css("div.fhlistitem").select {
|e| !e.content.squeeze(" ").strip.empty? }).map {
|e| [e.css('div.fhname a').first['href'],
e.content.squeeze("").strip.split("\r\n").compact ]
}
rows.each do |row|
row[1].delete(" ")
hash = {
"name" => row[1][0].strip,
"address" => row[1][1].strip,
"phone" => row[1][4].nil? ? "" : row[1][4].strip,
"email" => row[1][5].nil? ? "" : row[1][5].strip,
"license" => row[1][6],
"url" => row[0]
}
arry << hash
end
i += 1
end
arry
end
fhs = get_states("alaska", 4)
fhs.each do |fh|
FuneralHome.create(
name: fh["name"],
address: fh["address"],
phone: fh["phone"],
email: fh["email"],
license: fh["license"],
url: fh["url"],
)
end
When I run rake db:seed I get the following error:
WARN: Unresolved specs during Gem::Specification.reset:
mini_portile (~> 0.6.0)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
rake aborted!
Don't know how to build task 'db:seet'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/task_manager.rb:62:in `[]'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:149:in `invoke_task'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `each'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_exception_handling'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/lib/rake/application.rb:75:in `run'
/usr/local/lib/ruby/gems/2.1.0/gems/rake-10.3.2/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
I'm not sure how to interpret the error. I'm requiring 'httparty' in the file. How can I run a HTTParty GET request in my seed.rb file? Or is the a better alternative?

Don't know how to build task 'db:seet'
Maybe this is a problem: try to use "rake db:seed" instead of "rake db:seet"

Related

String can't be coerced into Integer, when running rake db:create in test environment

I am trying to create a Test database using rake db:create, It gives me the following error, I have recently upgraded from rails 3.2.17 to 3.2.22.5.
RAILS_ENV=test rake db:create --trace
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:create
String can't be coerced into Integer
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activesupport3.2.22.5/lib/active_support/core_ext/enumerable.rb:60:in `+'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activesupport3.2.22.5/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activesupport3.2.22.5/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activerecord3.2.22.5/lib/active_record/connection_adapters/postgresql_adapter.rb:747:in `create_database'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake144:in `rescue in create_database'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:84:in `create_database'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:62:in `block (3 levels) in <top (required)>'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:62:in `each'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:62:in `block (2 levels) in <top (required)>'
/data/rvm_installtion/.rvm/gems/ruby2.4.3#waltz/gems/rake10.5.0/lib/rake/task.rb:240:in `block in execute'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake10.5.0/lib/rake/task.rb:235:in `each'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake10.5.0/lib/rake/task.rb:235:in `execute'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/data/rvm_installtion/.rvm/rubies/ruby-2.4.3/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/gems/rake-10.5.0/bin/rake:33:in `<top (required)>'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/bin/rake:23:in `load'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/bin/rake:23:in `<main>'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/bin/ruby_executable_hooks:15:in `eval'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3#waltz/bin/ruby_executable_hooks:15:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "username"=>"postgres", "host"=>"localhost", "database"=>"test_database_name"}
Please suggest.
I am using Ruby 2.4.3 and Rails 3.2.22.5.
here is my database.yml file
DEFAULTS: &DEFAULTS
adapter: postgresql
encoding: unicode
pool: 5
username: postgres
host: localhost
development:
<<: *DEFAULTS
database: development
test: &test
<<: *DEFAULTS
database: test_2
staging:
<<: *test
production:
<<: *test
Thanks in Advance
Downgrade ruby version (e.g. 2.2)
It looks like rails 3.2 does not support ruby 2.4 (https://stackoverflow.com/a/23532476/299774)
This is terrible, but you can also add the following code to an initializer:
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
def create_database(name, options = {})
options = options.reverse_merge(:encoding => "utf8")
option_string = options.symbolize_keys.map do |key, value|
case key
when :owner
" OWNER = \"#{value}\""
when :template
" TEMPLATE = \"#{value}\""
when :encoding
" ENCODING = '#{value}'"
when :tablespace
" TABLESPACE = \"#{value}\""
when :connection_limit
" CONNECTION LIMIT = #{value}"
else
""
end
end
option_string = option_string.join("")
execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
end
end
end
end
This alters the problematic code from activerecord3.2.22.5/lib/active_record/connection_adapters/postgresql_adapter.rb:747; everything else should work.
config/initializers/postgresql_adapter.rb
require 'active_record/connection_adapters/postgresql_adapter'
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def create_database(name, options = {})
options = options.reverse_merge(:encoding => "utf8")
option_string = options.symbolize_keys.sum('') do |key, value|
case key
when :owner
" OWNER = \"#{value}\""
when :template
" TEMPLATE = \"#{value}\""
when :encoding
" ENCODING = '#{value}'"
when :tablespace
" TABLESPACE = \"#{value}\""
when :connection_limit
" CONNECTION LIMIT = #{value}"
else
""
end
end
execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
end
end
and then add line:
require File.expand_path('../config/initializers/postgresql_adapter', __FILE__)
into Rakefile

unable to run rake task with smarter_csv

I created a rake task for import a csv with smarter_csv gem.
Throws the following message, i added the entire error code if it helps:
rake import_csv:import_csv
Digest::Digest is deprecated; use Digest
I, [2017-03-01T11:46:08.551994 #7029] INFO -- : ** [Raven] Raven 0.14.0 configured not to send errors.
rake aborted!
NoMethodError: undefined method `close' for nil:NilClass
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `ensure in process'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Errno::ENOENT: No such file or directory # rb_sysopen - /csv_imports/pacientes.csv
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `initialize'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `open'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => import_csv:import_csv
(See full trace by running task with --trace)
This is the code on the rake task:
namespace :import_csv do
desc "import csv file"
task import_csv: :environment do
require 'smarter_csv'
filename = '/csv_imports/file.csv'
options = {:key_mapping => { :downcase_header => true }}
SmarterCSV.process(filename, options) do |array|
CsvImports.create( array.first )
end
end
end
This solved just witha dot. route issue.
./csv_imports/file.csv

ActionMailer w/ Custom Rake Task

I am trying to create two custom rake tasks in in Rails 4 app. They are supposed to read the agent_card table and based on the logic, fire off an email when the time is triggered. This is my first time creating a rake task and I am not getting an error, therefore I don't know if I am doing it right. I would assume when using the mailer that letter opener would work once the mailer is fired (should it all be working correctly), but I am not sure. I have posted all relevant code below. Thanks in advance!
.rake file
namespace :agent_cards do
desc 'Sends an email to an agent with a license expiring 2 months from today'
task license_expire_agent: :environment do
Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
AgentCard.all.each do |agent_card|
if agent_card.real_estate_license_expires_at == Date.today + 2.months
LicenseExpireMailer.license_expire_agent(#agent_card, #agent).deliver_later
end
end
end
end
namespace :agent_cards do
desc 'Sends an email to an the agent development manager when a license expires 1 week from today'
task license_expire_mgr: :environment do
Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
AgentCard.all.each do |agent_card|
if agent_card.real_estate_license_expires_at == Date.today + 7
LicenseExpireMailer.license_expire_mgr(#agent_card, #agent).deliver_later
end
end
end
end
license_expire_agent_mailer.rb
class LicenseExpireMailer < ActionMailer::Base
default from: "Mike <help#mike.com>"
def license_expire_agent(agent, agent_card)
#agent = agent
#agent_card = agent_card
mail to: "mike#mike.com", subject: 'Your license is about to expire!'
end
end
license_expire_mgr_mailer.rb
class LicenseExpireMailer < ActionMailer::Base
default from: "Mike <help#mike.com>"
def license_expire_mgr(agent, agent_card)
#agent = agent
#agent_card = agent_card
# Head of Agent Development
mail to: "mike#mike.com", subject: "#{#agent.name}'s License Expiring"
end
end
When I try to test the rake task I enter
rake agent_cards:license_expire_agent
or
rake agent_cards:license_expire_mgr
Error
NameError: uninitialized constant LicenseExpireMailer
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:7:in `block (3 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1#global/gems/activerecord- 4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1#global/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:5:in `block (2 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => agent_cards:license_expire_agent
Your mailer file name does not match with the mailer class name as per Rails convention.
Mailer class should be placed in this path:
app/mailers/license_expire_mailer.rb

Rails populate db rake task OpenURI::HTTPError: 500 Internal Server Error

I am trying to make rake task to populate db from JSON API fixer.io,
but when i type my rake :
rake db:populate
this error occurs:
OpenURI::HTTPError: 500 Internal Server Error
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:16:in `block (5 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:13:in `block (4 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:12:in `each'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:12:in `block (3 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:11:in `each'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:11:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:populate
(See full trace by running task with --trace)
This is my rake task (populate.rake) in lib/tasks:
require 'open-uri'
namespace :db do
desc "Erase and fill database"
task :populate => :environment do
require 'populator'
[ConversionRate].each(&:delete_all)
n = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys.count
currencies = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys
currencies.each do |curr1|
currencies.each do |curr2|
ConversionRate.populate 1 do |cr|
cr.currency1 = curr1
cr.currency2 = curr2
cr.conversion_rate = JSON.load(open('http://api.fixer.io/latest?base=' + curr1))["rates"][curr2]
end
end
end
end
end
Please help me, I have no idea what causes this problem.
the main problem is here, you are not taking keys of "rates"
currencies = JSON.load(open('http://api.fixer.io/latest')).keys
currencies = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys
require 'open-uri'
namespace :db do
desc "Erase and fill database"
task :populate => :environment do
require 'populator'
[ConversionRate].each(&:delete_all)
latest_data = JSON.load(open('http://api.fixer.io/latest'))
currencies = latest_data["rates"].keys
n = currencies.count
currencies.each do |curr1|
currencies.each do |curr2|
ConversionRate.populate 1 do |cr|
#take care of any error, as we are going to call third party api here
begin
cr.currency1 = curr1
cr.currency2 = curr2
cr.conversion_rate = JSON.load(open('http://api.fixer.io/latest?base=' + curr1))["rates"][curr2]
rescue => e
puts "error #{e}"
end
end
end
#give a bit rest
sleep 2
end
end
end

sunspot:reindex error - getaddrinfo: nodename nor servname provided, or not known

I'm working with a Rails 3.2 application that has a mysql database and a number of models that are being indexed in Solr.
Here's what's happening:
I am running the following command:
RAILS_ENV=development bundle exec rake sunspot:reindex[1000] --trace
After indexing about 12% of the 4 million records (although it's a different percentage every time), the process inevitably bombs out with the following error and stack trace:
rake aborted!
getaddrinfo: nodename nor servname provided, or not known
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `open'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `connect'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:744:in `start'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:1284:in `request'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/connection.rb:15:in `execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/solr_instrumentation.rb:14:in `block in execute_with_as_instrumentation'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/solr_instrumentation.rb:12:in `execute_with_as_instrumentation'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:167:in `execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:161:in `send_and_receive'
(eval):2:in `post'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:67:in `update'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:87:in `add'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:106:in `add_documents'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:30:in `add'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session.rb:91:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session_proxy/retry_5xx_session_proxy.rb:17:in `method_missing'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot.rb:184:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:261:in `block (2 levels) in solr_index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:365:in `solr_benchmark'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:260:in `block in solr_index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation/batches.rb:72:in `find_in_batches'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/querying.rb:8:in `find_in_batches'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:259:in `solr_index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:203:in `solr_reindex'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/tasks.rb:64:in `block (3 levels) in <top (required)>'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/class_set.rb:16:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/class_set.rb:16:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/tasks.rb:63:in `block (2 levels) in <top (required)>'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/bin/rake:33:in `<top (required)>'
/Users/tchapin/.rbenv/versions/1.9.3-p392/bin/rake:23:in `load'
/Users/tchapin/.rbenv/versions/1.9.3-p392/bin/rake:23:in `<main>'
The app is running in development mode at localhost:3000, and solr is running at localhost:8982. Here's my solr.rake file:
Rake::Task['sunspot:reindex'].enhance ['sunspot:scope_models_for_index']
Rake::Task['sunspot:solr:reindex'].enhance ['sunspot:scope_models_for_index']
namespace 'sunspot' do
task :scope_models_for_index => :environment do
require 'rsolr/error'
Dir.glob(Rails.root.join('app/models/**/*.rb')).each { |path| require path }
# Add the GC
commit_extension = Module.new do
def commit
GC.start
super
end
end
Sunspot.extend commit_extension
# Set all the models default scopes the index scope
Sunspot.searchable.each do |model|
model.class_eval do
default_scope ->{ sunspot_index } if model.respond_to?(:sunspot_index)
end
end
end
end
Anyone know what might be causing this error, or how to fix it?
Apparently the problem is due to an exception: SocketError.
The exception occurs in the file /Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/connection.rb:
def execute client, request_context
h = http request_context[:uri], request_context[:proxy], request_context[:read_timeout], request_context[:open_timeout]
request = setup_raw_request request_context
request.body = request_context[:data] if request_context[:method] == :post and request_context[:data]
begin
response = h.request request
charset = response.type_params["charset"]
{:status => response.code.to_i, :headers => response.to_hash, :body => force_charset(response.body, charset)}
rescue Errno::ECONNREFUSED => e
raise(Errno::ECONNREFUSED.new(request_context.inspect))
# catch the undefined closed? exception -- this is a confirmed ruby bug
rescue NoMethodError
$!.message == "undefined method `closed?' for nil:NilClass" ?
raise(Errno::ECONNREFUSED.new) :
raise($!)
end
end
It's not the prettiest fix, and it's obviously not a solution for production code, but I was able to temporarily solve the problem by adding a rescue handler for the SocketError exception, like so:
def execute client, request_context
h = http request_context[:uri], request_context[:proxy], request_context[:read_timeout], request_context[:open_timeout]
request = setup_raw_request request_context
request.body = request_context[:data] if request_context[:method] == :post and request_context[:data]
retries = 10
begin
response = h.request request
charset = response.type_params["charset"]
{:status => response.code.to_i, :headers => response.to_hash, :body => force_charset(response.body, charset)}
rescue Errno::ECONNREFUSED => e
raise(Errno::ECONNREFUSED.new(request_context.inspect))
# catch the undefined closed? exception -- this is a confirmed ruby bug
rescue NoMethodError
$!.message == "undefined method `closed?' for nil:NilClass" ?
raise(Errno::ECONNREFUSED.new) :
raise($!)
rescue SocketError => e
puts e
if retries > 0
puts "SocketError! Retrying connection after 1 second..."
retries -= 1
sleep(1)
retry
else
puts "SocketError: Not responding after 10 retries! Giving up!")
exit
end
end
end
Additionally, it looks like my local copy of Solr was having some issues with running out of memory. I solved the memory problem by updating my sunspot.yml file accordingly:
development:
solr:
hostname: 127.0.0.1
port: 8982
min_memory: 512M
max_memory: 2G
log_level: INFO
# open_timeout: 3
# read_timeout: 3

Resources