Class: CMDx::Executors
- Inherits:
-
Object
- Object
- CMDx::Executors
- Defined in:
- lib/cmdx/executors.rb,
lib/cmdx/executors/fiber.rb,
lib/cmdx/executors/thread.rb
Overview
Registry of named executors used by ‘:parallel` workflow groups to dispatch tasks concurrently. Ships with built-ins for `:threads` and `:fibers`. Executors are any callable accepting `call(jobs:, concurrency:, on_job:)` and must invoke `on_job.call(job)` for each job, blocking until every job is done.
Defined Under Namespace
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#deregister(name) ⇒ Executors
Self for chaining.
- #empty? ⇒ Boolean
-
#initialize ⇒ Executors
constructor
A new instance of Executors.
- #initialize_copy(source) ⇒ void
-
#lookup(name) ⇒ #call
The registered executor.
-
#register(name, callable = nil, &block) { ... } ⇒ Executors
Registers a named executor, overwriting any existing entry.
-
#resolve(spec) ⇒ #call
Resolves a declaration’s ‘:executor` option to a concrete callable.
- #size ⇒ Integer
Constructor Details
Instance Attribute Details
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
11 12 13 |
# File 'lib/cmdx/executors.rb', line 11 def registry @registry end |
Instance Method Details
#deregister(name) ⇒ Executors
Returns self for chaining.
50 51 52 53 |
# File 'lib/cmdx/executors.rb', line 50 def deregister(name) registry.delete(name.to_sym) self end |
#empty? ⇒ Boolean
85 86 87 |
# File 'lib/cmdx/executors.rb', line 85 def empty? registry.empty? end |
#initialize_copy(source) ⇒ void
This method returns an undefined value.
22 23 24 |
# File 'lib/cmdx/executors.rb', line 22 def initialize_copy(source) @registry = source.registry.dup end |
#lookup(name) ⇒ #call
Returns the registered executor.
58 59 60 61 62 |
# File 'lib/cmdx/executors.rb', line 58 def lookup(name) registry[name] || begin raise ArgumentError, "unknown executor: #{name.inspect}" end end |
#register(name, callable = nil, &block) { ... } ⇒ Executors
Registers a named executor, overwriting any existing entry.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cmdx/executors.rb', line 35 def register(name, callable = nil, &block) executor = callable || block if callable && block raise ArgumentError, "provide either a callable or a block, not both" elsif !executor.respond_to?(:call) raise ArgumentError, "executor must respond to #call" end registry[name.to_sym] = executor self end |
#resolve(spec) ⇒ #call
Resolves a declaration’s ‘:executor` option to a concrete callable. Accepts `nil` (default `:threads`), a Symbol (registry lookup), or any object responding to `#call`.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cmdx/executors.rb', line 71 def resolve(spec) case spec when NilClass lookup(:threads) when Symbol lookup(spec) else return spec if spec.respond_to?(:call) raise ArgumentError, "unknown executor: #{spec.inspect}" end end |
#size ⇒ Integer
90 91 92 |
# File 'lib/cmdx/executors.rb', line 90 def size registry.size end |