Class: Thread::Pool::Task

Inherits:
Object show all
Defined in:
lib/storyboard/thread-util.rb

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

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

#exceptionObject (readonly)

Returns the value of attribute exception.



17
18
19
# File 'lib/storyboard/thread-util.rb', line 17

def exception
  @exception
end

#poolObject (readonly)

Returns the value of attribute pool.



17
18
19
# File 'lib/storyboard/thread-util.rb', line 17

def pool
  @pool
end

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

#threadObject (readonly)

Returns the value of attribute thread.



17
18
19
# File 'lib/storyboard/thread-util.rb', line 17

def thread
  @thread
end

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

Returns:

  • (Boolean)


26
# File 'lib/storyboard/thread-util.rb', line 26

def finished?;   @finished;   end

#running?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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