Class: Thread::Pool::Task
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.
-
#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(thread) ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(pool, *args, &block) ⇒ Task
constructor
A new instance of Task.
- #running? ⇒ Boolean
- #terminate!(exception = Asked) ⇒ Object
- #terminated? ⇒ Boolean
- #timeout! ⇒ Object
- #timeout? ⇒ Boolean
- #timeout_after(time) ⇒ Object
Constructor Details
#initialize(pool, *args, &block) ⇒ Task
Returns a new instance of Task.
19 20 21 22 23 |
# File 'lib/storyboard/thread-util.rb', line 19 def initialize (pool, *args, &block) @pool = pool @arguments = args @block = block end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
17 18 19 |
# File 'lib/storyboard/thread-util.rb', line 17 def exception @exception end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
17 18 19 |
# File 'lib/storyboard/thread-util.rb', line 17 def pool @pool end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
17 18 19 |
# File 'lib/storyboard/thread-util.rb', line 17 def started_at @started_at end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
17 18 19 |
# File 'lib/storyboard/thread-util.rb', line 17 def thread @thread end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
17 18 19 |
# File 'lib/storyboard/thread-util.rb', line 17 def timeout @timeout end |
Instance Method Details
#execute(thread) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/storyboard/thread-util.rb', line 30 def execute (thread) return if terminated? || running? || finished? @thread = thread @running = true @started_at = Time.now pool.wake_up_timeout begin @block.call(*@arguments) rescue Exception => reason if reason.is_a? Timeout @timedout = true elsif reason.is_a? Asked return else @exception = reason end end @running = false @finished = true @thread = nil end |
#finished? ⇒ Boolean
26 |
# File 'lib/storyboard/thread-util.rb', line 26 def finished?; @finished; end |
#running? ⇒ Boolean
25 |
# File 'lib/storyboard/thread-util.rb', line 25 def running?; @running; end |
#terminate!(exception = Asked) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/storyboard/thread-util.rb', line 56 def terminate! (exception = Asked) return if terminated? || finished? || timeout? @terminated = true return unless running? @thread.raise exception end |
#terminated? ⇒ Boolean
28 |
# File 'lib/storyboard/thread-util.rb', line 28 def terminated?; @terminated; end |
#timeout! ⇒ Object
66 67 68 |
# File 'lib/storyboard/thread-util.rb', line 66 def timeout! terminate! Timeout end |
#timeout? ⇒ Boolean
27 |
# File 'lib/storyboard/thread-util.rb', line 27 def timeout?; @timedout; end |
#timeout_after(time) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/storyboard/thread-util.rb', line 70 def timeout_after (time) @timeout = time pool.timeout_for self, time self end |