module RubyExecutor
Constants
- FALLBACK_POLICY
The set of possible fallback policies that may be set at thread pool creation.
Public Instance Methods
# File lib/garcon/task/executor.rb, line 256 def execute(*args, &task) raise NotImplementedError end
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
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
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
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
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
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
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
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
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
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
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