Module: ThreadsHelper

Included in:
Mints::Contact, Mints::Pub, Mints::User
Defined in:
lib/mints/helpers/threads_helper.rb

Instance Method Summary collapse

Instance Method Details

#make_multiple_request(calls) ⇒ Object

[View source]

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mints/helpers/threads_helper.rb', line 5

def make_multiple_request(calls)
  set_threads_config
  payload_to_return = []
  now = Time.now
  calls = [calls] if !calls.kind_of?(Array) || (calls.kind_of?(Array) && calls.first.kind_of?(String))

  if @debug
    puts "min_threads_per_pool: #{@min_threads_per_pool}"
    puts "max_threads_per_pool: #{@max_threads_per_pool}"
    puts "max_threads_queue: #{@max_threads_queue}"
  end

  pool = Concurrent::ThreadPoolExecutor.new(
    min_threads: @min_threads_per_pool,
    max_threads: @max_threads_per_pool,
    max_queue: @max_threads_queue,
    fallback_policy: :caller_runs
  )

  calls.each_with_index do |call_data, index|
    pool.post do
      begin
        payload_to_return[index] = make_call(call_data)
      rescue => e
        payload_to_return[index] = e
      end
    end
  end

  # tell the pool to shutdown in an orderly fashion, allowing in progress work to complete
  pool.shutdown
  # now wait for all work to complete, wait as long as it takes
  pool.wait_for_termination

  if @debug
    end_time = Time.now
    puts "Time to make all calls: #{end_time - now}"
  end

  payload_to_return
end