Module: Resque::Integration::Continuous

Defined in:
lib/resque/integration/continuous.rb

Overview

Continuous job can re-enqueue self with respect to resque-lock and resque-meta.

Examples:

class ResqueJob
  include Resque::Integration

  unique
  continuous

  def self.execute(id)
    chunk = Company.find(id).products.limit(1000)

    if chunk.size > 0
      heavy_work
      continue # it will re-enqueue the job with the same arguments. Avoid infinite loops!
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#after_perform_continue(*args) ⇒ Object

‘after` callbacks are executed after `around` callbacks so here we can re-enqueue the job, because lock (from resque-lock) already released



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/resque/integration/continuous.rb', line 47

def after_perform_continue(*args)
  if continued?
    args = if @continued.any?
      if unique?
        meta_id = args.first # we should keep meta_id as first argument
        [meta_id] + @continued
      else
        @continued
      end
    else
      args
    end

    ::Resque.enqueue(self, *args)
  end
end

#after_perform_reset_metaObject

This callback resets Meta’s finish flags



29
30
31
32
33
34
35
36
37
38
# File 'lib/resque/integration/continuous.rb', line 29

def after_perform_reset_meta(*)
  if should_reset_meta?
    meta_obj = meta

    meta_obj.data.delete('succeeded')
    meta_obj.data.delete('finished_at')

    meta_obj.save
  end
end

#before_perform_continueObject

Just to ensure that previous jobs won’t affect current



41
42
43
# File 'lib/resque/integration/continuous.rb', line 41

def before_perform_continue(*)
  @continued = nil
end