lib/intercom-rails/import.rb in intercom-rails-0.2.19 vs lib/intercom-rails/import.rb in intercom-rails-0.2.20
- old
+ new
@@ -13,18 +13,19 @@
def self.run(*args)
new(*args).run
end
- attr_reader :uri, :http
+ attr_reader :uri, :http, :max_batch_size
attr_accessor :failed, :total_sent
def initialize(options = {})
@uri = Import.bulk_create_api_endpoint
@http = Net::HTTP.new(@uri.host, @uri.port)
@failed = []
@total_sent = 0
+ @max_batch_size = [(options[:max_batch_size] || 100), 100].min
@status_enabled = !!options[:status_enabled]
if uri.scheme == 'https'
http.use_ssl = true
@@ -56,11 +57,11 @@
end
def run
assert_runnable
- info "Sending users in batches of #{MAX_BATCH_SIZE}:"
+ info "Sending users in batches of #{max_batch_size}:"
batches do |batch, number_in_batch|
failures = send_users(batch)['failed']
self.failed += failures
self.total_sent += number_in_batch
@@ -76,21 +77,21 @@
def total_failed
self.failed.count
end
private
- MAX_BATCH_SIZE = 100
+
def batches
if active_record?(user_klass)
- user_klass.find_in_batches(:batch_size => MAX_BATCH_SIZE) do |users|
+ user_klass.find_in_batches(:batch_size => max_batch_size) do |users|
users_for_wire = map_to_users_for_wire(users)
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
end
elsif mongoid?(user_klass)
- 0.step(user_klass.all.count, MAX_BATCH_SIZE) do |offset|
- users_for_wire = map_to_users_for_wire(user_klass.limit(MAX_BATCH_SIZE).skip(offset))
+ 0.step(user_klass.all.count, max_batch_size) do |offset|
+ users_for_wire = map_to_users_for_wire(user_klass.limit(max_batch_size).skip(offset))
yield(prepare_batch(users_for_wire), users_for_wire.count) unless users_for_wire.count.zero?
end
end
end
@@ -143,10 +144,10 @@
response = http.request(request)
return response if successful_response?(response)
perform_request(request, attempts + 1, :failed_response => response)
- rescue Timeout::Error, Errno::ECONNREFUSED => e
+ rescue Timeout::Error, Errno::ECONNREFUSED, EOFError => e
perform_request(request, attempts + 1, :exception => e)
end
def successful_response?(response)
raise ImportError, "App ID or API Key are incorrect, please check them in config/initializers/intercom.rb" if response.code == '403'