Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# This class implements an ActiveJob job for performing reverse-geocoding
# asynchronously. Example usage:
# if @location.save && @location.address.blank?
# ReverseGeocodeJob.perform_later(@location.id)
# 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_id)
location = Location.where(id: location_id).first
if location.present?
reverse_geocode(location)
end
end
private
def reverse_geocode(location)
address = address(location)
if address.present?
location.update(address: address)
end
end
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