Class: Dwf::Workflow

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(options = {}) ⇒ Workflow

Returns a new instance of Workflow.



24
25
26
27
28
29
30
31
32
33
# File 'lib/dwf/workflow.rb', line 24

def initialize(options = {})
  @dependencies = []
  @id = id
  @jobs = []
  @persisted = false
  @stopped = false
  @callback_type = options[:callback_type] || BUILD_IN

  setup
end

Instance Attribute Details

#callback_typeObject (readonly)

Returns the value of attribute callback_type.



13
14
15
# File 'lib/dwf/workflow.rb', line 13

def callback_type
  @callback_type
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



13
14
15
# File 'lib/dwf/workflow.rb', line 13

def dependencies
  @dependencies
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



13
14
15
# File 'lib/dwf/workflow.rb', line 13

def finished_at
  @finished_at
end

#jobsObject (readonly)

Returns the value of attribute jobs.



13
14
15
# File 'lib/dwf/workflow.rb', line 13

def jobs
  @jobs
end

#persistedObject (readonly)

Returns the value of attribute persisted.



13
14
15
# File 'lib/dwf/workflow.rb', line 13

def persisted
  @persisted
end

#started_atObject (readonly)

Returns the value of attribute started_at.



13
14
15
# File 'lib/dwf/workflow.rb', line 13

def started_at
  @started_at
end

#stoppedObject (readonly)

Returns the value of attribute stopped.



13
14
15
# File 'lib/dwf/workflow.rb', line 13

def stopped
  @stopped
end

Class Method Details

.create(*args) ⇒ Object



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

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

Instance Method Details

#as_jsonObject



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

def as_json
  to_hash.to_json
end

#cb_build_in?Boolean

Returns:

  • (Boolean)


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

def cb_build_in?
  callback_type == BUILD_IN
end

#configureObject



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

def configure; end

#failed?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/dwf/workflow.rb', line 118

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

#find_job(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dwf/workflow.rb', line 73

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)


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

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

#idObject



52
53
54
# File 'lib/dwf/workflow.rb', line 52

def id
  @id ||= client.build_workflow_id
end

#mark_as_persistedObject



135
136
137
# File 'lib/dwf/workflow.rb', line 135

def mark_as_persisted
  @persisted = true
end

#mark_as_startedObject



139
140
141
# File 'lib/dwf/workflow.rb', line 139

def mark_as_started
  @stopped = false
end

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dwf/workflow.rb', line 58

def run(klass, options = {})
  node = klass.new(
    workflow_id: id,
    id: client.build_job_id(id, klass.to_s),
    params: options.fetch(:params, {}),
    queue: options[:queue],
    callback_type: callback_type
  )

  jobs << node

  build_dependencies_structure(node, options)
  node.name
end

#running?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/dwf/workflow.rb', line 114

def running?
  started? && !finished?
end

#saveObject



41
42
43
44
45
46
# File 'lib/dwf/workflow.rb', line 41

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

#start!Object



35
36
37
38
39
# File 'lib/dwf/workflow.rb', line 35

def start!
  initial_jobs.each do |job|
    cb_build_in? ? job.persist_and_perform_async! : Dwf::Callback.new.start(job)
  end
end

#started?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/dwf/workflow.rb', line 110

def started?
  !!started_at
end

#statusObject



126
127
128
129
130
131
132
133
# File 'lib/dwf/workflow.rb', line 126

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

  :running
end

#stopped?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/dwf/workflow.rb', line 122

def stopped?
  stopped
end

#to_hashObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dwf/workflow.rb', line 85

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
  }
end