module RubyExecutor

Constants

FALLBACK_POLICY

The set of possible fallback policies that may be set at thread pool creation.

Public Instance Methods

execute(*args, &task) click to toggle source
# File lib/garcon/task/executor.rb, line 256
def execute(*args, &task)
  raise NotImplementedError
end
init_executor() click to toggle source

Initialize the executor by creating and initializing all the internal synchronization objects.

# File lib/garcon/task/executor.rb, line 250
def init_executor
  @mutex         = Mutex.new
  @stop_event    = Event.new
  @stopped_event = Event.new
end
kill() click to toggle source

Begin an immediate shutdown. In-progress tasks will be allowed to complete but enqueued tasks will be dismissed and no new tasks will be accepted. Has no additional effect if the thread pool is not running.

# File lib/garcon/task/executor.rb, line 217
def kill
  mutex.synchronize do
    break if shutdown?
    stop_event.set
    kill_execution
    stopped_event.set
  end
  true
end
kill_execution() click to toggle source

Callback method called when the executor has been killed. The default behavior is to do nothing.

# File lib/garcon/task/executor.rb, line 270
def kill_execution
  # do nothing
end
running?() click to toggle source

Is the executor running?

@return [Boolean]

True when running, false when shutting down or shutdown.
# File lib/garcon/task/executor.rb, line 180
def running?
  ! stop_event.set?
end
shutdown() click to toggle source

Begin an orderly shutdown. Tasks already in the queue will be executed, but no new tasks will be accepted. Has no additional effect if the thread pool is not running.

# File lib/garcon/task/executor.rb, line 204
def shutdown
  mutex.synchronize do
    break unless running?
    stop_event.set
    shutdown_execution
  end
  true
end
shutdown?() click to toggle source

Is the executor shutdown?

@return [Boolean]

True when shutdown, false when shutting down or running.
# File lib/garcon/task/executor.rb, line 196
def shutdown?
  stopped_event.set?
end
shutdown_execution() click to toggle source

Callback method called when an orderly shutdown has completed. The default behavior is to signal all waiting threads.

# File lib/garcon/task/executor.rb, line 263
def shutdown_execution
  stopped_event.set
end
shuttingdown?() click to toggle source

Is the executor shuttingdown?

@return [Boolean]

True when not running and not shutdown, else false.
# File lib/garcon/task/executor.rb, line 188
def shuttingdown?
  ! (running? || shutdown?)
end
wait_for_termination(timeout = nil) click to toggle source

Block until executor shutdown is complete or until ‘timeout` seconds have passed.

@note

Does not initiate shutdown or termination. Either shutdown or kill must
be called before this method (or on another thread).

@param [Integer] timeout

The maximum number of seconds to wait for shutdown to complete

@return [Boolean]

True if shutdown complete or false on timeout.
# File lib/garcon/task/executor.rb, line 239
def wait_for_termination(timeout = nil)
  stopped_event.wait(timeout)
end

Private Instance Methods

<<(task) click to toggle source

Submit a task to the executor for asynchronous processing.

@param [Proc] task

the asynchronous task to perform

@return [self]

returns itself
# File lib/garcon/task/executor.rb, line 171
def <<(task)
  post(&task)
  self
end
post(*args, &task) click to toggle source

Submit a task to the executor for asynchronous processing.

@param [Array] args

Zero or more arguments to be passed to the task

@yield the asynchronous task to perform

@raise [ArgumentError]

if no task is given

@return [Boolean]

True if the task is queued, false if the executor is not running.
# File lib/garcon/task/executor.rb, line 154
def post(*args, &task)
  raise ArgumentError.new('no block given') unless block_given?
  mutex.synchronize do
    # If the executor is shut down, reject this task
    return handle_fallback(*args, &task) unless running?
    execute(*args, &task)
    true
  end
end