Module: Garcon::RubyExecutor

Includes:
Executor
Included in:
RubySingleThreadExecutor, ThreadPoolExecutor, TimerSet, TimerTask
Defined in:
lib/garcon/task/executor.rb

Constant Summary collapse

FALLBACK_POLICY =

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

[:abort, :discard, :caller_runs]

Instance Attribute Summary

Attributes included from Executor

#fallback_policy

Instance Method Summary collapse

Methods included from Executor

#auto_terminate?, #can_overflow?, #serialized?

Instance Method Details

#<<(task) ⇒ self

Submit a task to the executor for asynchronous processing.

Parameters:

  • task (Proc)

    the asynchronous task to perform

Returns:

  • (self)

    returns itself



171
172
173
174
# File 'lib/garcon/task/executor.rb', line 171

def <<(task)
  post(&task)
  self
end

#killObject

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.



217
218
219
220
221
222
223
224
225
# 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

#post(*args) { ... } ⇒ Boolean

Submit a task to the executor for asynchronous processing.

Parameters:

  • args (Array)

    Zero or more arguments to be passed to the task

Yields:

  • the asynchronous task to perform

Returns:

  • (Boolean)

    True if the task is queued, false if the executor is not running.

Raises:

  • (ArgumentError)

    if no task is given



154
155
156
157
158
159
160
161
162
# 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

#running?Boolean

Is the executor running?

Returns:

  • (Boolean)

    True when running, false when shutting down or shutdown.



180
181
182
# File 'lib/garcon/task/executor.rb', line 180

def running?
  ! stop_event.set?
end

#shutdownObject

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.



204
205
206
207
208
209
210
211
# 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?Boolean

Is the executor shutdown?

Returns:

  • (Boolean)

    True when shutdown, false when shutting down or running.



196
197
198
# File 'lib/garcon/task/executor.rb', line 196

def shutdown?
  stopped_event.set?
end

#shuttingdown?Boolean

Is the executor shuttingdown?

Returns:

  • (Boolean)

    True when not running and not shutdown, else false.



188
189
190
# File 'lib/garcon/task/executor.rb', line 188

def shuttingdown?
  ! (running? || shutdown?)
end

#wait_for_termination(timeout = nil) ⇒ Boolean

Note:

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

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

Parameters:

  • timeout (Integer) (defaults to: nil)

    The maximum number of seconds to wait for shutdown to complete

Returns:

  • (Boolean)

    True if shutdown complete or false on timeout.



239
240
241
# File 'lib/garcon/task/executor.rb', line 239

def wait_for_termination(timeout = nil)
  stopped_event.wait(timeout)
end