Module: ResqueJob::ClassMethods

Defined in:
lib/resque_job.rb

Instance Method Summary collapse

Instance Method Details

#fetch_result(job_id) ⇒ Object



49
50
51
52
# 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) ⇒ Object



9
10
11
# 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) ⇒ Object



13
14
15
16
17
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
# 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