Class: Delayed::Worker

Inherits:
Object
  • Object
show all
Extended by:
Delayed
Defined in:
lib/delayed_job_memento/delayed/worker.rb

Constant Summary collapse

DEFAULT_LOG_LEVEL =
Logger::INFO

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.backend=(backend) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/delayed_job_memento/delayed/worker.rb', line 8

def self.backend=(backend)
  if backend.is_a? Symbol
    require "delayed/serialization/#{backend}"
    require "delayed/backend/#{backend}"
    backend = "Delayed::Backend::#{backend.to_s.classify}::Job".constantize
  end
  @@backend = backend
  silence_warnings { ::Delayed.const_set(:Job, backend) }
end

Instance Method Details

#failed(job) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/delayed_job_memento/delayed/worker.rb', line 48

def failed(job)
  self.class.lifecycle.run_callbacks(:failure, self, job) do
    job.hook(:failure)
    if self.class.destroy_failed_jobs
      save_as_memento(job)
      job.destroy
    else
      job.fail!
    end
  end
end

#job_say(job, text, level = DEFAULT_LOG_LEVEL) ⇒ Object



60
61
62
63
# File 'lib/delayed_job_memento/delayed/worker.rb', line 60

def job_say(job, text, level = DEFAULT_LOG_LEVEL)
  text = "Job #{job.name} (id=#{job.id}) #{text}"
  say text, level
end

#run(job) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/delayed_job_memento/delayed/worker.rb', line 25

def run(job)
  instances = Delayed::Quota.instances
  if job.queue != nil && instances.has_key?(job.queue) && instances[job.queue].quota_reached?
    instances[job.queue].rebalance_queue
    return false
  else
    job_say job, 'RUNNING'
    runtime = Benchmark.realtime do
      Timeout.timeout(self.class.max_run_time.to_i, WorkerTimeout) { job.invoke_job }
      save_as_memento(job)
      job.destroy
    end
    job_say job, 'COMPLETED after %.4f' % runtime
    return true # did work
  end
rescue DeserializationError => error
  job.last_error = "#{error.message}\n#{error.backtrace.join("\n")}"
  failed(job)
rescue Exception => error
  self.class.lifecycle.run_callbacks(:error, self, job) { handle_failed_job(job, error) }
  return false # work failed
end

#save_as_memento(job) ⇒ Object



18
19
20
21
22
23
# File 'lib/delayed_job_memento/delayed/worker.rb', line 18

def save_as_memento(job)
  params = job.attributes
  attrs = ['run_at', 'priority', 'handler', 'queue', 'locked_at', 'locked_by', 'attempts', 'failed_at', 'last_error']
  params.delete_if { |k| !attrs.include? k }
  Delayed::Memento.create(params)
end