module Resque::Plugins::JobHistory::ClassMethods

The class methods added to the job class that is being enqueued and whose history is to be recorded.

Public Instance Methods

around_perform_job_history(*args) { || ... } click to toggle source
# File lib/resque/plugins/job_history.rb, line 53
def around_perform_job_history(*args)
  start_time           = Time.now
  running_job          = Resque::Plugins::JobHistory::Job.new(active_job_class_name(*args), SecureRandom.uuid)
  self.most_recent_job = running_job

  begin
    running_job.start(*active_job_args(*args))

    yield if block_given?

    running_job.finish(start_time, *args)
  rescue StandardError => exception
    running_job.failed exception, start_time, *args
    raise
  ensure
    if running_job.present? && !running_job.finished? && !running_job.error
      running_job.cancel(" Job did not signal completion on finish.", start_time, *args)
    end

    self.most_recent_job = nil
  end
end
exclude_from_linear_history() click to toggle source
# File lib/resque/plugins/job_history.rb, line 88
def exclude_from_linear_history
  @exclude_from_linear_history ||= false
end
job_history() click to toggle source
# File lib/resque/plugins/job_history.rb, line 92
def job_history
  Resque::Plugins::JobHistory::HistoryDetails.new(name)
end
job_history_len() click to toggle source
# File lib/resque/plugins/job_history.rb, line 76
def job_history_len
  @job_history_len ||= Resque::Plugins::JobHistory::MAX_JOB_HISTORY
end
most_recent_job() click to toggle source
# File lib/resque/plugins/job_history.rb, line 100
def most_recent_job
  @most_recent_job
end
most_recent_job=(job) click to toggle source
# File lib/resque/plugins/job_history.rb, line 96
def most_recent_job=(job)
  @most_recent_job = job
end
on_failure_job_history(error, *args) click to toggle source
# File lib/resque/plugins/job_history.rb, line 41
def on_failure_job_history(error, *args)
  job_class_name = active_job_class_name(*args)
  job_args       = *active_job_args(*args)

  failed_job = find_failed_job(job_args, job_class_name)

  return unless failed_job
  return if failed_job.finished? && failed_job.error.present?

  failed_job.failed(error)
end
page_size() click to toggle source
# File lib/resque/plugins/job_history.rb, line 84
def page_size
  @page_size ||= Resque::Plugins::JobHistory::PAGE_SIZE
end
purge_age() click to toggle source
# File lib/resque/plugins/job_history.rb, line 80
def purge_age
  @purge_jobs_after ||= Resque::Plugins::JobHistory::PURGE_AGE
end

Private Instance Methods

active_job_args(*args) click to toggle source
# File lib/resque/plugins/job_history.rb, line 115
def active_job_args(*args)
  if Object.const_defined?("ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper") &&
      self >= "ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper".constantize
    args[-1]["arguments"]
  else
    args
  end
end
active_job_class_name(*args) click to toggle source
# File lib/resque/plugins/job_history.rb, line 106
def active_job_class_name(*args)
  if Object.const_defined?("ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper") &&
      self >= "ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper".constantize
    args[-1]["job_class"]
  else
    name
  end
end
find_failed_job(job_args, job_class_name) click to toggle source
# File lib/resque/plugins/job_history.rb, line 124
def find_failed_job(job_args, job_class_name)
  recent_job = most_recent_job

  if recent_job&.class_name == job_class_name && recent_job&.args == job_args
    recent_job
  else
    running_list  = HistoryList.new(job_class_name, "running")
    possible_jobs = running_list.jobs.select { |job| job.args == job_args }

    possible_jobs.length == 1 ? possible_jobs.first : nil
  end
end