Class: Workers::Task
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#max_tries ⇒ Object
readonly
Returns the value of attribute max_tries.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#tries ⇒ Object
readonly
Returns the value of attribute tries.
Instance Method Summary collapse
- #failed? ⇒ Boolean
-
#initialize(options = {}) ⇒ Task
constructor
A new instance of Task.
- #run ⇒ Object
- #succeeded? ⇒ Boolean
Methods included from Helpers
#concat_e, #log_debug, #log_error, #log_info, #log_warn
Constructor Details
#initialize(options = {}) ⇒ Task
Returns a new instance of Task.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/workers/task.rb', line 12 def initialize( = {}) @logger = Workers::LogProxy.new([:logger]) @input = [:input] || [] @perform = [:perform] || raise('Perform callback is required.') @finished = [:finished] @max_tries = [:max_tries] || 1 @state = :initialized @tries = 0 raise 'max_tries must be >= 1' unless @max_tries >= 1 return nil end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
7 8 9 |
# File 'lib/workers/task.rb', line 7 def exception @exception end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
5 6 7 |
# File 'lib/workers/task.rb', line 5 def input @input end |
#max_tries ⇒ Object (readonly)
Returns the value of attribute max_tries.
9 10 11 |
# File 'lib/workers/task.rb', line 9 def max_tries @max_tries end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
6 7 8 |
# File 'lib/workers/task.rb', line 6 def result @result end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
8 9 10 |
# File 'lib/workers/task.rb', line 8 def state @state end |
#tries ⇒ Object (readonly)
Returns the value of attribute tries.
10 11 12 |
# File 'lib/workers/task.rb', line 10 def tries @tries end |
Instance Method Details
#failed? ⇒ Boolean
53 54 55 |
# File 'lib/workers/task.rb', line 53 def failed? return @state == :failed end |
#run ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/workers/task.rb', line 26 def run raise "Invalid state (#{@state})." unless @state == :initialized @state = :running while(@tries < @max_tries && @state != :succeeded) @tries += 1 begin @result = @perform.call(@input) @state = :succeeded @exception = nil rescue Exception => e @state = :failed @exception = e end end @finished.call(self) return nil end |
#succeeded? ⇒ Boolean
49 50 51 |
# File 'lib/workers/task.rb', line 49 def succeeded? return @state == :succeeded end |