class Dwf::Workflow

Constants

CALLBACK_TYPES

Attributes

arguments[R]
callback_type[RW]
dependencies[R]
finished_at[R]
id[RW]
jobs[RW]
persisted[R]
started_at[R]
stopped[RW]

Public Class Methods

create(*args) click to toggle source
# File lib/dwf/workflow.rb, line 17
def create(*args)
  flow = new(*args)
  flow.save
  flow
end
find(id) click to toggle source
# File lib/dwf/workflow.rb, line 23
def find(id)
  Dwf::Client.new.find_workflow(id)
end
new(*args) click to toggle source
# File lib/dwf/workflow.rb, line 28
def initialize(*args)
  @dependencies = []
  @id = build_id
  @jobs = []
  @persisted = false
  @stopped = false
  @arguments = args
  @callback_type = BUILD_IN

  setup
end

Public Instance Methods

as_json() click to toggle source
# File lib/dwf/workflow.rb, line 119
def as_json
  to_hash.to_json
end
build_id() click to toggle source
# File lib/dwf/workflow.rb, line 69
def build_id
  client.build_workflow_id
end
cb_build_in?() click to toggle source
# File lib/dwf/workflow.rb, line 65
def cb_build_in?
  callback_type == BUILD_IN
end
configure(*arguments) click to toggle source
# File lib/dwf/workflow.rb, line 73
def configure(*arguments); end
failed?() click to toggle source
# File lib/dwf/workflow.rb, line 135
def failed?
  jobs.any?(&:failed?)
end
find_job(name) click to toggle source
# File lib/dwf/workflow.rb, line 90
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?() click to toggle source
# File lib/dwf/workflow.rb, line 123
def finished?
  jobs.all?(&:finished?)
end
mark_as_persisted() click to toggle source
# File lib/dwf/workflow.rb, line 152
def mark_as_persisted
  @persisted = true
end
mark_as_started() click to toggle source
# File lib/dwf/workflow.rb, line 156
def mark_as_started
  @stopped = false
end
persist!() click to toggle source
# File lib/dwf/workflow.rb, line 40
def persist!
  client.persist_workflow(self)
  jobs.each(&:persist!)
  mark_as_persisted
  true
end
Also aliased as: save
reload() click to toggle source
# File lib/dwf/workflow.rb, line 57
def reload
  flow = self.class.find(id)
  self.stopped = flow.stopped
  self.jobs = flow.jobs

  self
end
run(klass, options = {}) click to toggle source
# File lib/dwf/workflow.rb, line 75
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?() click to toggle source
# File lib/dwf/workflow.rb, line 131
def running?
  started? && !finished?
end
save()
Alias for: persist!
start!() click to toggle source
# File lib/dwf/workflow.rb, line 49
def start!
  mark_as_started
  persist!
  initial_jobs.each do |job|
    cb_build_in? ? job.persist_and_perform_async! : Dwf::Callback.new.start(job)
  end
end
started?() click to toggle source
# File lib/dwf/workflow.rb, line 127
def started?
  !!started_at
end
status() click to toggle source
# File lib/dwf/workflow.rb, line 143
def status
  return :failed if failed?
  return :running if running?
  return :finished if finished?
  return :stopped if stopped?

  :running
end
stopped?() click to toggle source
# File lib/dwf/workflow.rb, line 139
def stopped?
  stopped
end
to_hash() click to toggle source
# File lib/dwf/workflow.rb, line 102
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

Private Instance Methods

build_dependencies_structure(node, options) click to toggle source
# File lib/dwf/workflow.rb, line 181
def build_dependencies_structure(node, options)
  deps_after = [*options[:after]]

  deps_after.each do |dep|
    @dependencies << { from: dep.to_s, to: node.name.to_s }
  end

  deps_before = [*options[:before]]

  deps_before.each do |dep|
    @dependencies << { from: node.name.to_s, to: dep.to_s }
  end
end
client() click to toggle source
# File lib/dwf/workflow.rb, line 195
def client
  @client ||= Dwf::Client.new
end
initial_jobs() click to toggle source
# File lib/dwf/workflow.rb, line 162
def initial_jobs
  jobs.select(&:no_dependencies?)
end
resolve_dependencies() click to toggle source
# File lib/dwf/workflow.rb, line 171
def resolve_dependencies
  @dependencies.each do |dependency|
    from = find_job(dependency[:from])
    to   = find_job(dependency[:to])

    to.incoming << dependency[:from]
    from.outgoing << dependency[:to]
  end
end
setup() click to toggle source
# File lib/dwf/workflow.rb, line 166
def setup
  configure(*arguments)
  resolve_dependencies
end