module ResqueJob::ClassMethods

Public Instance Methods

fetch_result(job_id) click to toggle source
# File lib/resque_job.rb, line 49
def fetch_result(job_id)
  res = Resque.redis.get result_key(job_id)
  JSON.parse(res) rescue res
end
find(job_id) click to toggle source
# File lib/resque_job.rb, line 9
def find(job_id)
  find_in_queue(job_id) || find_in_working(job_id)
end
find_in_queue(job_id) click to toggle source
# File lib/resque_job.rb, line 13
def find_in_queue(job_id)
  Resque.peek(queue_name, 0, 0)
    .map { |job| job['args'].first }
    .detect { |job| job['job_id'] == job_id }
end
find_in_queue_by_payload(job_class, &block) click to toggle source
# File lib/resque_job.rb, line 19
def find_in_queue_by_payload(job_class, &block)
  jobs = Array.wrap Resque.peek(queue_name, 0, 0)
  result = jobs
             .select { |j| j['args'].first['job_class'] == job_class.to_s }
             .flat_map { |j| j['args'] }
  return result unless block_given?

  result.detect(&block)
end
find_in_working(job_id) click to toggle source
# File lib/resque_job.rb, line 29
def find_in_working(job_id)
  Resque::Worker.working.map(&:job).detect do |job|
    if job.is_a?(Hash) && (args = job.dig 'payload', 'args').is_a?(Array)
      args.detect { |x| x['job_id'] == job_id }
    end
  end
end
find_in_working_by_payload(job_class, &block) click to toggle source
# File lib/resque_job.rb, line 37
def find_in_working_by_payload(job_class, &block)
  result =
    Resque::Worker.working.map(&:job).flat_map do |job|
      next unless job.is_a?(Hash) && (args = job.dig 'payload', 'args').is_a?(Array)

      args.select { |x| x['job_class'] == job_class.to_s }
    end.compact
  return result unless block_given?

  result.detect(&block)
end
result_key(job_id) click to toggle source
# File lib/resque_job.rb, line 54
def result_key(job_id)
  [Resque.redis.namespace, 'result', name.underscore, job_id].join(':')
end
status(job_id) click to toggle source
# File lib/resque_job.rb, line 58
def status(job_id)
  if find_in_queue(job_id)
    :waiting
  elsif find_in_working(job_id)
    :running
  else
    :done
  end
end