Class: Thread::Pool::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/balotelli/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

Constructor Details

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

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



21
22
23
24
25
26
27
28
29
30
# File 'lib/balotelli/thread/pool.rb', line 21

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.



17
18
19
# File 'lib/balotelli/thread/pool.rb', line 17

def exception
  @exception
end

#poolObject (readonly)

Returns the value of attribute pool.



17
18
19
# File 'lib/balotelli/thread/pool.rb', line 17

def pool
  @pool
end

#resultObject (readonly)

Returns the value of attribute result.



17
18
19
# File 'lib/balotelli/thread/pool.rb', line 17

def result
  @result
end

#started_atObject (readonly)

Returns the value of attribute started_at.



17
18
19
# File 'lib/balotelli/thread/pool.rb', line 17

def started_at
  @started_at
end

#threadObject (readonly)

Returns the value of attribute thread.



17
18
19
# File 'lib/balotelli/thread/pool.rb', line 17

def thread
  @thread
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/balotelli/thread/pool.rb', line 17

def timeout
  @timeout
end

Instance Method Details

#executeObject

Execute the task.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/balotelli/thread/pool.rb', line 49

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

  @thread     = Thread.current
  @running    = true
  @started_at = Time.now

  pool.__send__ :wake_up_timeout

  begin
    @result = @block.call(*@arguments)
  rescue Exception => reason
    if reason.is_a? Timeout
      @timedout = true
    elsif reason.is_a? Asked
      return
    else
      @exception = reason
      raise @exception if Thread::Pool.abort_on_exception
    end
  end

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

#finished?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/balotelli/thread/pool.rb', line 36

def finished?
  @finished
end

#raise(exception) ⇒ Object

Raise an exception in the thread used by the task.



77
78
79
# File 'lib/balotelli/thread/pool.rb', line 77

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

#running?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/balotelli/thread/pool.rb', line 32

def running?
  @running
end

#terminate!(exception = Asked) ⇒ Object

Terminate the exception with an optionally given exception.



82
83
84
85
86
87
88
89
90
# File 'lib/balotelli/thread/pool.rb', line 82

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

  @terminated = true

  return unless running?

  self.raise exception
end

#terminated?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/balotelli/thread/pool.rb', line 44

def terminated?
  @terminated
end

#timeout!Object

Force the task to timeout.



93
94
95
# File 'lib/balotelli/thread/pool.rb', line 93

def timeout!
  terminate! Timeout
end

#timeout?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/balotelli/thread/pool.rb', line 40

def timeout?
  @timedout
end

#timeout_after(time) ⇒ Object

Timeout the task after the given time.



98
99
100
101
102
103
104
# File 'lib/balotelli/thread/pool.rb', line 98

def timeout_after(time)
  @timeout = time

  pool.__send__ :timeout_for, self, time

  self
end