Skip to content
Snippets Groups Projects
Commit e27a6aed authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #820 from malandrina/lw-update-worker-example

Update background job example
parents 60d4498c 56d30cd8
No related branches found
No related tags found
No related merge requests found
# This class implements an ActiveJob job for performing reverse-geocoding
# asynchronously. Example usage:
# if @location.save && @location.address.blank?
# ReverseGeocodeJob.perform_later(@location)
# end
# Be sure to configure the queue adapter in config/application.rb:
# config.active_job.queue_adapter = :sidekiq
# You can read the Rails docs for more information on configuring ActiveJob:
# http://edgeguides.rubyonrails.org/active_job_basics.html
class ReverseGeocodeJob < ActiveJob::Base
queue_as :high
def perform(location)
address = address(location)
if address.present?
location.update(address: address)
end
end
private
def address(location)
Geocoder.address(location.coordinates)
rescue => exception
MonitoringService.notify(exception, location: { id: location.id })
if retryable?(exception)
raise exception
end
end
def retryable?(exception)
exception.is_a?(TimeoutError) || exception.is_a?(SocketError)
end
end
# This class implements a Sidekiq worker for performing geocoding
# asynchronously. Do something like this in your controller:
#
# if @object.save
# GeocoderWorker.perform_async(@object.id)
# end
#
class GeocoderWorker
include Sidekiq::Worker
def perform(object_id)
object = Object.find(object_id)
object.geocode
object.save!
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment