Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Async geocoding with sidekiq #593

Closed
toxin20 opened this issue Feb 3, 2014 · 5 comments
Closed

Feature request: Async geocoding with sidekiq #593

toxin20 opened this issue Feb 3, 2014 · 5 comments
Labels

Comments

@toxin20
Copy link

toxin20 commented Feb 3, 2014

It would be convenient to be able to run the geocoding in a worker process like sidekiq or resque

There's a (slightly outdated) fork with sidekiq implemented:
https://github.com/rdeshpande/geocoder/blob/master/lib/geocoder/stores/active_record.rb#L241

Would this be possible at all?

@alexreisner
Copy link
Owner

I'm not sure this warrants an addition to Geocoder but perhaps something could be added to /examples?

@wasafiri
Copy link

wasafiri commented Nov 8, 2014

Just worked through this - here's an example of how it can be done without adding code to Geocoder. I used Sidekiq - so you'll have to set it up first, but implementation is pretty quick. In this example, I'm using zipcode to look up latitude and longitude. You'll want to remove the geocoded_by and after_validation lines from your model if they're in there.

in the controller:

def create
  if object.save
    GeocoderWorker.perform_async(@object.id) unless @object.zipcode.blank?
  end
end

then create a worker:

#app/workers/GeocoderWorker.rb

class GeocoderWorker
  include Sidekiq::Worker

  def perform(object_id)
    object = object.find(object_id)

    coordinates = Geocoder.coordinates(object.zipcode)
    object.latitude = coordinates[0]
    object.longitude = coordinates[1]
    object.save!
  end
end

Feel free to add this to /examples.

@alexreisner
Copy link
Owner

@wasafiri thanks! I just added a simplified version of this as an example. I'm closing this issue for now, but if anyone wants to submit a pull request for a generalized worker that could be used for any geocoded ActiveRecord class (ie, accepts a class name as well as an ID) I'd certainly consider merging.

@richardonrails
Copy link

richardonrails commented Nov 14, 2020

I also wanted to convert my sync ActiveRecord geocoding:

  geocoded_by :geocoded_by
  after_validation :geocode, if: :geocoded_by_changed?

To conditionally happen async. (e.g. When user is updating one record, do it sync, but when updating a batch of records, do it async)
One idea was to perhaps have some way to pass a flag when creating/updating, like model.save(geocode_async: true)

I understand that I could remove the ActiveRecord geocoding altogether and geocode manually in controllers, but that seems less convenient and I might forget a spot where I update model attributes and forget to geocode.

Ideas welcomed, otherwise I suppose I'll just have to remove all the convenient ActiveRecord bits.

@richardonrails
Copy link

One other thought is that even if I wanted it to always happen async, it'd be nice to be able to do that with like an async option on the geocoded_by flag. Or perhaps it could be done with a custom geocoder that always performs async, while still using the ActiveRecord integration?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants