Class: Workers::Task
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
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.
10 11 12 13 14 15 16 17 18 |
# File 'lib/workers/task.rb', line 10 def initialize( = {}) @logger = Workers::LogProxy.new([:logger]) @args = [:args] || [] @perform = [:perform] || raise('Perform callback is required.') @finished = [:finished] @state = :initialized return nil end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
5 6 7 |
# File 'lib/workers/task.rb', line 5 def args @args end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
7 8 9 |
# File 'lib/workers/task.rb', line 7 def exception @exception 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 |
Instance Method Details
#failed? ⇒ Boolean
42 43 44 |
# File 'lib/workers/task.rb', line 42 def failed? return @state == :failed end |
#run ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/workers/task.rb', line 20 def run raise "Invalid state (#{@state})." unless @state == :initialized @state = :running begin @result = @perform.call(*@args) @state = :succeeded rescue Exception => e @state = :failed @exception = e end @finished.call(self) return nil end |
#succeeded? ⇒ Boolean
38 39 40 |
# File 'lib/workers/task.rb', line 38 def succeeded? return @state == :succeeded end |