Class: ParallelWorkforce::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_workforce/executor.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor_classes:, actor_args_array:, serial_execution_indexes: nil, execute_serially: nil, job_class: nil, execution_block: nil) ⇒ Executor

actor_classes: a single class or array of classes that can be instantiated with no args and have a ‘perform` method. If an array is passed, the array size must batch the actor_args_array size. Return results array with element from each actor in the order of the actor_args_array rubocop:disable Metrics/ParameterLists



19
20
21
22
23
24
25
26
27
# File 'lib/parallel_workforce/executor.rb', line 19

def initialize(actor_classes:, actor_args_array:,
    serial_execution_indexes: nil, execute_serially: nil, job_class: nil, execution_block: nil)
  @actor_classes = normalize_actor_classes!(actor_classes, actor_args_array)
  @actor_args_array = actor_args_array
  @serial_execution_indexes = serial_execution_indexes
  @execute_serially = execute_serially.nil? ? calculate_execute_serially : execute_serially
  @job_class = job_class || configuration.job_class
  @execution_block = execution_block
end

Instance Attribute Details

#actor_args_arrayObject (readonly)

Returns the value of attribute actor_args_array.



6
7
8
# File 'lib/parallel_workforce/executor.rb', line 6

def actor_args_array
  @actor_args_array
end

#actor_classesObject (readonly)

Returns the value of attribute actor_classes.



6
7
8
# File 'lib/parallel_workforce/executor.rb', line 6

def actor_classes
  @actor_classes
end

#execute_seriallyObject (readonly)

Returns the value of attribute execute_serially.



6
7
8
# File 'lib/parallel_workforce/executor.rb', line 6

def execute_serially
  @execute_serially
end

#job_classObject (readonly)

Returns the value of attribute job_class.



6
7
8
# File 'lib/parallel_workforce/executor.rb', line 6

def job_class
  @job_class
end

#serial_execution_indexesObject (readonly)

Returns the value of attribute serial_execution_indexes.



6
7
8
# File 'lib/parallel_workforce/executor.rb', line 6

def serial_execution_indexes
  @serial_execution_indexes
end

Instance Method Details

#perform_allObject

rubocop:enable Metrics/ParameterLists



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/parallel_workforce/executor.rb', line 30

def perform_all
  serial_execution_indexes = calculate_serial_execution_indexes
  serial_actor_classes, serial_actor_args_array, parallel_actor_classes, parallel_actor_args_array =
    split_serial_parallel(serial_execution_indexes, actor_classes, actor_args_array)

  serial_results = nil
  parallel_results = execute_actors(parallel_actor_classes, parallel_actor_args_array) do
    serial_results = serial_actor_classes.zip(serial_actor_args_array).collect do |actor_class, actor_args|
      execute_actor_serially(actor_class, actor_args)
    end
  end

  Array.new(actor_args_array.length) do |index|
    if index == serial_execution_indexes.first
      serial_execution_indexes.shift
      serial_results.shift
    else
      parallel_results.shift
    end
  end
end