Class: Ductwork::Pipeline

Inherits:
Record
  • Object
show all
Defined in:
lib/ductwork/models/pipeline.rb

Overview

rubocop:todo Metrics/ClassLength

Defined Under Namespace

Classes: DefinitionError, ReviveError

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.pipeline_definitionObject (readonly)

Returns the value of attribute pipeline_definition.



34
35
36
# File 'lib/ductwork/models/pipeline.rb', line 34

def pipeline_definition
  @pipeline_definition
end

Class Method Details

.define(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ductwork/models/pipeline.rb', line 36

def define(&block)
  if !block_given?
    raise DefinitionError, "Definition block must be given"
  end

  if pipeline_definition
    raise DefinitionError, "Pipeline has already been defined"
  end

  builder = Ductwork::DSL::DefinitionBuilder.new

  block.call(builder)

  @pipeline_definition = builder.complete

  Ductwork.defined_pipelines << name.to_s
end

.inherited(subclass) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/ductwork/models/pipeline.rb', line 22

def self.inherited(subclass)
  super

  subclass.class_eval do
    default_scope { where(klass: name.to_s) }
  end
end

.trigger(*args) ⇒ Object

rubocop:todo Metrics



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ductwork/models/pipeline.rb', line 54

def trigger(*args) # rubocop:todo Metrics
  if pipeline_definition.nil?
    raise DefinitionError, "Pipeline must be defined before triggering"
  end

  now = Time.current
  node = pipeline_definition.dig(:nodes, 0)
  klass = pipeline_definition.dig(:edges, node, :klass)
  definition = JSON.dump(pipeline_definition)

  pipeline = Record.transaction do # rubocop:todo Metrics/BlockLength
    p = create!(
      klass: name.to_s,
      status: :in_progress
    )
    run = p.runs.create!(
      pipeline_klass: name.to_s,
      status: :in_progress,
      definition: definition,
      definition_sha1: Digest::SHA1.hexdigest(definition),
      triggered_at: now,
      started_at: now
    )
    branch = run.branches.create!(
      pipeline_klass: name.to_s,
      status: :in_progress,
      started_at: now,
      last_advanced_at: now
    )
    step = branch.steps.create!(
      run: run,
      node: node,
      klass: klass,
      status: :in_progress,
      to_transition: :start,
      started_at: now
    )
    Ductwork::Job.enqueue(step, *args)

    p
  end

  Ductwork.logger.info(
    msg: "Pipeline triggered",
    pipeline_id: pipeline.id,
    role: :application
  )

  pipeline
end

Instance Method Details

#current_runObject



106
107
108
# File 'lib/ductwork/models/pipeline.rb', line 106

def current_run
  runs.in_progress.sole
end

#revive!(duplicate_context: false) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ductwork/models/pipeline.rb', line 110

def revive!(duplicate_context: false)
  if !halted?
    raise ReviveError, "Cannot revive #{status} pipeline"
  end

  last_run = runs.order(started_at: :desc).first

  if last_run.blank?
    raise ReviveError, "Cannot revive pipeline without previous run"
  end

  now = Time.current
  new_run = last_run.dup
  new_run.triggered_at = now
  new_run.started_at = now
  new_run.status = "in_progress"
  new_run.on_halt_dispatched_at = nil

  Ductwork::Record.transaction do
    lock!
    new_run.save!
    duplicate_successful_branches_and_steps(new_run, last_run, now)
    duplicate_halted_branches_and_steps(new_run, last_run, now)
    conditionally_duplicate_context(last_run, new_run, now, duplicate_context)
    duplicate_all_branch_links(new_run, last_run)
    in_progress!
  end

  self
end