Module: Resque::Plugins::Status::ClassOverridesAndExtensions

Defined in:
lib/resque_manager/overrides/resque_status/status.rb

Instance Method Summary collapse

Instance Method Details

#counter(counter, uuid) ⇒ Object



63
64
65
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 63

def counter(counter, uuid)
  Resque.redis[counter_key(counter, uuid)].to_i
end

#counter_key(counter, uuid) ⇒ Object

If multiple workers are running at once and you need an incrementer, you can’t use the status’ num attribute because of race conditions. You can use a counter and call incr on it instead



59
60
61
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 59

def counter_key(counter, uuid)
  "#{counter}:#{uuid}"
end

#enqueue_to(queue, klass, options = {}) ⇒ Object

OVERRIDE to set the name that will be displayed on the status page for this job when it is first queued. The name will be changed when set_status is called, which is called on #tick, to the value set in your name method, but the UI name field is blank when it is first queued, so setting it here so we have something.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 23

def enqueue_to(queue, klass, options = {})
  uuid = Resque::Plugins::Status::Hash.generate_uuid
  Resque::Plugins::Status::Hash.create uuid, {name: "#{self.name}: #{options.inspect}"}.merge(options)

  if Resque.enqueue_to(queue, klass, uuid, options)
    uuid
  else
    Resque::Plugins::Status::Hash.remove(uuid)
    nil
  end
end

#incr_counter(counter, uuid) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 67

def incr_counter(counter, uuid)
  key = counter_key(counter, uuid)
  n = Resque.redis.incr(key)
  if Resque::Plugins::Status::Hash.expire_in
    Resque.redis.expire(key, Resque::Plugins::Status::Hash.expire_in)
  end
  n
end

#perform(uuid = nil, options = {}) ⇒ Object

This is the method called by Resque::Worker when processing jobs. It creates a new instance of the job class and populates it with the uuid and options.

You should not override this method, rather the perform instance method. OVERRIDE to get the worker and set when initializing the class



41
42
43
44
45
46
47
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 41

def perform(uuid=nil, options = {})
  uuid ||= Resque::Plugins::Status::Hash.generate_uuid
  worker = yield if block_given?
  instance = new(uuid, worker, options)
  instance.safe_perform!
  instance
end

#remove(uuid) ⇒ Object

OVERRIDE to clear all the keys that have the UUI. status, counters, etc.



50
51
52
53
54
55
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 50

def remove(uuid)
  Resque.redis.zrem(Resque::Plugins::Status::Hash.set_key, uuid)
  Resque.redis.keys("*#{uuid}").each do |key|
    Resque.redis.del(key)
  end
end