class RJR::ThreadPoolJob

Work item to be executed in a thread launched by {ThreadPool}.

The end user should initialize this class with a handle to the job to be executed and the params to pass to it, then hand the instance off to the thread pool to take care of the rest.

Attributes

handler[RW]

Proc to be invoked to perform work

params[RW]

Parameters to pass to handler proc

thread[RW]

Thread running the job

time_completed[RW]

Time job completed, if nil job hasn’t completed yet

time_started[RW]

Time job started, if nil job hasn’t started yet

Public Class Methods

new(*params, &block) click to toggle source

ThreadPoolJob initializer @param [Array] params arguments to pass to the job when it is invoked @param [Callable] block handle to callable object corresponding to job to invoke

# File lib/rjr/util/thread_pool.rb, line 34
def initialize(*params, &block)
  @params = params
  @handler = block
  @being_executed = false
  @timestamp = nil
end

Public Instance Methods

completed?() click to toggle source

Return bool indicating if job has completed

# File lib/rjr/util/thread_pool.rb, line 47
def completed?
  !@time_started.nil? && !@time_completed.nil?
end
exec(lock) click to toggle source

Set job metadata and execute job with specified params.

Used internally by thread pool

# File lib/rjr/util/thread_pool.rb, line 60
def exec(lock)
  lock.synchronize {
    @thread = Thread.current
    @time_started = Time.now
  }

  @handler.call *@params

  # ensure we do not switch to another job
  # before atomic check expiration / terminate
  # expired threads happens below
  lock.synchronize {
    @time_completed = Time.now
    @thread = nil
  }
end
expired?(timeout) click to toggle source

Return bool indicating if the job has started but not completed and the specified timeout has expired

# File lib/rjr/util/thread_pool.rb, line 53
def expired?(timeout)
  !@time_started.nil? && @time_completed.nil? && ((Time.now - @time_started) > timeout)
end
started?() click to toggle source

Return bool indicating if job has started

# File lib/rjr/util/thread_pool.rb, line 42
def started?
  !@time_started.nil?
end