Module: FulfilApi::Relation::Batchable
- Included in:
- FulfilApi::Relation
- Defined in:
- lib/fulfil_api/relation/batchable.rb
Overview
The Batchable module includes a set of
helper/query methods that queries resources in Fulfil in batches.
Defined Under Namespace
Classes: RetryLimitExceeded
Instance Method Summary collapse
-
#find_each(batch_size: 500) {|FulfilApi::Resource| ... } ⇒ FulfilApi::Relation
The #find_each is a shorthand for iterating over individual API resources in a memory effective way.
-
#in_batches(of: 500, retries: :unlimited) {|FulfilApi::Relation| ... } ⇒ FulfilApi::Relation
Finds API resources in batches.
Instance Method Details
#find_each(batch_size: 500) {|FulfilApi::Resource| ... } ⇒ FulfilApi::Relation
The #find_each is a shorthand for iterating over individual API resources
in a memory effective way.
Under the hood, it uses the #in_batches to find API resources in batches
and process them efficiently.
27 28 29 30 31 |
# File 'lib/fulfil_api/relation/batchable.rb', line 27 def find_each(batch_size: 500, &block) in_batches(of: batch_size) do |batch| batch.each(&block) end end |
#in_batches(of: 500, retries: :unlimited) {|FulfilApi::Relation| ... } ⇒ FulfilApi::Relation
Note:
the #in_batches automatically retries when it encounters a 429 (TooManyRequests) HTTP error to ensure the lookup can be completed.
Finds API resources in batches. Defaults to the maximum number of resources
Fulfil's API endpoints will return (500 resources).
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/fulfil_api/relation/batchable.rb', line 52 def in_batches(of: 500, retries: :unlimited) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize current_retry = 0 current_offset = request_offset.presence || 0 batch_size = of loop do batch_relation = dup.offset(current_offset * batch_size).limit(batch_size) batch_relation.load yield(batch_relation) break unless batch_relation.size == batch_size current_offset += 1 current_retry = 0 # Reset the retries back to the default rescue FulfilApi::Error => e if e.details[:response_status] == 429 if retries != :unlimited && current_retry > retries raise RetryLimitExceeded, "the maximum number of #{retries} retries has been reached." end current_retry += 1 sleep 0.25 retry end raise e end self end |