Skip to content

Commit 1f71dd4

Browse files
committed
Handle FB truncating batch
1 parent 3c7037e commit 1f71dd4

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/koala/api/graph_batch_api.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def execute(http_options = {})
4545
return [] if batch_calls.empty?
4646

4747
batch_results = []
48-
batch_calls.each_slice(MAX_CALLS) do |batch|
48+
until batch_calls.empty? do
49+
batch = batch_calls.shift(MAX_CALLS)
50+
4951
# Turn the call args collected into what facebook expects
5052
args = {"batch" => batch_args(batch)}
5153
batch.each do |call|
@@ -55,7 +57,13 @@ def execute(http_options = {})
5557
original_api.graph_call("/", args, "post", http_options) do |response|
5658
raise bad_response if response.nil?
5759

58-
batch_results += generate_results(response, batch)
60+
# FB sometimes truncates the submission batch. dhm thinks it may be limiting create actions
61+
# without erroring
62+
slice_results = generate_results(response, batch)
63+
batch_results += slice_results
64+
if slice_results.length < batch.length
65+
batch_calls.unshift(*batch[slice_results.length .. batch.length])
66+
end
5967
end
6068
end
6169

spec/cases/graph_api_batch_spec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,17 @@
474474
end
475475

476476
describe '#big_batches' do
477+
before :all do
478+
@random = Random.new
479+
end
477480
before :each do
478481
payload = [{code: 200, headers: [{name: "Content-Type", value: "text/javascript; charset=UTF-8"}], body: "{\"id\":\"1234\"}"}]
479482
allow(Koala).to receive(:make_request) do |_request, args, _verb, _options|
480483
request_count = JSON.parse(args['batch']).length
481484
expect(request_count).to be <= 50 # check FB's limit
482-
Koala::HTTPService::Response.new(200, (payload * request_count).to_json, {})
485+
# simulate FB's result truncation
486+
response_count = request_count > 35 ? request_count - @random.rand(15) : request_count
487+
Koala::HTTPService::Response.new(200, (payload * response_count).to_json, {})
483488
end
484489
end
485490

0 commit comments

Comments
 (0)