Class: Salus::ThreadPool::Task

Inherits:
Object
  • Object
show all
Includes:
Logging, Observable
Defined in:
lib/salus/thread/pool.rb

Overview

A task incapsulates a block being ran by the pool and the arguments to pass to it.

Constant Summary collapse

Timeout =
Class.new(Exception)
Asked =
Class.new(Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Methods included from Observable

#add_observer, #count_observers, #delete_observer, #delete_observers, #notify_and_delete_observers, #notify_observers, #with_observer

Constructor Details

#initialize(pool, *args, &block) ⇒ Task

Create a task in the given pool which will pass the arguments to the block.



32
33
34
35
36
37
38
39
40
41
# File 'lib/salus/thread/pool.rb', line 32

def initialize(pool, *args, &block)
  @pool      = pool
  @arguments = args
  @block     = block

  @running    = false
  @finished   = false
  @timedout   = false
  @terminated = false
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



28
29
30
# File 'lib/salus/thread/pool.rb', line 28

def exception
  @exception
end

#poolObject (readonly)

Returns the value of attribute pool.



28
29
30
# File 'lib/salus/thread/pool.rb', line 28

def pool
  @pool
end

#resultObject (readonly)

Returns the value of attribute result.



28
29
30
# File 'lib/salus/thread/pool.rb', line 28

def result
  @result
end

#started_atObject (readonly)

Returns the value of attribute started_at.



28
29
30
# File 'lib/salus/thread/pool.rb', line 28

def started_at
  @started_at
end

#threadObject (readonly)

Returns the value of attribute thread.



28
29
30
# File 'lib/salus/thread/pool.rb', line 28

def thread
  @thread
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



28
29
30
# File 'lib/salus/thread/pool.rb', line 28

def timeout
  @timeout
end

Instance Method Details

#executeObject

Execute the task.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/salus/thread/pool.rb', line 60

def execute
  return if terminated? || running? || finished?

  @thread     = Thread.current
  @running    = true
  @result     = nil
  @started_at = MonotonicTime.get

  pool.__send__ :wake_up_timeout

  begin
    @result = @block.call(*@arguments)
    notify_observers(Time.now, @result, nil)
  rescue Exception => reason
    notify_observers(Time.now, nil, reason)
    if reason.is_a? Timeout
      @timedout = true
      log DEBUG, reason
    elsif reason.is_a? Asked
      log DEBUG, reason
      return
    else
      @exception = reason
      log ERROR, reason
      raise @exception if ThreadPool.abort_on_exception
    end
  end

  @running  = false
  @finished = true
  @thread   = nil
end

#finished?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/salus/thread/pool.rb', line 47

def finished?
  @finished
end

#raise(exception) ⇒ Object

Raise an exception in the thread used by the task.



94
95
96
# File 'lib/salus/thread/pool.rb', line 94

def raise(exception)
  @thread.raise(exception) if @thread
end

#running?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/salus/thread/pool.rb', line 43

def running?
  @running
end

#terminate!(exception = Asked) ⇒ Object

Terminate the exception with an optionally given exception.



99
100
101
102
103
104
105
106
107
# File 'lib/salus/thread/pool.rb', line 99

def terminate!(exception = Asked)
  return if terminated? || finished? || timeout?

  @terminated = true

  return unless running?

  self.raise exception
end

#terminated?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/salus/thread/pool.rb', line 55

def terminated?
  @terminated
end

#timeout!Object

Force the task to timeout.



110
111
112
# File 'lib/salus/thread/pool.rb', line 110

def timeout!
  terminate! Timeout
end

#timeout?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/salus/thread/pool.rb', line 51

def timeout?
  @timedout
end

#timeout_after(time) ⇒ Object

Timeout the task after the given time.



115
116
117
118
119
120
121
# File 'lib/salus/thread/pool.rb', line 115

def timeout_after(time)
  @timeout = time

  pool.__send__ :timeout_for, self, time

  self
end