Module: Resque::Plugins::State::ClassMethods

Defined in:
lib/resque/plugins/state.rb

Instance Method Summary collapse

Instance Method Details

#create(options = {}) ⇒ Object

Create is the primary method for adding jobs to the queue. This would be called on the job class to create a job of that type. Any options passed are passed to the Job instance as a hash of options. It returns the UUID of the job.

Example:

class ExampleJob
  include Resque::Plugins::State

  def perform
    job_status "Hey I'm a job num #{options['num']}"
  end

end

job_id = ExampleJob.create(:num => 100)


93
94
95
# File 'lib/resque/plugins/state.rb', line 93

def create(options = {})
  enqueue(self, options)
end

#dequeue(klass, uuid) ⇒ Object

Removes a job of type <tt>klass<tt> from the queue.

The initially given options are retrieved from the status hash. (Resque needs the options to find the correct queue entry)



126
127
128
129
# File 'lib/resque/plugins/state.rb', line 126

def dequeue(klass, uuid)
  status = Resque::Plugins::State::Hash.get(uuid)
  Resque.dequeue(klass, uuid, status.options)
end

#enqueue(klass, options = {}) ⇒ Object

Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>.

Returns the UUID of the job if the job was queued, or nil if the job was rejected by a before_enqueue hook.



101
102
103
# File 'lib/resque/plugins/state.rb', line 101

def enqueue(klass, options = {})
  enqueue_to(Resque.queue_from_class(klass) || queue, klass, options)
end

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

Adds a job of type <tt>klass<tt> to a specified queue with <tt>options<tt>.

Returns the UUID of the job if the job was queued, or nil if the job was rejected by a before_enqueue hook.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/resque/plugins/state.rb', line 110

def enqueue_to(queue, klass, options = {})
  uuid = Resque::Plugins::State::Hash.generate_uuid
  Resque::Plugins::State::Hash.create uuid, options: options

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

#nameObject

used when displaying the Job in the resque-web UI and identifiyng the job type by status. By default this is the name of the job class, but can be overidden in the specific job class to present a more user friendly job name



71
72
73
# File 'lib/resque/plugins/state.rb', line 71

def name
  to_s
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, rahter the perform instance method.



137
138
139
140
141
142
# File 'lib/resque/plugins/state.rb', line 137

def perform(uuid = nil, options = {})
  uuid ||= Resque::Plugins::State::Hash.generate_uuid
  instance = new(uuid, options)
  instance.safe_perform!
  instance
end

#queueObject

The default queue is :statused, this can be ovveridden in the specific job class to put the jobs on a specific worker queue



63
64
65
# File 'lib/resque/plugins/state.rb', line 63

def queue
  :statused
end

#scheduled(queue, _klass, *args) ⇒ Object

Wrapper API to forward a Resque::Job creation API call into a Resque::Plugins::State call. This is needed to be used with resque scheduler github.com/bvandenbos/resque-scheduler



148
149
150
# File 'lib/resque/plugins/state.rb', line 148

def scheduled(queue, _klass, *args)
  enqueue_to(queue, self, *args)
end