ruby-on-railsrubyproduction-environmentrails-geocoder

Geocoder Gem not working in Production Environment


So I am using Geocoder to pull in latitude and longitude coordinates based on an address provided by a user when submitting a form. I am doing this so that I can plot markers using the Google Maps API. This works perfectly in development - zero issues. When I push to production however, the latitude and longitude are not generated by Geocoder. I have checked my production logs and there are zero errors. I also checked to make sure that the gem is not just installed for development and I have restarted unicorn and nginx on my production machine. Any ideas as to what could be going on?

Here is my model code for the model I am preforming this task in --

class Order < ActiveRecord::Base

  attr_accessible :city, :customer_email, :customer_id, :delivery_date, :delivery_time, :street, :zipcode, :coupon, :total, :name, :items_with_day, :user_id, :shopping_cart_id, :extras, :latitude, :longitude, :location_name, :phone, :suite, :state

  belongs_to :user

  validates :name, :presence => true
  validates :street, :presence => true
  validates :city, :presence => true
  validates :zipcode, :presence => true

  def address
    [street, city, state, "United States"].compact.join(', ')
  end

  geocoded_by :address
  after_validation :geocode

end

============ UPDATE =============

So after a suggestion, I opened up the rails console on my production machine using --

bundle exec rails console production

and ran --

Geocoder.search("San Francisco, CA")

which generated this error --

Geocoding API not responding fast enough (use Geocoder.configure(:timeout => ...) to set limit).
=> []

that being said, in my config/initilizers/geocoder.rb file, I have --

Geocoder::Configuration.timeout = 15

What is an appropriate timeout limit to set? I need the coordinates, but also don't want to let this run forever...

=========== UPDATE =============

I found this related question, though I would prefer to run this task server side, instead of on the client side.


Solution

  • The author of the Geocoder gem has this to say:

    [A]re you sure the app server was restarted since you added Geocoder?

    First, verify that the gem is indeed installed by issuing a Geocoder command in your production console:

    # from the production console
    Geocoder.search("San Francisco, CA")
    

    If this does not throw any errors, try restarting the server. A soft boot might work – a hard boot is better. This will reload your entire Rails environment, inclusive of any installed dependencies.

    UPDATE:

    As mentioned in the comments to this answer, the problem is related to the Geocoding API not responding fast enough error.

    How long is the appropriate timeout?

    That's hard to say. The gem's author suggests 15, as you already have it set to. The geocoding service may be momentarily slow – or it may be slow overall. IMO, set it incrementally higher and find the number where it starts working – then leave it there.

    Note that the client-side vs. server-side argument may not apply here. The SO post cited in the OP's question relates to request quotas. The problem being witnessed in the question, in contrast, seems to relate to geocoding speed, which would not be affected whether the calls are made by the client or by the server.