Class: Thread::Pool::Task
- Inherits:
-
Object
- Object
- Thread::Pool::Task
- Defined in:
- lib/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
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#execute ⇒ Object
Execute the task.
- #finished? ⇒ Boolean
-
#initialize(pool, *args, &block) ⇒ Task
constructor
Create a task in the given pool which will pass the arguments to the block.
-
#raise(exception) ⇒ Object
Raise an exception in the thread used by the task.
- #running? ⇒ Boolean
-
#terminate!(exception = Asked) ⇒ Object
Terminate the exception with an optionally given exception.
- #terminated? ⇒ Boolean
-
#timeout! ⇒ Object
Force the task to timeout.
- #timeout? ⇒ Boolean
-
#timeout_after(time) ⇒ Object
Timeout the task after the given time.
Constructor Details
#initialize(pool, *args, &block) ⇒ Task
Create a task in the given pool which will pass the arguments to the block.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/thread/pool.rb', line 29 def initialize(pool, *args, &block) @pool = pool @arguments = args @block = block @running = false @finished = false @timedout = false @terminated = false end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
25 26 27 |
# File 'lib/thread/pool.rb', line 25 def exception @exception end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
25 26 27 |
# File 'lib/thread/pool.rb', line 25 def pool @pool end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
25 26 27 |
# File 'lib/thread/pool.rb', line 25 def result @result end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
25 26 27 |
# File 'lib/thread/pool.rb', line 25 def started_at @started_at end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
25 26 27 |
# File 'lib/thread/pool.rb', line 25 def thread @thread end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
25 26 27 |
# File 'lib/thread/pool.rb', line 25 def timeout @timeout end |
Instance Method Details
#execute ⇒ Object
Execute the task.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/thread/pool.rb', line 57 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
44 45 46 |
# File 'lib/thread/pool.rb', line 44 def finished? @finished end |
#raise(exception) ⇒ Object
Raise an exception in the thread used by the task.
85 86 87 |
# File 'lib/thread/pool.rb', line 85 def raise(exception) @thread.raise(exception) end |
#running? ⇒ Boolean
40 41 42 |
# File 'lib/thread/pool.rb', line 40 def running? @running end |
#terminate!(exception = Asked) ⇒ Object
Terminate the exception with an optionally given exception.
90 91 92 93 94 95 96 97 98 |
# File 'lib/thread/pool.rb', line 90 def terminate!(exception = Asked) return if terminated? || finished? || timeout? @terminated = true return unless running? self.raise exception end |
#terminated? ⇒ Boolean
52 53 54 |
# File 'lib/thread/pool.rb', line 52 def terminated? @terminated end |
#timeout! ⇒ Object
Force the task to timeout.
101 102 103 |
# File 'lib/thread/pool.rb', line 101 def timeout! terminate! Timeout end |
#timeout? ⇒ Boolean
48 49 50 |
# File 'lib/thread/pool.rb', line 48 def timeout? @timedout end |
#timeout_after(time) ⇒ Object
Timeout the task after the given time.
106 107 108 109 110 111 112 |
# File 'lib/thread/pool.rb', line 106 def timeout_after(time) @timeout = time pool.__send__ :timeout_for, self, time self end |