class Thread::Pool::Task

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

Constants

Asked
Timeout

Attributes

exception[R]
pool[R]
result[R]
started_at[R]
thread[R]
timeout[R]

Public Class Methods

new(pool, *args, &block) click to toggle source

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

# 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

Public Instance Methods

execute() click to toggle source

Execute the task.

# 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?() click to toggle source
# File lib/thread/pool.rb, line 44
def finished?
        @finished
end
raise(exception) click to toggle source

Raise an exception in the thread used by the task.

# File lib/thread/pool.rb, line 85
def raise(exception)
        @thread.raise(exception) if @thread
end
running?() click to toggle source
# File lib/thread/pool.rb, line 40
def running?
        @running
end
terminate!(exception = Asked) click to toggle source

Terminate the exception with an optionally given exception.

# 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?() click to toggle source
# File lib/thread/pool.rb, line 52
def terminated?
        @terminated
end
timeout!() click to toggle source

Force the task to timeout.

# File lib/thread/pool.rb, line 101
def timeout!
        terminate! Timeout
end
timeout?() click to toggle source
# File lib/thread/pool.rb, line 48
def timeout?
        @timedout
end
timeout_after(time) click to toggle source

Timeout the task after the given time.

# File lib/thread/pool.rb, line 106
def timeout_after(time)
        @timeout = time

        pool.__send__ :timeout_for, self, time

        self
end