Class: Delayed::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/delayed_job_extras/job.rb,
lib/delayed_job_extras/extras.rb,
lib/delayed_job_test_enhancements.rb,
lib/delayed_job_extras/unique_dj_validator.rb,
lib/delayed_job_extras/validate_with_unique.rb

Defined Under Namespace

Modules: Extras

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enqueue_with_work_off(obj, *args) ⇒ Object



7
8
9
10
11
# File 'lib/delayed_job_test_enhancements.rb', line 7

def enqueue_with_work_off(obj, *args)
  job = enqueue_without_work_off(obj, *args)
  job.invoke_job
  return job
end

.new_with_worker_class_name(options = {}) ⇒ Object



71
72
73
74
# File 'lib/delayed_job_extras/job.rb', line 71

def new_with_worker_class_name(options = {})
  worker_class_name = options[:payload_object].respond_to?(:worker_class_name) ? options[:payload_object].worker_class_name : 'unknown'
  new_without_worker_class_name({:worker_class_name => worker_class_name}.merge(options))
end

Instance Method Details

#deserialize_with_extras(source) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/delayed_job_extras/job.rb', line 12

def deserialize_with_extras(source)
  begin
    source.scan(/!ruby\/object:(\S+)\s/).flatten.each do |x| 
      x.strip!
      x.to_s.constantize
    end
  rescue Exception => e
    raise e
  end
  deserialize_without_extras(source)
end

#enqueue_againObject



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

def enqueue_again
  if self.payload_object.re_enqueuable
    new_worker = self.payload_object.clone()
    if self.payload_object.__re_enqueue_block
      self.payload_object.__re_enqueue_block.call(self.payload_object, new_worker)
    end
    new_worker.enqueue
  end
end

#finished?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/delayed_job_extras/job.rb', line 65

def finished?
  !self.started_at.nil? && !self.finished_at.nil?
end

#invoke_job_with_extrasObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/delayed_job_extras/job.rb', line 26

def invoke_job_with_extras
  begin
    self.payload_object.dj_object = self
    self.touch(:started_at)
    DJ::Worker.logger.info("Starting #{self.payload_object.class.name}#perform (DJ.id = '#{self.id}')")
    
    invoke_job_without_extras
    
    DJ::Worker.logger.info("Completed #{self.payload_object.class.name}#perform (DJ.id = '#{self.id}') [SUCCESS]")
    self.touch(:finished_at)
  rescue Exception => e
    DJ::Worker.logger.error("Halted #{self.payload_object.class.name}#perform (DJ.id = '#{self.id}') [FAILURE]")
    self.update_attributes(:started_at => nil)
    raise e
  end
  enqueue_again
end

#pending?Boolean

Returns:

  • (Boolean)


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

def pending?
  self.started_at.nil? && self.finished_at.nil?
end

#reset!Object



4
5
6
7
8
9
10
# File 'lib/delayed_job_extras/job.rb', line 4

def reset!
  self.run_at = Time.now
  self.attempts = 0
  self.last_error = nil
  self.save!
  self.reload
end

#running?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/delayed_job_extras/job.rb', line 61

def running?
  !self.started_at.nil? && self.finished_at.nil?
end

#validate_with_uniqueObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/delayed_job_extras/validate_with_unique.rb', line 4

def validate_with_unique
  validate_without_unique
  if self.payload_object.respond_to?(:unique?) && self.new_record?
    if self.payload_object.unique?
      if Delayed::Job.count(:all, :conditions => {:worker_class_name => self.worker_class_name, :finished_at => nil}) > 0
        self.errors.add_to_base("Only one #{self.worker_class_name} can be queued at a time!")
      end
    end
  end
end