Added option to move geocoding to background
Andrew Kane
committed Oct 27, 2014
commit 729d9e4496b38759b0ecefcc02f47c03eb6edabf
Showing 8
changed files with
66 additions
and 9 deletions
CHANGELOG.md
+1
-0
| @@ | @@ -1,5 +1,6 @@ |
| ## 1.0.3 [unreleased] | |
| + | - Added `geocode` option |
| - Fixed association mismatch | |
| ## 1.0.2 | |
README.md
+20
-0
| @@ | @@ -329,6 +329,26 @@ class ApplicationController < ActionController::Base |
| end | |
| ``` | |
| + | ### Geocoding [master] |
| + | |
| + | By default, geocoding is performed inline. For performance, move it to the background. |
| + | |
| + | ```ruby |
| + | gem 'activejob_backport' |
| + | ``` |
| + | |
| + | And set: |
| + | |
| + | ```ruby |
| + | Ahoy.geocode = :async |
| + | ``` |
| + | |
| + | Or disable it with: |
| + | |
| + | ```ruby |
| + | Ahoy.geocode = false |
| + | ``` |
| + | |
| ### Track Visits Immediately | |
| Visitor and visit ids are generated on the first request (so you can use them immediately), but the `track_visit` method isn’t called until the JavaScript library posts to the server. This prevents browsers with cookies disabled from creating multiple visits and ensures visits are not created for API endpoints. Change this with: | |
ahoy.rb b/lib/ahoy.rb
+11
-0
| @@ | @@ -24,6 +24,14 @@ require "ahoy/stores/mongoid_store" |
| require "ahoy/engine" | |
| require "ahoy/warden" if defined?(Warden) | |
| + | # background jobs |
| + | begin |
| + | require "active_job" |
| + | rescue LoadError |
| + | # do nothing |
| + | end |
| + | require "ahoy/geocode_job" if defined?(ActiveJob) |
| + | |
| # deprecated | |
| require "ahoy/subscribers/active_record" | |
| @@ | @@ -44,6 +52,9 @@ module Ahoy |
| mattr_accessor :quiet | |
| self.quiet = true | |
| + | mattr_accessor :geocode |
| + | self.geocode = true |
| + | |
| def self.ensure_uuid(id) | |
| valid = UUIDTools::UUID.parse(id) rescue nil | |
| if valid | |
ahoy/geocode_job.rb b/lib/ahoy/geocode_job.rb
+14
-0
| @@ | @@ -0,0 +1,14 @@ |
| + | module Ahoy |
| + | class GeocodeJob < ActiveJob::Base |
| + | queue_as :ahoy |
| + | |
| + | def perform(visit) |
| + | deckhand = Deckhands::LocationDeckhand.new(visit.ip) |
| + | Ahoy::VisitProperties::LOCATION_KEYS.each do |key| |
| + | visit.send(:"#{key}=", deckhand.send(key)) if visit.respond_to?(:"#{key}=") |
| + | end |
| + | visit.save! |
| + | end |
| + | |
| + | end |
| + | end |
ahoy/stores/active_record_store.rb b/lib/ahoy/stores/active_record_store.rb
+2
-3
| @@ | @@ -11,14 +11,13 @@ module Ahoy |
| v.started_at = options[:started_at] | |
| end | |
| - | visit_properties.keys.each do |key| |
| - | visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") |
| - | end |
| + | set_visit_properties(visit) |
| yield(visit) if block_given? | |
| begin | |
| visit.save! | |
| + | geocode(visit) |
| rescue ActiveRecord::RecordNotUnique | |
| # do nothing | |
| end | |
ahoy/stores/active_record_token_store.rb b/lib/ahoy/stores/active_record_token_store.rb
+2
-3
| @@ | @@ -11,14 +11,13 @@ module Ahoy |
| v.created_at = options[:started_at] | |
| end | |
| - | visit_properties.keys.each do |key| |
| - | visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") |
| - | end |
| + | set_visit_properties(visit) |
| yield(visit) if block_given? | |
| begin | |
| visit.save! | |
| + | geocode(visit) |
| rescue ActiveRecord::RecordNotUnique | |
| # do nothing | |
| end | |
ahoy/stores/base_store.rb b/lib/ahoy/stores/base_store.rb
+14
-0
| @@ | @@ -64,6 +64,20 @@ module Ahoy |
| ahoy.visit_properties | |
| end | |
| + | def set_visit_properties(visit) |
| + | keys = visit_properties.keys |
| + | keys -= Ahoy::VisitProperties::LOCATION_KEYS if Ahoy.geocode != true |
| + | keys.each do |key| |
| + | visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") && visit_properties[key] |
| + | end |
| + | end |
| + | |
| + | def geocode(visit) |
| + | if Ahoy.geocode == :async |
| + | Ahoy::GeocodeJob.perform_later(visit) |
| + | end |
| + | end |
| + | |
| end | |
| end | |
| end | |
ahoy/stores/mongoid_store.rb b/lib/ahoy/stores/mongoid_store.rb
+2
-3
| @@ | @@ -11,13 +11,12 @@ module Ahoy |
| v.started_at = options[:started_at] | |
| end | |
| - | visit_properties.keys.each do |key| |
| - | visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") && visit_properties[key] |
| - | end |
| + | set_visit_properties(visit) |
| yield(visit) if block_given? | |
| visit.upsert | |
| + | geocode(visit) |
| end | |
| def track_event(name, properties, options, &block) | |