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
Proc to be invoked to perform work
Parameters to pass to handler proc
Thread running the job
Time job completed, if nil job hasn’t completed yet
Time job started, if nil job hasn’t started yet
Public Class Methods
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
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
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
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
Return bool indicating if job has started
# File lib/rjr/util/thread_pool.rb, line 42 def started? !@time_started.nil? end