Class: UltraMarathon::SubRunner

Inherits:
Object
  • Object
show all
Includes:
Callbacks, Instrumentation, Logging
Defined in:
lib/ultra_marathon/sub_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Methods included from Instrumentation

#instrumentations

Methods included from Contexticution

#contexticute

Constructor Details

#initialize(options, run_block_or_sub_context) ⇒ SubRunner

The :context option is required, because you’ll never want to run a SubRunner in context of itself. SubContext is necessary because we want to run in the context of the other class, but do other things (like log) in the context of this one.

Parameters:

  • options (Hash)

    the options for instantiation

  • run_block_or_sub_context (Proc, SubContext)

    either a proc to be run within a new SubContext

Options Hash (options):

  • :name (String)

    The name of the sub runner

  • :context (Object)

    The context in which the run block will be run. Only required if second parameter is a callable object and not a SubContext already.

  • :instrument (Boolean) — default: false

    whether to log instrumentation information.

  • :threaded (Boolean) — default: false

    whether to run in a separate thread.

  • :requires (Array, Set) — default: []

    the names of sub runners that should have successfully run before this one. Not used by this class but necessary state for enveloping runners.



47
48
49
50
51
52
53
54
55
56
# File 'lib/ultra_marathon/sub_runner.rb', line 47

def initialize(options, run_block_or_sub_context)
  @run_block_or_sub_context = run_block_or_sub_context
  @name = options.delete(:name)
  @options = {
    instrument: false,
    threaded: false,
    requires: Set.new,
    timeout: 100
  }.merge(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/ultra_marathon/sub_runner.rb', line 14

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/ultra_marathon/sub_runner.rb', line 14

def options
  @options
end

#run_block_or_sub_contextObject (readonly)

Returns the value of attribute run_block_or_sub_context.



14
15
16
# File 'lib/ultra_marathon/sub_runner.rb', line 14

def run_block_or_sub_context
  @run_block_or_sub_context
end

#run_threadObject

Returns the value of attribute run_thread.



13
14
15
# File 'lib/ultra_marathon/sub_runner.rb', line 13

def run_thread
  @run_thread
end

#successObject

Returns the value of attribute success.



13
14
15
# File 'lib/ultra_marathon/sub_runner.rb', line 13

def success
  @success
end

Instance Method Details

#complete?Boolean

Tells whether the runner has completed. If running in a threaded context, checks if the thread is alive. Otherwise, returns true.

Returns:

  • (Boolean)

    whether the runner has compeleted running



74
75
76
77
78
79
80
# File 'lib/ultra_marathon/sub_runner.rb', line 74

def complete?
  if threaded?
    !running?
  else
    true
  end
end

#parentsSet

Set of all sub runners that should be run before this one, as specified by the :requires option.

Returns:

  • (Set)

    set of all runner names that should be run before this one.



107
108
109
# File 'lib/ultra_marathon/sub_runner.rb', line 107

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

#resetself

Invokes all on_reset callbacks

Returns:

  • (self)


99
100
101
102
# File 'lib/ultra_marathon/sub_runner.rb', line 99

def reset
  invoke_on_reset_callbacks
  self
end

#run!self

Run the run block or sub context. If #threaded? is true, will envelope the run in a thread and immediately return. If there is already an active thread, it will not run again.

Returns:

  • (self)


62
63
64
65
66
67
68
69
# File 'lib/ultra_marathon/sub_runner.rb', line 62

def run!
  if threaded?
    run_in_thread
  else
    run_without_thread
  end
  self
end

#running?Boolean

If #threaded?, returns if the run_thread is alive. Otherwise false.

Returns:

  • (Boolean)

    whether the SubRunner is currently executing #run!



84
85
86
87
88
89
90
# File 'lib/ultra_marathon/sub_runner.rb', line 84

def running?
  if threaded?
    run_thread && run_thread.alive?
  else
    false
  end
end

#threaded?Boolean

Returns whether #run! will be executed in a thread.

Returns:

  • (Boolean)

    whether #run! will be executed in a thread.



93
94
95
# File 'lib/ultra_marathon/sub_runner.rb', line 93

def threaded?
  !!options[:threaded]
end