Class: CMDx::Task
- Inherits:
-
Object
- Object
- CMDx::Task
- Defined in:
- lib/cmdx/task.rb
Overview
Base class for all units of work. Subclasses override #work and
declare their contract via required, optional, output, callbacks,
retry_on, idempotent_by, lock_with, circuit_breaker, rate_limit,
deprecation, and settings.
Invoked via Task.execute (safe) or Task.execute! (strict, raises on failure).
Inheritance: every registry accessor (middlewares, callbacks, coercions, validators, executors, mergers, telemetry, inputs, outputs) lazily clones from the superclass's registry (or the global configuration at the top of the hierarchy), so subclasses extend rather than replace.
Instance Attribute Summary collapse
-
#context ⇒ Object
(also: #ctx)
readonly
Returns the value of attribute context.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#tid ⇒ Object
readonly
Returns the value of attribute tid.
Class Method Summary collapse
-
.call {|result| ... } ⇒ Result, Object
Executes the task.
-
.callbacks ⇒ Callbacks
Cloned from superclass/configuration on first call.
-
.circuit_breaker(key = nil, **options) {|task| ... } ⇒ CircuitBreaker
Declares a shared circuit breaker for the task.
-
.coercions ⇒ Coercions
Cloned from superclass/configuration on first call.
-
.deprecation(value = nil, **options, &block) { ... } ⇒ Deprecation?
Reads, sets, or inherits the task class's Deprecation.
-
.deprecators ⇒ Deprecators
Cloned from superclass/configuration on first call.
-
.deregister(type) ⇒ Object
Dispatches to the appropriate registry's
deregistermethod. -
.execute(context = EMPTY_HASH) {|result| ... } ⇒ Result, Object
Executes the task.
-
.execute!(context = EMPTY_HASH) {|result| ... } ⇒ Result, Object
(also: call!)
Strict execution.
-
.executors ⇒ Executors
Cloned from superclass/configuration on first call.
-
.idempotent_by(key = nil, **options) {|task| ... } ⇒ Idempotency
Declares a stable idempotency key from context (or a callable).
-
.inputs(*names, **options) { ... } ⇒ Inputs
(also: input)
Reads, or declares more, inputs.
-
.inputs_schema ⇒ Hash{Symbol => Hash}
Serialized input definitions.
-
.limiters ⇒ Limiters
Cloned from superclass/configuration on first call.
-
.lock_with(key = nil, **options) {|task| ... } ⇒ Lock
Declares a distributed lock from context (or a callable).
-
.mergers ⇒ Mergers
Cloned from superclass/configuration on first call.
-
.middlewares ⇒ Middlewares
Cloned from superclass/configuration on first call.
-
.optional(*names, **options) { ... } ⇒ Object
Declares optional inputs (shorthand for
inputs ..., required: false). -
.outputs(*keys, **options) ⇒ Outputs
(also: output)
Reads, or declares more, outputs.
-
.outputs_schema ⇒ Hash{Symbol => Hash}
Serialized output definitions.
-
.rate_limit(key = nil, **options) {|task| ... } ⇒ RateLimit
Declares a shared rate limit for the task.
-
.register(type) ⇒ Object
Dispatches to the appropriate registry's
registermethod. -
.required(*names, **options) { ... } ⇒ Object
Declares required inputs (shorthand for
inputs ..., required: true). -
.retriers ⇒ Retriers
Cloned from superclass/configuration on first call.
-
.retry_on(*exceptions, **options) {|attempt, delay| ... } ⇒ Retry
Declares exceptions to retry on.
-
.settings(options = EMPTY_HASH) ⇒ Settings
Reads or extends this class's Settings.
-
.telemetry ⇒ Telemetry
Cloned from superclass/configuration on first call.
-
.type ⇒ String
"Workflow"when the class includes Workflow, else"Task". -
.validators ⇒ Validators
Cloned from superclass/configuration on first call.
Instance Method Summary collapse
-
#execute(strict: false) {|result| ... } ⇒ Result, Object
(also: #call)
Executes this task instance through Runtime.
-
#fail!(reason = nil, **sigdata) ⇒ void
Halts
#workand marks the Result asfailed. -
#initialize(context = EMPTY_HASH) ⇒ Task
constructor
A new instance of Task.
-
#logger ⇒ Logger
A logger tailored to this task's settings.
-
#skip!(reason = nil, **sigdata) ⇒ void
Halts
#workand marks the Result asskipped. -
#success!(reason = nil, **sigdata) ⇒ void
Halts
#workearly with a successful outcome. - #throw!(other, **sigdata) ⇒ void?
-
#work ⇒ void
abstract
The task's core logic.
Constructor Details
#initialize(context = EMPTY_HASH) ⇒ Task
The built Context inherits strict mode from
Settings#strict_context (falling back to
Configuration#strict_context), so dynamic reads for unknown keys
raise NoMethodError instead of returning nil.
Returns a new instance of Task.
604 605 606 607 608 609 610 611 |
# File 'lib/cmdx/task.rb', line 604 def initialize(context = EMPTY_HASH) @metadata = {} @tid = SecureRandom.uuid_v7 @errors = Errors.new @context = Context.build(context).tap do |c| c.strict = self.class.settings.strict_context end end |
Instance Attribute Details
#context ⇒ Object (readonly) Also known as: ctx
Returns the value of attribute context.
596 597 598 |
# File 'lib/cmdx/task.rb', line 596 def context @context end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
596 597 598 |
# File 'lib/cmdx/task.rb', line 596 def errors @errors end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
596 597 598 |
# File 'lib/cmdx/task.rb', line 596 def @metadata end |
#tid ⇒ Object (readonly)
Returns the value of attribute tid.
596 597 598 |
# File 'lib/cmdx/task.rb', line 596 def tid @tid end |
Class Method Details
.call {|result| ... } ⇒ Result, Object
Executes the task. Never raises on failure; inspect the returned Result instead.
549 550 551 |
# File 'lib/cmdx/task.rb', line 549 def execute(context = EMPTY_HASH, &) new(context).execute(strict: false, &) end |
.callbacks ⇒ Callbacks
Returns cloned from superclass/configuration on first call.
230 231 232 233 234 235 236 237 |
# File 'lib/cmdx/task.rb', line 230 def callbacks @callbacks ||= if superclass.respond_to?(:callbacks) superclass.callbacks.dup else CMDx.configuration.callbacks.dup end end |
.circuit_breaker(key = nil, **options) {|task| ... } ⇒ CircuitBreaker
Declares a shared circuit breaker for the task. Builds on the
superclass's CircuitBreaker. Passing no key and no options returns the
current (possibly inherited) configuration. Pass false to disable.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cmdx/task.rb', line 138 def circuit_breaker(key = nil, **, &block) @circuit_breaker ||= if superclass.respond_to?(:circuit_breaker) superclass.circuit_breaker else CircuitBreaker.disabled end @circuit_breaker = if key == false CircuitBreaker.disabled else @circuit_breaker.build(key || block, ) end end |
.coercions ⇒ Coercions
Returns cloned from superclass/configuration on first call.
256 257 258 259 260 261 262 263 |
# File 'lib/cmdx/task.rb', line 256 def coercions @coercions ||= if superclass.respond_to?(:coercions) superclass.coercions.dup else CMDx.configuration.coercions.dup end end |
.deprecation(value = nil, **options, &block) { ... } ⇒ Deprecation?
Reads, sets, or inherits the task class's Deprecation. With a
value or block, replaces any current deprecation. Otherwise returns
the locally defined one, or the superclass's.
412 413 414 415 416 417 418 419 420 |
# File 'lib/cmdx/task.rb', line 412 def deprecation(value = nil, **, &block) if value || block @deprecation = Deprecation.new(value || block, ) elsif defined?(@deprecation) @deprecation elsif superclass.respond_to?(:deprecation) superclass.deprecation end end |
.deprecators ⇒ Deprecators
Returns cloned from superclass/configuration on first call.
316 317 318 319 320 321 322 323 |
# File 'lib/cmdx/task.rb', line 316 def deprecators @deprecators ||= if superclass.respond_to?(:deprecators) superclass.deprecators.dup else CMDx.configuration.deprecators.dup end end |
.deregister(type) ⇒ Object
Dispatches to the appropriate registry's deregister method.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/cmdx/task.rb', line 368 def deregister(type, ...) case type when :middleware middlewares.deregister(...) when :callback callbacks.deregister(...) when :coercion coercions.deregister(...) when :validator validators.deregister(...) when :executor executors.deregister(...) when :merger mergers.deregister(...) when :retrier retriers.deregister(...) when :limiter limiters.deregister(...) when :deprecator deprecators.deregister(...) when :input inputs.deregister(self, ...) when :output outputs.deregister(...) else raise ArgumentError, <<~MSG.chomp unknown registry type #{type.inspect}; expected one of [:middleware, :callback, :coercion, :validator, :executor, :merger, :retrier, :limiter, :deprecator, :input, :output]. See https://drexed.github.io/cmdx/configuration/#registrations-register-deregister MSG end end |
.execute(context = EMPTY_HASH) {|result| ... } ⇒ Result, Object
Executes the task. Never raises on failure; inspect the returned Result instead.
546 547 548 |
# File 'lib/cmdx/task.rb', line 546 def execute(context = EMPTY_HASH, &) new(context).execute(strict: false, &) end |
.execute!(context = EMPTY_HASH) {|result| ... } ⇒ Result, Object Also known as: call!
558 559 560 |
# File 'lib/cmdx/task.rb', line 558 def execute!(context = EMPTY_HASH, &) new(context).execute(strict: true, &) end |
.executors ⇒ Executors
Returns cloned from superclass/configuration on first call.
276 277 278 279 280 281 282 283 |
# File 'lib/cmdx/task.rb', line 276 def executors @executors ||= if superclass.respond_to?(:executors) superclass.executors.dup else CMDx.configuration.executors.dup end end |
.idempotent_by(key = nil, **options) {|task| ... } ⇒ Idempotency
Declares a stable idempotency key from context (or a callable). Builds on
the superclass's Idempotency. Passing no key and no options returns the
current (possibly inherited) configuration. Pass false to disable.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cmdx/task.rb', line 69 def idempotent_by(key = nil, **, &block) @idempotent_by ||= if superclass.respond_to?(:idempotent_by) superclass.idempotent_by else Idempotency.disabled end @idempotent_by = if key == false Idempotency.disabled else @idempotent_by.build(key || block, ) end end |
.inputs(*names, **options) { ... } ⇒ Inputs Also known as: input
Reads, or declares more, inputs. With no names, returns the registry; with names, registers them and defines accessors.
442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/cmdx/task.rb', line 442 def inputs(*names, **, &) @inputs ||= if superclass.respond_to?(:inputs) superclass.inputs.dup else Inputs.new end return @inputs if names.empty? @inputs.register(self, *names, **, &) end |
.inputs_schema ⇒ Hash{Symbol => Hash}
Returns serialized input definitions.
503 504 505 |
# File 'lib/cmdx/task.rb', line 503 def inputs_schema inputs.registry.transform_values(&:to_h) end |
.limiters ⇒ Limiters
Returns cloned from superclass/configuration on first call.
306 307 308 309 310 311 312 313 |
# File 'lib/cmdx/task.rb', line 306 def limiters @limiters ||= if superclass.respond_to?(:limiters) superclass.limiters.dup else CMDx.configuration.limiters.dup end end |
.lock_with(key = nil, **options) {|task| ... } ⇒ Lock
Declares a distributed lock from context (or a callable). Builds on the
superclass's Lock. Passing no key and no options returns the current
(possibly inherited) configuration. Pass false to disable.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cmdx/task.rb', line 102 def lock_with(key = nil, **, &block) @lock_with ||= if superclass.respond_to?(:lock_with) superclass.lock_with else Lock.disabled end @lock_with = if key == false Lock.disabled else @lock_with.build(key || block, ) end end |
.mergers ⇒ Mergers
Returns cloned from superclass/configuration on first call.
286 287 288 289 290 291 292 293 |
# File 'lib/cmdx/task.rb', line 286 def mergers @mergers ||= if superclass.respond_to?(:mergers) superclass.mergers.dup else CMDx.configuration.mergers.dup end end |
.middlewares ⇒ Middlewares
Returns cloned from superclass/configuration on first call.
220 221 222 223 224 225 226 227 |
# File 'lib/cmdx/task.rb', line 220 def middlewares @middlewares ||= if superclass.respond_to?(:middlewares) superclass.middlewares.dup else CMDx.configuration.middlewares.dup end end |
.optional(*names, **options) { ... } ⇒ Object
Declares optional inputs (shorthand for inputs ..., required: false).
An explicit required: in options is ignored — use inputs when
you need to set the flag dynamically.
475 476 477 |
# File 'lib/cmdx/task.rb', line 475 def optional(*names, **, &) register(:input, *names, **, required: false, &) end |
.outputs(*keys, **options) ⇒ Outputs Also known as: output
Reads, or declares more, outputs. With no keys, returns the registry.
516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/cmdx/task.rb', line 516 def outputs(*keys, **) @outputs ||= if superclass.respond_to?(:outputs) superclass.outputs.dup else Outputs.new end return @outputs if keys.empty? @outputs.register(*keys, **) end |
.outputs_schema ⇒ Hash{Symbol => Hash}
Returns serialized output definitions.
531 532 533 |
# File 'lib/cmdx/task.rb', line 531 def outputs_schema outputs.registry.transform_values(&:to_h) end |
.rate_limit(key = nil, **options) {|task| ... } ⇒ RateLimit
Declares a shared rate limit for the task. Builds on the superclass's
RateLimit. Passing no key and no options returns the current
(possibly inherited) configuration. Pass false to disable.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/cmdx/task.rb', line 175 def rate_limit(key = nil, **, &block) @rate_limit ||= if superclass.respond_to?(:rate_limit) superclass.rate_limit else RateLimit.disabled end @rate_limit = if key == false RateLimit.disabled else @rate_limit.build(key || block, ) end end |
.register(type) ⇒ Object
Dispatches to the appropriate registry's register method.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/cmdx/task.rb', line 330 def register(type, ...) case type when :middleware middlewares.register(...) when :callback callbacks.register(...) when :coercion coercions.register(...) when :validator validators.register(...) when :executor executors.register(...) when :merger mergers.register(...) when :retrier retriers.register(...) when :limiter limiters.register(...) when :deprecator deprecators.register(...) when :input inputs.register(self, ...) when :output outputs.register(...) else raise ArgumentError, <<~MSG.chomp unknown registry type #{type.inspect}; expected one of [:middleware, :callback, :coercion, :validator, :executor, :merger, :retrier, :limiter, :deprecator, :input, :output]. See https://drexed.github.io/cmdx/configuration/#registrations-register-deregister MSG end end |
.required(*names, **options) { ... } ⇒ Object
Declares required inputs (shorthand for inputs ..., required: true).
An explicit required: in options is ignored — use inputs when
you need to set the flag dynamically.
498 499 500 |
# File 'lib/cmdx/task.rb', line 498 def required(*names, **, &) register(:input, *names, **, required: true, &) end |
.retriers ⇒ Retriers
Returns cloned from superclass/configuration on first call.
296 297 298 299 300 301 302 303 |
# File 'lib/cmdx/task.rb', line 296 def retriers @retriers ||= if superclass.respond_to?(:retriers) superclass.retriers.dup else CMDx.configuration.retriers.dup end end |
.retry_on(*exceptions, **options) {|attempt, delay| ... } ⇒ Retry
Declares exceptions to retry on. Builds on the superclass's Retry.
Passing no exceptions and no options returns the current (possibly
inherited) Retry. Pass false to disable retries on this class.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cmdx/task.rb', line 37 def retry_on(*exceptions, **, &) @retry_on ||= if superclass.respond_to?(:retry_on) superclass.retry_on else Retry.disabled end @retry_on = if exceptions == [false] Retry.disabled else @retry_on.build(exceptions, , &) end end |
.settings(options = EMPTY_HASH) ⇒ Settings
Reads or extends this class's Settings. Inherits from the superclass.
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/cmdx/task.rb', line 206 def settings( = EMPTY_HASH) @settings ||= if superclass.respond_to?(:settings) superclass.settings.build() else Settings.new() end return @settings if .empty? @settings = @settings.build() end |
.telemetry ⇒ Telemetry
Returns cloned from superclass/configuration on first call.
246 247 248 249 250 251 252 253 |
# File 'lib/cmdx/task.rb', line 246 def telemetry @telemetry ||= if superclass.respond_to?(:telemetry) superclass.telemetry.dup else CMDx.configuration.telemetry.dup end end |
.type ⇒ String
Returns "Workflow" when the class includes Workflow, else "Task".
536 537 538 |
# File 'lib/cmdx/task.rb', line 536 def type @type ||= include?(Workflow) ? "Workflow" : "Task" end |
.validators ⇒ Validators
Returns cloned from superclass/configuration on first call.
266 267 268 269 270 271 272 273 |
# File 'lib/cmdx/task.rb', line 266 def validators @validators ||= if superclass.respond_to?(:validators) superclass.validators.dup else CMDx.configuration.validators.dup end end |
Instance Method Details
#execute(strict: false) {|result| ... } ⇒ Result, Object Also known as: call
Executes this task instance through Runtime.
621 622 623 624 |
# File 'lib/cmdx/task.rb', line 621 def execute(strict: false) result = Runtime.execute(self, strict:) block_given? ? yield(result) : result end |
#fail!(reason = nil, **sigdata) ⇒ void
690 691 692 693 694 695 696 697 698 699 700 |
# File 'lib/cmdx/task.rb', line 690 def fail!(reason = nil, **sigdata) if frozen? raise FrozenTaskError, <<~MSG.chomp cannot call :fail! on #{self.class}; the task has already been executed and frozen. See https://drexed.github.io/cmdx/outcomes/result/#lifecycle-predicates MSG end .merge!(sigdata) unless sigdata.empty? throw(Signal::TAG, Signal.failed(reason, metadata:, backtrace: caller_locations(1))) end |
#logger ⇒ Logger
Returns a logger tailored to this task's settings.
628 629 630 |
# File 'lib/cmdx/task.rb', line 628 def logger @logger ||= LoggerProxy.logger(self) end |
#skip!(reason = nil, **sigdata) ⇒ void
670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/cmdx/task.rb', line 670 def skip!(reason = nil, **sigdata) if frozen? raise FrozenTaskError, <<~MSG.chomp cannot call :skip! on #{self.class}; the task has already been executed and frozen. See https://drexed.github.io/cmdx/outcomes/result/#lifecycle-predicates MSG end .merge!(sigdata) unless sigdata.empty? throw(Signal::TAG, Signal.skipped(reason, metadata:)) end |
#success!(reason = nil, **sigdata) ⇒ void
This method returns an undefined value.
Halts #work early with a successful outcome. Any sigdata is merged
onto #metadata before the signal is thrown.
651 652 653 654 655 656 657 658 659 660 661 |
# File 'lib/cmdx/task.rb', line 651 def success!(reason = nil, **sigdata) if frozen? raise FrozenTaskError, <<~MSG.chomp cannot call :success! on #{self.class}; the task has already been executed and frozen. See https://drexed.github.io/cmdx/outcomes/result/#lifecycle-predicates MSG end .merge!(sigdata) unless sigdata.empty? throw(Signal::TAG, Signal.success(reason, metadata:)) end |
#throw!(other, **sigdata) ⇒ void?
712 713 714 715 716 717 718 719 720 721 722 723 724 |
# File 'lib/cmdx/task.rb', line 712 def throw!(other, **sigdata) if frozen? raise FrozenTaskError, <<~MSG.chomp cannot call :throw! on #{self.class}; the task has already been executed and frozen. See https://drexed.github.io/cmdx/outcomes/result/#lifecycle-predicates MSG end return unless other.failed? .merge!(sigdata) unless sigdata.empty? throw(Signal::TAG, Signal.echoed(other, metadata:, backtrace: caller_locations(1))) end |
#work ⇒ void
This method returns an undefined value.
The task's core logic. Subclasses must override.
637 638 639 640 641 642 |
# File 'lib/cmdx/task.rb', line 637 def work raise ImplementationError, <<~MSG.chomp #{self.class} must implement #work. See https://drexed.github.io/cmdx/basics/setup/#structure MSG end |