Class: Smith::Workflow::Composite::Plan

Inherits:
Payload
  • Object
show all
Defined in:
lib/smith/workflow/composite/plan.rb

Constant Summary collapse

VERSION =
1
MAX_BRANCHES =
10_000
RESUME_POLICY =
:incomplete_only
FAILURE_POLICY =
:host_committed_primary
REDUCTION_POLICY =
:ordered_all_success
RETRY_POLICY =
:none

Constants inherited from Payload

Smith::Workflow::Composite::Payload::MAX_SERIALIZED_BYTES

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Plan

Returns a new instance of Plan.



119
120
121
122
123
124
# File 'lib/smith/workflow/composite/plan.rb', line 119

def initialize(attributes)
  self.class.preflight_attributes!(attributes)
  owned = self.class.normalize_attributes(attributes)
  super(owned)
  PlanIntegrity.new(self).call
end

Class Method Details

.build(**attributes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/smith/workflow/composite/plan.rb', line 44

def build(**attributes)
  values = {
    version: VERSION,
    execution_semantics_version: Smith::EXECUTION_SEMANTICS_VERSION,
    dispatch: attributes.fetch(:dispatch),
    kind: attributes.fetch(:kind),
    transition: attributes.fetch(:transition).to_s,
    from: attributes.fetch(:from).to_s,
    execution_namespace: attributes.fetch(:execution_namespace),
    branches: attributes.fetch(:branches),
    input_digest: attributes.fetch(:input_digest),
    budget_state_digest: attributes.fetch(:budget_state_digest),
    resume_policy: RESUME_POLICY,
    failure_policy: FAILURE_POLICY,
    reduction_policy: REDUCTION_POLICY,
    retry_policy: RETRY_POLICY
  }
  new(values.merge(plan_digest: PayloadDigest.call(serializable(values))))
end

.normalize_attributes(attributes) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/smith/workflow/composite/plan.rb', line 64

def normalize_attributes(attributes)
  normalized = super
  normalize_dispatch!(normalized)
  normalize_branches!(normalized)
  normalize_policies!(normalized)
  normalized
end

.preflight_attributes!(attributes) ⇒ Object



72
73
74
75
76
# File 'lib/smith/workflow/composite/plan.rb', line 72

def preflight_attributes!(attributes)
  branches = raw_attribute(attributes, :branches)
  validate_branch_count!(branches) if branches.is_a?(Array)
  attributes
end

.serializable(values) ⇒ Object



78
79
80
81
82
83
# File 'lib/smith/workflow/composite/plan.rb', line 78

def serializable(values)
  values.merge(
    dispatch: values.fetch(:dispatch).to_h,
    branches: values.fetch(:branches).map(&:to_h)
  )
end

.validate_branch_count!(branches_or_count) ⇒ Object

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
# File 'lib/smith/workflow/composite/plan.rb', line 85

def validate_branch_count!(branches_or_count)
  count = if branches_or_count.is_a?(Array)
            Array.instance_method(:length).bind_call(branches_or_count)
          else
            branches_or_count
          end
  return if count&.between?(1, MAX_BRANCHES)

  raise ArgumentError, "composite plan branch count is outside the transport limit"
end

Instance Method Details

#execution_for(branch) ⇒ Object



126
# File 'lib/smith/workflow/composite/plan.rb', line 126

def execution_for(branch) = BranchExecution.build(plan: self, branch:)