Class: FacebookAds::ServerSide::BatchProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_ads/ad_objects/server_side/batch_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch_size = 50, concurrent_requests = 4) ⇒ BatchProcessor

Initializes the object

Parameters:

  • batch_size (Integer) (defaults to: 50)

    The number of events to send in each request

  • concurrent_requests (Integer) (defaults to: 4)

    The number of threads to use when making requests concurrently



27
28
29
30
# File 'lib/facebook_ads/ad_objects/server_side/batch_processor.rb', line 27

def initialize(batch_size=50, concurrent_requests=4)
  self.batch_size = batch_size
  self.concurrent_requests = concurrent_requests
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



21
22
23
# File 'lib/facebook_ads/ad_objects/server_side/batch_processor.rb', line 21

def batch_size
  @batch_size
end

#concurrent_requestsObject

Returns the value of attribute concurrent_requests.



22
23
24
# File 'lib/facebook_ads/ad_objects/server_side/batch_processor.rb', line 22

def concurrent_requests
  @concurrent_requests
end

Instance Method Details

#process_event_requests(event_requests_async) ⇒ Object



32
33
34
35
36
37
# File 'lib/facebook_ads/ad_objects/server_side/batch_processor.rb', line 32

def process_event_requests(event_requests_async)
  generator = self.process_event_requests_generator(event_requests_async)
  generator.each do |batch|
    Concurrent::Promise.zip(*batch).execute.value!
  end
end

#process_event_requests_generator(event_requests_async) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/facebook_ads/ad_objects/server_side/batch_processor.rb', line 46

def process_event_requests_generator(event_requests_async)
  index = 0
  Enumerator.new do |generator|
    while index < event_requests_async.size do
      batch = event_requests_async[index, concurrent_requests].map(&:execute)
      generator.yield *[batch]
      index += concurrent_requests
    end
  end
end

#process_events(event_request_async_to_clone, events) ⇒ Object



39
40
41
42
43
44
# File 'lib/facebook_ads/ad_objects/server_side/batch_processor.rb', line 39

def process_events(event_request_async_to_clone, events)
  generator = self.process_events_generator(event_request_async_to_clone, events)
  generator.each do |batch|
    Concurrent::Promise.zip(*batch).execute.value!
  end
end

#process_events_generator(event_request_async_to_clone, events) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/facebook_ads/ad_objects/server_side/batch_processor.rb', line 57

def process_events_generator(event_request_async_to_clone, events)
  index = 0
  Enumerator.new do |generator|
    while index < events.size do
      batch = []
      while index < events.size && batch.size < concurrent_requests do
        event_request_async = event_request_async_to_clone.clone_without_events
        event_request_async.events = events[index, batch_size]
        batch << event_request_async
        index += batch_size
      end
      generator.yield *[batch.map(&:execute)]
    end
  end
end