Class: Dwf::Workflow

Inherits:
Object
  • Object
show all
Includes:
Concerns::Checkable
Defined in:
lib/dwf/workflow.rb

Constant Summary collapse

CALLBACK_TYPES =
[
  BUILD_IN = 'build-in',
  SK_BATCH = 'sk-batch'
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Checkable

#leaf?, #no_dependencies?, #ready_to_start?, #running?, #started?, #succeeded?

Constructor Details

#initialize(*args) ⇒ Workflow

Returns a new instance of Workflow.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dwf/workflow.rb', line 31

def initialize(*args)
  @dependencies = []
  @id = build_id
  @jobs = []
  @persisted = false
  @stopped = false
  @arguments = *args
  @parent_id = nil
  @klass = self.class
  @callback_type = BUILD_IN
  @incoming = []
  @outgoing = []

  setup
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



16
17
18
# File 'lib/dwf/workflow.rb', line 16

def arguments
  @arguments
end

#callback_typeObject

Returns the value of attribute callback_type.



16
17
18
# File 'lib/dwf/workflow.rb', line 16

def callback_type
  @callback_type
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



16
17
18
# File 'lib/dwf/workflow.rb', line 16

def dependencies
  @dependencies
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



16
17
18
# File 'lib/dwf/workflow.rb', line 16

def finished_at
  @finished_at
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/dwf/workflow.rb', line 15

def id
  @id
end

#incomingObject

Returns the value of attribute incoming.



15
16
17
# File 'lib/dwf/workflow.rb', line 15

def incoming
  @incoming
end

#jobsObject

Returns the value of attribute jobs.



15
16
17
# File 'lib/dwf/workflow.rb', line 15

def jobs
  @jobs
end

#klassObject (readonly)

Returns the value of attribute klass.



16
17
18
# File 'lib/dwf/workflow.rb', line 16

def klass
  @klass
end

#outgoingObject

Returns the value of attribute outgoing.



15
16
17
# File 'lib/dwf/workflow.rb', line 15

def outgoing
  @outgoing
end

#parent_idObject

Returns the value of attribute parent_id.



15
16
17
# File 'lib/dwf/workflow.rb', line 15

def parent_id
  @parent_id
end

#persistedObject (readonly)

Returns the value of attribute persisted.



16
17
18
# File 'lib/dwf/workflow.rb', line 16

def persisted
  @persisted
end

#started_atObject (readonly)

Returns the value of attribute started_at.



16
17
18
# File 'lib/dwf/workflow.rb', line 16

def started_at
  @started_at
end

#stoppedObject

Returns the value of attribute stopped.



15
16
17
# File 'lib/dwf/workflow.rb', line 15

def stopped
  @stopped
end

Class Method Details

.create(*args) ⇒ Object



20
21
22
23
24
# File 'lib/dwf/workflow.rb', line 20

def create(*args)
  flow = new(*args)
  flow.save
  flow
end

.find(id) ⇒ Object



26
27
28
# File 'lib/dwf/workflow.rb', line 26

def find(id)
  Dwf::Client.new.find_workflow(id)
end

Instance Method Details

#as_jsonObject



146
147
148
# File 'lib/dwf/workflow.rb', line 146

def as_json
  to_hash.to_json
end

#build_idObject



100
101
102
# File 'lib/dwf/workflow.rb', line 100

def build_id
  client.build_workflow_id
end

#cb_build_in?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/dwf/workflow.rb', line 96

def cb_build_in?
  callback_type == BUILD_IN
end

#configure(*arguments) ⇒ Object



104
# File 'lib/dwf/workflow.rb', line 104

def configure(*arguments); end

#enqueue_outgoing_jobsObject



177
178
179
180
181
182
183
184
185
186
# File 'lib/dwf/workflow.rb', line 177

def enqueue_outgoing_jobs
  return unless sub_workflow?

  outgoing.each do |job_name|
    client.check_or_lock(parent_id, job_name) do
      node = client.find_node(job_name, parent_id)
      node.persist_and_perform_async! if node.ready_to_start?
    end
  end
end

#failed?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/dwf/workflow.rb', line 156

def failed?
  jobs.any?(&:failed?)
end

#find_job(name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dwf/workflow.rb', line 114

def find_job(name)
  match_data = /(?<klass>\w*[^-])-(?<identifier>.*)/.match(name.to_s)

  if match_data.nil?
    job = jobs.find { |node| node.klass.to_s == name.to_s }
  else
    job = jobs.find { |node| node.name.to_s == name.to_s }
  end

  job
end

#finished?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/dwf/workflow.rb', line 150

def finished?
  jobs.all?(&:finished?)
end

#leaf_nodesObject



196
197
198
# File 'lib/dwf/workflow.rb', line 196

def leaf_nodes
  jobs.select(&:leaf?)
end

#mark_as_persistedObject



188
189
190
# File 'lib/dwf/workflow.rb', line 188

def mark_as_persisted
  @persisted = true
end

#mark_as_startedObject



192
193
194
# File 'lib/dwf/workflow.rb', line 192

def mark_as_started
  @stopped = false
end

#nameObject



54
55
56
# File 'lib/dwf/workflow.rb', line 54

def name
  "#{self.class.name}|#{id}"
end

#output_payloadObject



200
201
202
203
204
205
206
207
# File 'lib/dwf/workflow.rb', line 200

def output_payload
  leaf_nodes.map do |node|
    data = node.output_payload
    next if data.nil?

    data
  end.compact
end

#parents_succeeded?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/dwf/workflow.rb', line 164

def parents_succeeded?
  incoming.all? { |name| client.find_node(name, parent_id).succeeded? }
end

#payloadsObject



78
79
80
# File 'lib/dwf/workflow.rb', line 78

def payloads
  @payloads ||= build_payloads
end

#persist!Object Also known as: save



47
48
49
50
51
52
# File 'lib/dwf/workflow.rb', line 47

def persist!
  client.persist_workflow(self)
  jobs.each(&:persist!)
  mark_as_persisted
  true
end

#reloadObject



88
89
90
91
92
93
94
# File 'lib/dwf/workflow.rb', line 88

def reload
  flow = self.class.find(id)
  self.stopped = flow.stopped
  self.jobs = flow.jobs

  self
end

#run(klass, options = {}) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/dwf/workflow.rb', line 106

def run(klass, options = {})
  node = build_node(klass, options)
  jobs << node

  build_dependencies_structure(node, options)
  node.name
end

#start!Object Also known as: persist_and_perform_async!



69
70
71
72
73
74
75
76
# File 'lib/dwf/workflow.rb', line 69

def start!
  mark_as_started
  persist!
  initial_jobs.each do |job|
    job.payloads = payloads if sub_workflow?
    job.start_initial!
  end
end

#start_initial!Object



82
83
84
# File 'lib/dwf/workflow.rb', line 82

def start_initial!
  cb_build_in? ? start! : Callback.new.start(self)
end

#statusObject



168
169
170
171
172
173
174
175
# File 'lib/dwf/workflow.rb', line 168

def status
  return :failed if failed?
  return :running if running?
  return :finished if finished?
  return :stopped if stopped?

  :running
end

#stopped?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/dwf/workflow.rb', line 160

def stopped?
  stopped
end

#sub_workflow?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/dwf/workflow.rb', line 58

def sub_workflow?
  !parent_id.nil?
end

#to_hashObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dwf/workflow.rb', line 126

def to_hash
  name = self.class.to_s
  {
    name: name,
    id: id,
    arguments: @arguments,
    total: jobs.count,
    finished: jobs.count(&:finished?),
    klass: name,
    status: status,
    stopped: stopped,
    started_at: started_at,
    finished_at: finished_at,
    callback_type: callback_type,
    incoming: incoming,
    outgoing: outgoing,
    parent_id: parent_id
  }
end