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.



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

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.



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

def arguments
  @arguments
end

#callback_typeObject

Returns the value of attribute callback_type.



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

def callback_type
  @callback_type
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



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

def dependencies
  @dependencies
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



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

def finished_at
  @finished_at
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#incomingObject

Returns the value of attribute incoming.



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

def incoming
  @incoming
end

#jobsObject

Returns the value of attribute jobs.



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

def jobs
  @jobs
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#outgoingObject

Returns the value of attribute outgoing.



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

def outgoing
  @outgoing
end

#parent_idObject

Returns the value of attribute parent_id.



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

def parent_id
  @parent_id
end

#persistedObject (readonly)

Returns the value of attribute persisted.



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

def persisted
  @persisted
end

#started_atObject (readonly)

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#stoppedObject

Returns the value of attribute stopped.



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

def stopped
  @stopped
end

Class Method Details

.create(*args) ⇒ Object



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

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

.find(id) ⇒ Object



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

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

Instance Method Details

#as_jsonObject



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

def as_json
  to_hash.to_json
end

#build_idObject



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

def build_id
  client.build_workflow_id
end

#cb_build_in?Boolean

Returns:

  • (Boolean)


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

def cb_build_in?
  callback_type == BUILD_IN
end

#configure(*arguments) ⇒ Object



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

def configure(*arguments); end

#failed?Boolean

Returns:

  • (Boolean)


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

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

#find_job(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dwf/workflow.rb', line 117

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)


153
154
155
# File 'lib/dwf/workflow.rb', line 153

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

#leaf_nodesObject



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

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

#mark_as_persistedObject



180
181
182
# File 'lib/dwf/workflow.rb', line 180

def mark_as_persisted
  @persisted = true
end

#mark_as_startedObject



184
185
186
# File 'lib/dwf/workflow.rb', line 184

def mark_as_started
  @stopped = false
end

#nameObject



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

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

#output_payloadObject



192
193
194
195
196
197
198
199
# File 'lib/dwf/workflow.rb', line 192

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)


167
168
169
# File 'lib/dwf/workflow.rb', line 167

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

#payloadsObject



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

def payloads
  @payloads ||= build_payloads
end

#persist!Object Also known as: save



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

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

#reloadObject



91
92
93
94
95
96
97
# File 'lib/dwf/workflow.rb', line 91

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

  self
end

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



109
110
111
112
113
114
115
# File 'lib/dwf/workflow.rb', line 109

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!

Raises:



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

def start!
  raise UnsupportCallback, 'Sub workflow only works with Sidekiq batch callback' if invalid_callback?

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

#start_initial!Object



85
86
87
# File 'lib/dwf/workflow.rb', line 85

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

#statusObject



171
172
173
174
175
176
177
178
# File 'lib/dwf/workflow.rb', line 171

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

  :running
end

#stopped?Boolean

Returns:

  • (Boolean)


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

def stopped?
  stopped
end

#sub_workflow?Boolean

Returns:

  • (Boolean)


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

def sub_workflow?
  !parent_id.nil?
end

#to_hashObject



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

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