class Tumugi::Task

Constants

AVAILABLE_STATES

Attributes

elapsed_time[R]
max_retry[R]
retry_interval[R]
tries[R]
visible_at[R]

Public Class Methods

new() click to toggle source
Calls superclass method Tumugi::Mixin::Parameterizable::new
# File lib/tumugi/task.rb, line 26
def initialize
  super()
  @visible_at = Time.now
  @tries = 0
  @max_retry = Tumugi.config.max_retry
  @retry_interval = Tumugi.config.retry_interval
  @state = :pending
  @lock = Mutex.new
  @_elapsed_times = []
  @elapsed_time = '00:00:00'
end

Public Instance Methods

_output() click to toggle source
# File lib/tumugi/task.rb, line 209
def _output
  @_output ||= output
end
_requires() click to toggle source

Following methods are internal use only

# File lib/tumugi/task.rb, line 205
def _requires
  @_requires ||= requires
end
completed?() click to toggle source
# File lib/tumugi/task.rb, line 89
def completed?
  outputs = list(output)
  if outputs.empty?
    success?
  else
    outputs.all?(&:exist?)
  end
end
eql?(other) click to toggle source
# File lib/tumugi/task.rb, line 46
def eql?(other)
  self.hash == other.hash
end
failure?() click to toggle source
# File lib/tumugi/task.rb, line 136
def failure?
  case state
  when :failed, :requires_failed
    true
  else
    false
  end
end
finished?() click to toggle source
# File lib/tumugi/task.rb, line 145
def finished?
  success? or failure?
end
hash() click to toggle source
# File lib/tumugi/task.rb, line 50
def hash
  self.id.hash
end
id() click to toggle source
# File lib/tumugi/task.rb, line 38
def id
  @id ||= self.class.name
end
id=(s) click to toggle source
# File lib/tumugi/task.rb, line 42
def id=(s)
  @id = s
end
input() click to toggle source
# File lib/tumugi/task.rb, line 71
def input
  @input ||= _input
end
instance() click to toggle source
# File lib/tumugi/task.rb, line 54
def instance
  self
end
log(msg) click to toggle source
# File lib/tumugi/task.rb, line 62
def log(msg)
  logger.info(msg)
end
logger() click to toggle source
# File lib/tumugi/task.rb, line 58
def logger
  @logger ||= Tumugi::ScopedLogger.new(->{"Thread-#{Thread.list.index {|t| t == Thread.current}}: #{id}"})
end
output() click to toggle source

If you need to define output of task to skip alredy done task, override in subclass. If not, a task run always.

# File lib/tumugi/task.rb, line 77
def output
  []
end
ready?() click to toggle source
# File lib/tumugi/task.rb, line 85
def ready?
  list(_requires).all? { |t| t.instance.completed? }
end
requires() click to toggle source

If you need to define task dependencies, override in subclass

# File lib/tumugi/task.rb, line 67
def requires
  []
end
requires_failed?() click to toggle source
# File lib/tumugi/task.rb, line 98
def requires_failed?
  list(_requires).any? { |t| t.instance.finished? && !t.instance.success? }
end
retry() click to toggle source
# File lib/tumugi/task.rb, line 153
def retry
  @tries += 1
  @visible_at += @retry_interval
  retriable?
end
run() click to toggle source
# File lib/tumugi/task.rb, line 81
def run
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end
runnable?(now) click to toggle source
# File lib/tumugi/task.rb, line 102
def runnable?(now)
  ready? && visible?(now)
end
state() click to toggle source
# File lib/tumugi/task.rb, line 159
def state
  @lock.synchronize { @state }
end
success?() click to toggle source
# File lib/tumugi/task.rb, line 127
def success?
  case state
  when :completed, :skipped
    true
  else
    false
  end
end
timeout() click to toggle source
# File lib/tumugi/task.rb, line 149
def timeout
  nil # meaning use default timeout
end
trigger!(event) click to toggle source
# File lib/tumugi/task.rb, line 163
def trigger!(event)
  @lock.synchronize do
    now = Time.now
    @_elapsed_times[tries] ||= { start: now }

    s = case event
        when :skip
          :skipped
        when :start
          :running
        when :pend
          :pending
        when :complete
          :completed
        when :fail
          :failed
        when :requires_fail
          :requires_failed
        else
          raise Tumugi::TumugiError.new("Invalid event: #{event}")
        end

    if not AVAILABLE_STATES.include?(s)
      raise Tumugi::TumugiError.new("Invalid state: #{s}")
    end

    @_elapsed_times[tries][:end] = now
    @elapsed_time = human_readable_time((@_elapsed_times[tries][:end] - @_elapsed_times[tries][:start]).to_i)
    @state = s
  end
end

Private Instance Methods

_input() click to toggle source
# File lib/tumugi/task.rb, line 215
def _input
  if _requires.nil?
    []
  elsif _requires.is_a?(Array)
    _requires.map { |t| t.instance._output }
  elsif _requires.is_a?(Hash)
    Hash[_requires.map { |k, t| [k, t.instance._output] }]
  else
    _requires.instance._output
  end
end
retriable?() click to toggle source
# File lib/tumugi/task.rb, line 231
def retriable?
  @tries <= @max_retry
end
visible?(now) click to toggle source
# File lib/tumugi/task.rb, line 227
def visible?(now)
  now >= @visible_at
end