class Sidekiq::Hierarchy::Workflow

Attributes

root[R]

Public Class Methods

find(root)
Alias for: new
find_by_jid(root_jid) click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 15
def find_by_jid(root_jid)
  find(Job.find(root_jid))
end
new(root) click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 8
def initialize(root)
  @root = root
end
Also aliased as: find

Public Instance Methods

==(other_workflow) click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 22
def ==(other_workflow)
  other_workflow.instance_of?(self.class) &&
    self.jid == other_workflow.jid
end
complete?() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 79
def complete?
  status == :complete
end
complete_at() click to toggle source

Returns the time at which all jobs were complete; nil if any jobs are still incomplete

# File lib/sidekiq/hierarchy/workflow.rb, line 100
def complete_at
  Time.at(self[Job::WORKFLOW_FINISHED_AT_FIELD].to_f) if complete?
end
delete() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 31
def delete
  wset = workflow_set  # save it for later
  root.delete  # deleting nodes is more important than a dangling reference
  wset.remove(self) if wset  # now we can clear out from the set
end
enqueued_at() click to toggle source

Calculated metrics

# File lib/sidekiq/hierarchy/workflow.rb, line 90
def enqueued_at
  root.enqueued_at
end
failed?() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 83
def failed?
  status == :failed
end
failed_at() click to toggle source

Returns the earliest time at which a job failed; nil if none did

# File lib/sidekiq/hierarchy/workflow.rb, line 106
def failed_at
  Time.at(self[Job::WORKFLOW_FINISHED_AT_FIELD].to_f) if failed?
end
finished_at() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 110
def finished_at
  if timestamp = self[Job::WORKFLOW_FINISHED_AT_FIELD]
    Time.at(timestamp.to_f)
  end
end
run_at() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 94
def run_at
  root.run_at
end
running?() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 75
def running?
  status == :running
end
status() click to toggle source

Status

# File lib/sidekiq/hierarchy/workflow.rb, line 43
def status
  case self[Job::WORKFLOW_STATUS_FIELD]
  when Job::STATUS_RUNNING
    :running
  when Job::STATUS_COMPLETE
    :complete
  when Job::STATUS_FAILED
    :failed
  else
    :unknown
  end
end
to_s() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 121
def to_s
  Sidekiq.dump_json(self.as_json)
end
update_status(from_job_status) click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 56
def update_status(from_job_status)
  old_status = status
  return if [:failed, :complete].include?(old_status)  # these states are final

  if [:enqueued, :running, :requeued].include?(from_job_status)
    new_status, s_val = :running, Job::STATUS_RUNNING
  elsif from_job_status == :failed
    new_status, s_val = :failed, Job::STATUS_FAILED
  elsif from_job_status == :complete && root.subtree_size == root.finished_subtree_size
    new_status, s_val = :complete, Job::STATUS_COMPLETE
  end
  return if !new_status || new_status == old_status  # don't publish null updates

  self[Job::WORKFLOW_STATUS_FIELD] = s_val
  self[Job::WORKFLOW_FINISHED_AT_FIELD] = Time.now.to_f.to_s if [:failed, :complete].include?(new_status)

  Sidekiq::Hierarchy.publish(Notifications::WORKFLOW_UPDATE, self, new_status, old_status)
end
workflow_set() click to toggle source
# File lib/sidekiq/hierarchy/workflow.rb, line 27
def workflow_set
  WorkflowSet.for_status(status)
end