14

I have had lots of trouble with deploying my rails 3.0.10 application to Ubuntu 10.04 server with Passenger, Capistrano, nginx and MySQL (and even more trouble with apache2). After downgrading rake to 0.8.7 it's finally working, but now Sunspot/Solr doesn't work in my development environment. The sunspot_solr server is running, and I am able to get the server's url, and connect to it via the browser. The url is the same as in sunspot.yml. I have no idea what I have done that can have caused the problem. I had the same problem with sunspot earlier (before I downgraded rake). Then it suddenly started working again. I don't know why... I have tried two versions of Sunspot (1.2.1 and 1.3.0). Both have been working earlier, but not anymore.

This is the error message I'm getting:

SocketError (getaddrinfo: Name or service not known):
app/models/resource.rb:128:in `text_search'
app/controllers/search_controller.rb:21:in `index'

Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (161.6ms)
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (168.9ms)

The controller is a bit messy so I just include the relevant part:

@criterion = params[:criterion]
@sort_direction = params[:sort_direction]
if @criterion && @sort_direction
  session["sort_search"] = @criterion + "|" + @sort_direction 
elsif session["sort_search"]
  @criterion = session["sort_search"].split("|")[0]
  @sort_direction = session["sort_search"].split("|")[1]
else
  @criterion = "updated_at"
  @sort_direction = "desc"
 end
@search = Resource.text_search(session[:search_params] || "", current_user, @criterion, @sort_direction) 
@resources = @search.results

I have not changed anything in the controller since it was working.

My gemfile:

source 'http://rubygems.org'
gem 'rake'
gem 'rails', '3.0.10'
gem 'jquery-rails', '>= 1.0.12'
gem 'sqlite3'
gem 'ancestry'
gem 'carrierwave'
gem 'sunspot_rails', '>= 1.3' #'~> 1.2.1' #
gem 'authlogic'
gem 'will_paginate'
gem 'declarative_authorization'
group :production do
  gem 'mysql'
end
group :development do
  gem 'sunspot_solr'
end
gem 'capistrano'

My rakefile:

require File.expand_path('../config/application', __FILE__)
require 'rake'

Skolearkivet::Application.load_tasks
Johan Hovda
  • 855
  • 2
  • 10
  • 23

7 Answers7

34

just a wild guess, any chance you are using localhost as the hostname? Try changing localhost to 127.0.0.1

Sam Baumgarten
  • 2,231
  • 3
  • 21
  • 46
Roger
  • 7,535
  • 5
  • 41
  • 63
  • ah, its working? nice. yeah no idea, but that localhost thing is sometimes weird... – Roger Mar 11 '12 at 12:48
  • Yes, it's working! I guess that when the application is deployed, 'localhost' points to localhost on the remote server, and not on the development machine. Otherwise I can't understand it either. – Johan Hovda Mar 11 '12 at 13:05
  • I'm having the same issue yet my server_name isn't localhost, any advice? – Sam Baumgarten Apr 05 '13 at 04:57
  • @SamBaumgarten did you ever solve this? Did you check your /etc/hosts file? – Roger Jul 16 '13 at 08:12
  • I have some trouble changing that hostname, since Ror doesn't use Apache. Should localhost be mapped to 127.0.0.1 in the hosts file or what do you mean? – Rápli András Apr 22 '14 at 18:06
  • @RápliAndrás not sure what you mean. It depends on your setup. But normal you run passenger on your localhost (which is a instance of your RoR app). Apache (or NGINX) forwards the request to your localhost (i.e. passenger) and then back to the client. – Roger Apr 22 '14 at 19:56
  • Adding my dumb reason for getting this error, I wasn't vpn'ed in, so my script couldn't find jira..smh – Siddhartha May 30 '14 at 09:04
  • I by mistake had removed localhost entry from hosts – Amol Pujari Aug 08 '14 at 21:04
  • I got this when I set my redis config in rails to localhost instead of 127.0.0.1. +1 – film42 May 17 '16 at 17:10
10

I added this line to the top of file app/controllers/search_controller.rb

require 'resolv-replace'

Or alternatively you can put it to initializers/requires.rb

josliber
  • 43,891
  • 12
  • 98
  • 133
ramdanplusplus
  • 101
  • 1
  • 3
2

I've been having the same issue with rails and logstasher on OSX, couldn't figure out what was going on until I read this post. Thought I'd add this so that anyone else having the same kind of issue with logstasher can find something...

`=> Booting Unicorn
=> Rails 4.2.5 application starting in development on http://0.0.0.0:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/xx/Projects/xx/config/environments/development.rb:80:in 'getaddress': getaddrinfo: nodename nor servname provided, or not known (SocketError)`

Fix was to add my machine hostname to the list under /etc/hosts for 127.0.0.1

2

It could be that one of your third party service URLs is incorrect. For example, we had the wrong SMTP mail address. An exception within the app caused it to email the error to the admins and it failed with the error. Here's how you know when it's a bad address:

irb(main):009:0> Socket.gethostbyname("example.net")
SocketError: getaddrinfo: Name or service not known
konyak
  • 10,818
  • 4
  • 59
  • 65
1

I had a similar issue, which I worked around by editing the /etc/resolv.conf

before

nameserver 10.0.x.x
search example.com

after

#nameserver 10.0.x.x
nameserver 8.8.8.8
#search example.com
spuder
  • 17,437
  • 19
  • 87
  • 153
1

For me it was sufficient to remove the search line in my resolv.conf. My company inserted automatically it's own domain and my hostname was not a FQDN in my hosts file. This is a test environment obviously.

1

I added this line

require 'resolv-replace'

to my controller file where the error was coming from and it worked for me. Hope it works for you too!

Linda Kadz
  • 329
  • 2
  • 17