Class: UltraMarathon::CollectionRunner

Inherits:
BaseRunner show all
Defined in:
lib/ultra_marathon/collection_runner.rb

Constant Summary

Constants inherited from BaseRunner

BaseRunner::RUN_INSTRUMENTATION_NAME

Instance Attribute Summary collapse

Attributes inherited from BaseRunner

#success

Instance Method Summary collapse

Methods inherited from BaseRunner

new, #reset, #run!, #run_instrumentation, #success?

Methods included from Contexticution

#contexticute

Methods included from Instrumentation

#instrumentations

Methods included from Logging

#logger

Constructor Details

#initialize(collection, options = {}, &run_block) ⇒ CollectionRunner

Takes a collection, each of which will be run in its own subrunner. The collection Also takes a number of options:

name: The name of the collection run block sub_name: A callable object (passed the index of the collection) that

should return a unique (to the collection) name for that subrunner
Defaults to :"#{options[:name]}__#{index}"

sub_runner: Class inheiriting from UltraMarathon::SubRunner in which each

run_block will be run
Defaults to UltraMarathon::SubRunner

iterator: Method called to iterate over collection. For example, a Rails

application may wish to use :find_each with an ActiveRecord::Relation
to batch queries
Defaults to :each

threaded: Run each iteration in its own thread



32
33
34
35
36
37
38
39
40
41
# File 'lib/ultra_marathon/collection_runner.rb', line 32

def initialize(collection, options={}, &run_block)
  @collection, @run_block = collection, run_block
  @name = options[:name]
  @options = {
    sub_name: proc { |index| :"#{options[:name]}__#{index}" },
    sub_runner: SubRunner,
    iterator:   :each,
    threaded: false
  }.merge(options)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



9
10
11
# File 'lib/ultra_marathon/collection_runner.rb', line 9

def collection
  @collection
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ultra_marathon/collection_runner.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/ultra_marathon/collection_runner.rb', line 9

def options
  @options
end

#run_blockObject (readonly)

Returns the value of attribute run_block.



9
10
11
# File 'lib/ultra_marathon/collection_runner.rb', line 9

def run_block
  @run_block
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ultra_marathon/collection_runner.rb', line 60

def complete?
  unrun_sub_runners.empty? && running_sub_runners.empty?
end

#parentsObject

Set of all sub runners that should be run before this one. This class cannot do anything with this information, but it is useful to the enveloping runner.



67
68
69
# File 'lib/ultra_marathon/collection_runner.rb', line 67

def parents
  @parents ||= Set.new(options[:requires])
end

#threaded?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ultra_marathon/collection_runner.rb', line 56

def threaded?
  false
end

#unrun_sub_runnersObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ultra_marathon/collection_runner.rb', line 43

def unrun_sub_runners
  @unrun_sub_runners ||= begin
    store = Store.new
    index = 0
    collection.send(options[:iterator]) do |item|
      this_index = index
      index += 1
      store << new_item_sub_runner(item, this_index)
    end
    store
  end
end