Class: DJ::Worker

Inherits:
Object
  • Object
show all
Includes:
MagicAttributes
Defined in:
lib/dj_remixes/run_at.rb,
lib/dj_remixes/unique.rb,
lib/dj_remixes/worker.rb,
lib/dj_remixes/hoptoad.rb,
lib/dj_remixes/airbrake.rb,
lib/dj_remixes/priority.rb,
lib/dj_remixes/callbacks.rb,
lib/dj_remixes/attributes.rb,
lib/dj_remixes/re_enqueue.rb

Direct Known Subclasses

Mail::Message::MailmanWorker

Constant Summary collapse

PRIORITY_LEVELS =

A helpful list of symbols to priority levels

{:urgent => -100000, :immediate => -10000, :high => -1000, :medium => -500, :normal => 0, :low => 100, :who_cares => 1000}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.re_enqueuableObject

Class level accessor to let DJ know whether it should be enqueued again after it has successfully completed.



9
10
11
# File 'lib/dj_remixes/re_enqueue.rb', line 9

def re_enqueuable
  @re_enqueuable
end

Instance Attribute Details

#priorityObject

Returns the value of attribute priority.



7
8
9
# File 'lib/dj_remixes/priority.rb', line 7

def priority
  @priority
end

#run_atObject

Returns the value of attribute run_at.



4
5
6
# File 'lib/dj_remixes/run_at.rb', line 4

def run_at
  @run_at
end

#worker_class_nameObject

Returns the value of attribute worker_class_name.



4
5
6
# File 'lib/dj_remixes/worker.rb', line 4

def worker_class_name
  @worker_class_name
end

Class Method Details

.enqueue(*args) ⇒ Object



8
9
10
# File 'lib/dj_remixes/worker.rb', line 8

def enqueue(*args)
  self.new(*args).enqueue!
end

.is_uniqueObject

Tell DJ to only allow one of this worker at a given time.

Example:

# We only want to charge the card once!
class PurchaseWorker < DJ::Worker
  is_unique

  def perform
    # charge the credit card...
  end
end


17
18
19
20
21
# File 'lib/dj_remixes/unique.rb', line 17

def is_unique
  define_method('unique?') do
    return true
  end
end

.priority(level = 0) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/dj_remixes/priority.rb', line 11

def priority(level = 0)
  define_method('priority') do
    if level.is_a?(Symbol)
      level = DJ::Worker::PRIORITY_LEVELS[level] ||= 0
    end
    return @priority ||= level
  end
end

.re_enqueueObject

A convience method to tell DJ to re-enqueue this worker after it has successfully completely. NOTE: This will actually create a new DJ object in the database, not reuse the same one.

Example:

# Run every 30 days and charge a credit card.
class SubscriptionWorker < DJ::Worker
  re_enqueue

  def run_at
    30.days.from_now
  end

  def perform
    # charge the credit card...
  end
end


28
29
30
# File 'lib/dj_remixes/re_enqueue.rb', line 28

def re_enqueue
  self.re_enqueuable = true
end

.run_at(&block) ⇒ Object



8
9
10
# File 'lib/dj_remixes/run_at.rb', line 8

def run_at(&block)
  define_method('run_at', &block)
end

Instance Method Details

#after(job) ⇒ Object



9
10
# File 'lib/dj_remixes/callbacks.rb', line 9

def after(job)
end

#before(job) ⇒ Object



4
5
6
7
# File 'lib/dj_remixes/callbacks.rb', line 4

def before(job)
  self.dj_object = job
  job.touch(:started_at)
end

#cloneObject

:nodoc:



45
46
47
48
49
# File 'lib/dj_remixes/worker.rb', line 45

def clone # :nodoc:
  cl = super
  cl.run_at = nil
  cl
end

#dj_objectObject



18
19
20
# File 'lib/dj_remixes/worker.rb', line 18

def dj_object
  DJ.find(@dj_object)
end

#dj_object=(dj) ⇒ Object



14
15
16
# File 'lib/dj_remixes/worker.rb', line 14

def dj_object=(dj)
  @dj_object = dj.id
end

#enqueue!(priority = self.priority, run_at = self.run_at) ⇒ Object Also known as: save



30
31
32
33
34
35
# File 'lib/dj_remixes/worker.rb', line 30

def enqueue!(priority = self.priority, run_at = self.run_at)
  job = DJ.enqueue(:payload_object => self, :priority => priority, :run_at => run_at)
  job.worker_class_name = self.worker_class_name
  job.save
  return job
end

#error(job, error) ⇒ Object



17
18
19
# File 'lib/dj_remixes/callbacks.rb', line 17

def error(job, error)
  job.update_attributes(:started_at => nil)
end

#error_with_airbrake(job, error) ⇒ Object

Report Errors to Airbrake:



6
7
8
9
# File 'lib/dj_remixes/airbrake.rb', line 6

def error_with_airbrake(job, error)
  Airbrake.notify_or_ignore(error, :cgi_data => self.dj_object.attributes)
  error_without_airbrake(job, error)
end

#error_with_hoptoad(job, error) ⇒ Object

Report Errors to Hoptoad:



6
7
8
9
# File 'lib/dj_remixes/hoptoad.rb', line 6

def error_with_hoptoad(job, error)
  HoptoadNotifier.notify_or_ignore(error, :cgi_data => self.dj_object.attributes)
  error_without_hoptoad(job, error)
end

#performObject

Needs to be implemented by subclasses! It’s only here so people can hook into it.

Raises:

  • (NoMethodError)


41
42
43
# File 'lib/dj_remixes/worker.rb', line 41

def perform
  raise NoMethodError.new('perform')
end

#success(job) ⇒ Object



12
13
14
15
# File 'lib/dj_remixes/callbacks.rb', line 12

def success(job)
  job.touch(:finished_at)
  self.enqueue_again if self.respond_to?(:enqueue_again)
end

#unique?Boolean

Returns true or false based on whether this should be the only worker that should be allowed in the system at any one time. If set to true, using the is_unique class method, then when a new job is enqueued it will be validated against the worker_class_name field in the database. If there is already a job with the same worker_class_name then the enqueing of the new job will fail.

Returns:

  • (Boolean)


31
32
33
# File 'lib/dj_remixes/unique.rb', line 31

def unique?
  false
end