class Gush::Workflow

Attributes

arguments[RW]
id[RW]
jobs[RW]
persisted[RW]
stopped[RW]

Public Class Methods

create(*args) click to toggle source
# File lib/gush/workflow.rb, line 22
def self.create(*args)
  flow = new(*args)
  flow.save
  flow
end
descendants() click to toggle source
# File lib/gush/workflow.rb, line 179
def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end
find(id) click to toggle source
# File lib/gush/workflow.rb, line 18
def self.find(id)
  Gush::Client.new.find_workflow(id)
end
new(*args) click to toggle source
# File lib/gush/workflow.rb, line 7
def initialize(*args)
  @id = id
  @jobs = []
  @dependencies = []
  @persisted = false
  @stopped = false
  @arguments = args

  setup
end

Public Instance Methods

configure(*args) click to toggle source
# File lib/gush/workflow.rb, line 41
def configure(*args)
end
continue() click to toggle source
# File lib/gush/workflow.rb, line 28
def continue
  client = Gush::Client.new
  failed_jobs = jobs.select(&:failed?)

  failed_jobs.each do |job|
    client.enqueue_job(id, job)
  end
end
failed?() click to toggle source
# File lib/gush/workflow.rb, line 96
def failed?
  jobs.any?(&:failed?)
end
find_job(name) click to toggle source
# File lib/gush/workflow.rb, line 74
def find_job(name)
  match_data = /(?<klass>\w*[^-])-(?<identifier>.*)/.match(name.to_s)
  if match_data.nil?
    job = jobs.find { |node| node.class.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/gush/workflow.rb, line 84
def finished?
  jobs.all?(&:finished?)
end
finished_at() click to toggle source
# File lib/gush/workflow.rb, line 154
def finished_at
  last_job ? last_job.finished_at : nil
end
initial_jobs() click to toggle source
# File lib/gush/workflow.rb, line 131
def initial_jobs
  jobs.select(&:has_no_dependencies?)
end
mark_as_persisted() click to toggle source
# File lib/gush/workflow.rb, line 56
def mark_as_persisted
  @persisted = true
end
mark_as_started() click to toggle source
# File lib/gush/workflow.rb, line 60
def mark_as_started
  @stopped = false
end
mark_as_stopped() click to toggle source
# File lib/gush/workflow.rb, line 44
def mark_as_stopped
  @stopped = true
end
persist!() click to toggle source
# File lib/gush/workflow.rb, line 52
def persist!
  client.persist_workflow(self)
end
reload() click to toggle source
# File lib/gush/workflow.rb, line 127
def reload
  self.class.find(id)
end
resolve_dependencies() click to toggle source
# File lib/gush/workflow.rb, line 64
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
run(klass, opts = {}) click to toggle source
# File lib/gush/workflow.rb, line 104
def run(klass, opts = {})
  options =

  node = klass.new(self, {
    name: client.next_free_job_id(id,klass.to_s),
    params: opts.fetch(:params, {})
  })

  jobs << node

  deps_after = [*opts[:after]]
  deps_after.each do |dep|
    @dependencies << {from: dep.to_s, to: node.name.to_s }
  end

  deps_before = [*opts[:before]]
  deps_before.each do |dep|
    @dependencies << {from: node.name.to_s, to: dep.to_s }
  end

  node.name
end
running?() click to toggle source
# File lib/gush/workflow.rb, line 92
def running?
  started? && !finished?
end
save() click to toggle source
# File lib/gush/workflow.rb, line 37
def save
  persist!
end
start!() click to toggle source
# File lib/gush/workflow.rb, line 48
def start!
  client.start_workflow(self)
end
started?() click to toggle source
# File lib/gush/workflow.rb, line 88
def started?
  !!started_at
end
started_at() click to toggle source
# File lib/gush/workflow.rb, line 150
def started_at
  first_job ? first_job.started_at : nil
end
status() click to toggle source
# File lib/gush/workflow.rb, line 135
def status
  case
    when failed?
      :failed
    when running?
      :running
    when finished?
      :finished
    when stopped?
      :stopped
    else
      :running
  end
end
stopped?() click to toggle source
# File lib/gush/workflow.rb, line 100
def stopped?
  stopped
end
to_hash() click to toggle source
# File lib/gush/workflow.rb, line 158
def to_hash
  name = self.class.to_s
  {
    name: name,
    id: id,
    arguments: @arguments,
    total: jobs.count,
    finished: jobs.count(&:finished?),
    klass: name,
    jobs: jobs.map(&:as_json),
    status: status,
    stopped: stopped,
    started_at: started_at,
    finished_at: finished_at
  }
end
to_json(options = {}) click to toggle source
# File lib/gush/workflow.rb, line 175
def to_json(options = {})
  Gush::JSON.encode(to_hash)
end

Private Instance Methods

client() click to toggle source
# File lib/gush/workflow.rb, line 194
def client
  @client ||= Client.new
end
first_job() click to toggle source
# File lib/gush/workflow.rb, line 198
def first_job
  jobs.min_by{ |n| n.started_at || Time.now.to_i }
end
last_job() click to toggle source
# File lib/gush/workflow.rb, line 202
def last_job
  jobs.max_by{ |n| n.finished_at || 0 } if finished?
end
setup() click to toggle source
# File lib/gush/workflow.rb, line 189
def setup
  configure(*@arguments)
  resolve_dependencies
end