Class: Workers::Task

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/workers/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @logger = Workers::LogProxy.new(options[:logger])
  @args = options[:args] || []
  @perform = options[:perform] || raise('Perform callback is required.')
  @finished = options[:finished]
  @state = :initialized

  return nil
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/workers/task.rb', line 5

def args
  @args
end

#exceptionObject (readonly)

Returns the value of attribute exception.



7
8
9
# File 'lib/workers/task.rb', line 7

def exception
  @exception
end

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/workers/task.rb', line 6

def result
  @result
end

#stateObject (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

Returns:

  • (Boolean)


42
43
44
# File 'lib/workers/task.rb', line 42

def failed?
  return @state == :failed
end

#runObject



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

Returns:

  • (Boolean)


38
39
40
# File 'lib/workers/task.rb', line 38

def succeeded?
  return @state == :succeeded
end