SocketError (getaddrinfo: Name or service not known) - Sunspot/Solr Rails development
Asked Answered
G

7

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
Gnaw answered 11/3, 2012 at 10:58 Comment(4)
please post the source of the controllerSymbiosis
edited my posting to include the relevant parts of the controllerGnaw
i think we need more, since there is error is triggered in the model: app/models/resource.rb:128:Symbiosis
Everything in the controller and the model has been working fine. The only things I have changed, are files like gemfile, rakefile, etc. I'll edit my initial post to include these.Gnaw
S
34

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

Symbiosis answered 11/3, 2012 at 11:49 Comment(9)
ah, its working? nice. yeah no idea, but that localhost thing is sometimes weird...Symbiosis
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.Gnaw
I'm having the same issue yet my server_name isn't localhost, any advice?Mollymollycoddle
@SamBaumgarten did you ever solve this? Did you check your /etc/hosts file?Symbiosis
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?Jazzman
@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.Symbiosis
Adding my dumb reason for getting this error, I wasn't vpn'ed in, so my script couldn't find jira..smhNylanylghau
I by mistake had removed localhost entry from hostsDecline
I got this when I set my redis config in rails to localhost instead of 127.0.0.1. +1Sosthena
S
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

Scold answered 12/6, 2015 at 23:27 Comment(0)
J
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

Jewry answered 19/7, 2016 at 19:23 Comment(0)
B
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
Blackcap answered 30/4, 2018 at 22:0 Comment(0)
S
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
Stroller answered 15/1, 2015 at 0:6 Comment(1)
what do the above changes actually do?Barbwire
P
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.

Pepi answered 29/9, 2017 at 10:19 Comment(0)
M
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!

Mccurry answered 8/8, 2018 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.