Skip to content
Snippets Groups Projects
Commit 77c6f99f authored by Alex Reisner's avatar Alex Reisner Committed by GitHub
Browse files

Merge pull request #1169 from fleveque/limit-rake-task

Allow to limit the objects that will be geocoded by rake task
parents 4d7d8dbc f494555f
Branches
Tags
No related merge requests found
...@@ -114,6 +114,10 @@ Geocoder will print warnings if you exceed the rate limit for your geocoding ser ...@@ -114,6 +114,10 @@ Geocoder will print warnings if you exceed the rate limit for your geocoding ser
rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100 rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100
To avoid per-day limit issues (for example if you are trying to geocode thousands of objects and don't want to reach the limit), you can add a `LIMIT` option. Warning: This will ignore the `BATCH` value if provided.
rake geocode:all CLASS=YourModel LIMIT=1000
### Avoiding Unnecessary API Requests ### Avoiding Unnecessary API Requests
Geocoding only needs to be performed under certain conditions. To avoid unnecessary work (and quota usage) you will probably want to geocode an object only when: Geocoding only needs to be performed under certain conditions. To avoid unnecessary work (and quota usage) you will probably want to geocode an object only when:
......
...@@ -7,6 +7,7 @@ namespace :geocode do ...@@ -7,6 +7,7 @@ namespace :geocode do
sleep_timer = ENV['SLEEP'] || ENV['sleep'] sleep_timer = ENV['SLEEP'] || ENV['sleep']
batch = ENV['BATCH'] || ENV['batch'] batch = ENV['BATCH'] || ENV['batch']
reverse = ENV['REVERSE'] || ENV['reverse'] reverse = ENV['REVERSE'] || ENV['reverse']
limit = ENV['LIMIT'] || ENV['limit']
raise "Please specify a CLASS (model)" unless class_name raise "Please specify a CLASS (model)" unless class_name
klass = class_from_string(class_name) klass = class_from_string(class_name)
batch = (batch.to_i unless batch.nil?) || 1000 batch = (batch.to_i unless batch.nil?) || 1000
...@@ -20,15 +21,22 @@ namespace :geocode do ...@@ -20,15 +21,22 @@ namespace :geocode do
} }
scope = reverse ? klass.not_reverse_geocoded : klass.not_geocoded scope = reverse ? klass.not_reverse_geocoded : klass.not_geocoded
scope = scope.limit(limit) if limit
if orm == 'mongoid' if orm == 'mongoid'
scope.each do |obj| scope.each do |obj|
geocode_record.call(obj) geocode_record.call(obj)
end end
elsif orm == 'active_record' elsif orm == 'active_record'
if limit
scope.each do |obj|
geocode_record.call(obj)
end
else
scope.find_each(batch_size: batch) do |obj| scope.find_each(batch_size: batch) do |obj|
geocode_record.call(obj) geocode_record.call(obj)
end end
end end
end
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment