Class: Processor::ProcessRunner::Threads

Inherits:
Object
  • Object
show all
Defined in:
lib/processor/process_runner/threads.rb

Direct Known Subclasses

Successive

Instance Method Summary collapse

Constructor Details

#initialize(number_of_threads = 1) ⇒ Threads



4
5
6
7
# File 'lib/processor/process_runner/threads.rb', line 4

def initialize(number_of_threads = 1)
  @number_of_threads = number_of_threads
  @threads = []
end

Instance Method Details

#call(processor, events, recursion_preventer) ⇒ Object



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
# File 'lib/processor/process_runner/threads.rb', line 9

def call(processor, events, recursion_preventer)
  join_threads

  begin
    processor.records.each do |record|
      recursion_preventer.call
      if threads_created >= number_of_threads then join_threads end

      new_thread(processor, record) do |thread_data_processor, thread_record|
        begin
          events.register :before_record_processing, thread_record

          result = thread_data_processor.process(thread_record)

          events.register :after_record_processing, thread_record, result
        rescue StandardError => exception
          events.register :record_processing_error, thread_record, exception
        end
      end

    end
  ensure # join already created threads
    join_threads
  end
end