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
-
#key?(name) ⇒ Boolean
Whether an executor is registered under
name. -
#lookup(name) ⇒ #call
The registered executor.
-
#register(name, callable = nil, &block) { ... } ⇒ Executors
Registers a named executor, overwriting any existing entry.
-
#resolve(spec, default: nil) ⇒ #call
Resolves a declaration's
:executoroption 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.
53 54 55 56 |
# File 'lib/cmdx/executors.rb', line 53 def deregister(name) registry.delete(name) self end |
#empty? ⇒ Boolean
107 108 109 |
# File 'lib/cmdx/executors.rb', line 107 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 |
#key?(name) ⇒ Boolean
Returns whether an executor is registered under name.
60 61 62 |
# File 'lib/cmdx/executors.rb', line 60 def key?(name) registry.key?(name) end |
#lookup(name) ⇒ #call
Returns the registered executor.
67 68 69 70 71 72 73 74 |
# File 'lib/cmdx/executors.rb', line 67 def lookup(name) registry[name] || begin raise UnknownEntryError, <<~MSG.chomp unknown executor #{name.inspect}; registered: #{registry.keys.inspect}. See https://drexed.github.io/cmdx/workflows/#executors MSG 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 47 48 49 |
# File 'lib/cmdx/executors.rb', line 35 def register(name, callable = nil, &block) executor = callable || block if callable && block raise ArgumentError, "executor: provide either a callable or a block, not both" elsif !executor.respond_to?(:call) raise ArgumentError, <<~MSG.chomp executor must respond to #call (got #{executor.class}). See https://drexed.github.io/cmdx/workflows/#executors MSG end registry[name.to_sym] = executor self end |
#resolve(spec, default: nil) ⇒ #call
Resolves a declaration's :executor option to a concrete callable.
Accepts nil (default :threads unless default: is set), a Symbol
(:threads, :fibers, or :auto), or any object responding to #call.
:auto expands to :fibers when ::Fiber.scheduler is installed,
otherwise :threads. When spec is nil, default supplies the
Settings#default_executor / Configuration#default_executor value.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cmdx/executors.rb', line 87 def resolve(spec, default: nil) unless spec.is_a?(Symbol) || spec.nil? return spec if spec.respond_to?(:call) raise UnknownEntryError, <<~MSG.chomp unknown executor #{spec.inspect}; expected a Symbol from #{registry.keys.inspect} or a callable. See https://drexed.github.io/cmdx/workflows/#executors MSG end effective = spec.nil? ? (default || :threads) : spec if effective == :auto effective = ::Fiber.scheduler.nil? ? :threads : :fibers end lookup(effective) end |
#size ⇒ Integer
112 113 114 |
# File 'lib/cmdx/executors.rb', line 112 def size registry.size end |