Module: ResqueJob::ClassMethods

Defined in:
lib/resque_job.rb

Instance Method Summary collapse

Instance Method Details

#fetch_result(job_id) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/resque_job.rb', line 48

def fetch_result(job_id)
  res = Resque.redis.multi do
    key = result_key(job_id)
    Resque.redis.get key
    Resque.redis.del key
  end.first
  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
# File 'lib/resque_job.rb', line 19

def find_in_queue_by_payload(job_class, &block)
  result = Resque.peek(:default, 0, 0)
             .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



28
29
30
31
32
33
34
# File 'lib/resque_job.rb', line 28

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/resque_job.rb', line 36

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



57
58
59
# File 'lib/resque_job.rb', line 57

def result_key(job_id)
  [Resque.redis.namespace, 'result', name.underscore, job_id].join(':')
end

#status(job_id) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/resque_job.rb', line 61

def status(job_id)
  if find_in_queue(job_id)
    :waiting
  elsif find_in_working(job_id)
    :running
  else
    :done
  end
end