Class: Stormtroopers::DelayedJobTrooper

Inherits:
Trooper
  • Object
show all
Defined in:
lib/stormtroopers/trooper/delayed_job.rb

Instance Attribute Summary collapse

Attributes inherited from Trooper

#parameters, #task

Instance Method Summary collapse

Methods inherited from Trooper

#after_run, #before_run, #exception, #logger

Constructor Details

#initialize(job) ⇒ DelayedJobTrooper

Returns a new instance of DelayedJobTrooper.



5
6
7
# File 'lib/stormtroopers/trooper/delayed_job.rb', line 5

def initialize(job)
  @job = job
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job.



3
4
5
# File 'lib/stormtroopers/trooper/delayed_job.rb', line 3

def job
  @job
end

Instance Method Details

#max_attempts(job) ⇒ Object



30
31
32
# File 'lib/stormtroopers/trooper/delayed_job.rb', line 30

def max_attempts(job)
  job.max_attempts || Delayed::Worker.max_attempts
end

#rescheduleObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stormtroopers/trooper/delayed_job.rb', line 18

def reschedule
  if (job.attempts += 1) < max_attempts(job)
    job.run_at = job.reschedule_at
    job.unlock
    job.save!
  else
    logger.error("PERMANENTLY removing #{job.name} because of #{job.attempts} consecutive failures.")
    job.hook(:failure)
    job.fail!
  end
end

#runObject



9
10
11
12
13
14
15
16
# File 'lib/stormtroopers/trooper/delayed_job.rb', line 9

def run
  job.invoke_job
  job.destroy
rescue => error
  job.last_error = "#{error.message}\n#{error.backtrace.join("\n")}"
  logger.info "#{job.name} failed with #{error.class.name}: #{error.message} - #{job.attempts} failed attempts"
  reschedule
end