Module: Legion::Gaia::Workflow
- Defined in:
- lib/legion/gaia/workflow.rb,
lib/legion/gaia/workflow/errors.rb,
lib/legion/gaia/workflow/instance.rb,
lib/legion/gaia/workflow/checkpoint.rb,
lib/legion/gaia/workflow/definition.rb
Overview
Generic state machine DSL for workflow orchestration.
Provides a ‘workflow` class method that defines a state machine on any Ruby class or module, plus `create_workflow` for instantiation.
Quick example
class DocProcessor
include Legion::Gaia::Workflow
workflow :document_processing do |w|
w.state :received, initial: true
w.state :parsing
w.state :enriching
w.state :indexed, terminal: true
w.state :failed, terminal: true
w.transition :received, to: :parsing
w.transition :parsing, to: :enriching, guard: ->(ctx) { ctx[:parse_ok] }
w.transition :parsing, to: :failed
w.transition :enriching, to: :indexed
w.transition :enriching, to: :failed
w.checkpoint :enriching, name: :quality_check,
condition: ->(ctx) { ctx[:score].to_f >= 0.8 }
w.on_enter(:indexed) { |inst| puts "Indexed! id=#{inst.id}" }
w.on_enter(:failed) { |inst| puts "Failed! id=#{inst.id}" }
end
end
inst = DocProcessor.create_workflow(metadata: { doc_id: 42 })
inst.transition!(:parsing)
inst.transition!(:enriching, parse_ok: true)
inst.transition!(:indexed, score: 0.9)
Standalone (no include)
You can also use the DSL directly:
defn = Legion::Gaia::Workflow.define(:my_pipeline) do |w|
w.state :start, initial: true
w.state :finish, terminal: true
w.transition :start, to: :finish
end
inst = Legion::Gaia::Workflow::Instance.new(definition: defn)
inst.transition!(:finish)
Defined Under Namespace
Modules: ClassMethods Classes: Checkpoint, CheckpointBlocked, Definition, Error, GuardRejected, Instance, InvalidTransition, NotInitialized, UnknownState
Class Method Summary collapse
-
.define(name) {|definition| ... } ⇒ Definition
Build a Definition without including the module into a class.
-
.included(base) ⇒ Object
—————————————————————— class methods.
Class Method Details
.define(name) {|definition| ... } ⇒ Definition
Build a Definition without including the module into a class.
112 113 114 115 116 |
# File 'lib/legion/gaia/workflow.rb', line 112 def self.define(name, &block) defn = Definition.new(name.to_sym) block&.call(defn) defn end |
.included(base) ⇒ Object
—————————————————————— class methods
60 61 62 |
# File 'lib/legion/gaia/workflow.rb', line 60 def self.included(base) base.extend(ClassMethods) end |