Class: Tumugi::Task

Inherits:
Object
  • Object
show all
Includes:
Mixin::Listable, Mixin::Parameterizable, Mixin::TaskHelper
Defined in:
lib/tumugi/task.rb

Direct Known Subclasses

Plugin::ExternalTask

Constant Summary collapse

AVAILABLE_STATES =
[
  :pending,
  :running,
  :completed,
  :skipped,
  :failed,
  :requires_failed,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::TaskHelper

#target

Methods included from Mixin::Listable

#list

Methods included from Mixin::Parameterizable

#configure, included, #validate_params

Constructor Details

#initializeTask

Returns a new instance of Task.



23
24
25
26
27
28
29
30
31
# File 'lib/tumugi/task.rb', line 23

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
end

Instance Attribute Details

#max_retryObject (readonly)

Returns the value of attribute max_retry.



12
13
14
# File 'lib/tumugi/task.rb', line 12

def max_retry
  @max_retry
end

#retry_intervalObject (readonly)

Returns the value of attribute retry_interval.



12
13
14
# File 'lib/tumugi/task.rb', line 12

def retry_interval
  @retry_interval
end

#triesObject (readonly)

Returns the value of attribute tries.



12
13
14
# File 'lib/tumugi/task.rb', line 12

def tries
  @tries
end

#visible_atObject (readonly)

Returns the value of attribute visible_at.



12
13
14
# File 'lib/tumugi/task.rb', line 12

def visible_at
  @visible_at
end

Instance Method Details

#_outputObject



166
167
168
# File 'lib/tumugi/task.rb', line 166

def _output
  @_output ||= output
end

#_requiresObject

Following methods are internal use only



162
163
164
# File 'lib/tumugi/task.rb', line 162

def _requires
  @_requires ||= requires
end

#completed?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/tumugi/task.rb', line 84

def completed?
  outputs = list(output)
  if outputs.empty?
    success?
  else
    outputs.all?(&:exist?)
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/tumugi/task.rb', line 41

def eql?(other)
  self.hash == other.hash
end

#finished?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
# File 'lib/tumugi/task.rb', line 110

def finished?
  case state
  when :completed, :skipped, :failed, :requires_failed
    true
  else
    false
  end
end

#hashObject



45
46
47
# File 'lib/tumugi/task.rb', line 45

def hash
  self.id.hash
end

#idObject



33
34
35
# File 'lib/tumugi/task.rb', line 33

def id
  @id ||= self.class.name
end

#id=(s) ⇒ Object



37
38
39
# File 'lib/tumugi/task.rb', line 37

def id=(s)
  @id = s
end

#inputObject



66
67
68
# File 'lib/tumugi/task.rb', line 66

def input
  @input ||= _input
end

#instanceObject



49
50
51
# File 'lib/tumugi/task.rb', line 49

def instance
  self
end

#log(msg) ⇒ Object



57
58
59
# File 'lib/tumugi/task.rb', line 57

def log(msg)
  logger.info(msg)
end

#loggerObject



53
54
55
# File 'lib/tumugi/task.rb', line 53

def logger
  @logger ||= Tumugi::ScopedLogger.new(->{"Thread-#{Thread.list.index {|t| t == Thread.current}}: #{id}"})
end

#outputObject

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



72
73
74
# File 'lib/tumugi/task.rb', line 72

def output
  []
end

#ready?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/tumugi/task.rb', line 80

def ready?
  list(_requires).all? { |t| t.instance.completed? }
end

#requiresObject

If you need to define task dependencies, override in subclass



62
63
64
# File 'lib/tumugi/task.rb', line 62

def requires
  []
end

#requires_failed?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/tumugi/task.rb', line 93

def requires_failed?
  list(_requires).any? { |t| t.instance.finished? && !t.instance.success? }
end

#retryObject



123
124
125
126
127
# File 'lib/tumugi/task.rb', line 123

def retry
  @tries += 1
  @visible_at += @retry_interval
  retriable?
end

#runObject

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/tumugi/task.rb', line 76

def run
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

#runnable?(now) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/tumugi/task.rb', line 97

def runnable?(now)
  ready? && visible?(now)
end

#stateObject



129
130
131
# File 'lib/tumugi/task.rb', line 129

def state
  @lock.synchronize { @state }
end

#success?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'lib/tumugi/task.rb', line 101

def success?
  case state
  when :completed, :skipped
    true
  else
    false
  end
end

#timeoutObject



119
120
121
# File 'lib/tumugi/task.rb', line 119

def timeout
  nil # meaning use default timeout
end

#trigger!(event) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tumugi/task.rb', line 133

def trigger!(event)
  @lock.synchronize do
    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

    @state = s
  end
end