Commit 41fadb5a71acfaa8eeb112a0650b7fb46f131be4

Authored by Rodrigo Souto
1 parent e7abfbba

Moving delayed job changes to a monkey patch

script/delayed_job
... ... @@ -10,25 +10,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'envi
10 10 require 'daemons'
11 11 require 'delayed/command'
12 12  
13   -# based on https://groups.google.com/forum/#!topic/delayed_job/ZGMUFFppNgs
14   -class Delayed::Worker
15   - class ExceptionNotification < ActionMailer::Base
16   - def mail error
17   - environment = Environment.default
18   -
19   - recipients NOOSFERO_CONF['exception_recipients']
20   - from environment.contact_email
21   - reply_to environment.contact_email
22   - subject "[#{environment.name}] DelayedJob: #{error.message}"
23   - body render(:text => error.backtrace.join("\n"))
24   - end
25   - end
26   -
27   - def handle_failed_job_with_notification job, error
28   - Delayed::Worker::ExceptionNotification.deliver_mail error if NOOSFERO_CONF['exception_recipients'].present?
29   - handle_failed_job_without_notification job, error
30   - end
31   - alias_method_chain :handle_failed_job, :notification
32   -end
33   -
34 13 Delayed::Command.new(ARGV).daemonize
... ...
vendor/plugins/delayed_job/lib/delayed/worker.rb
... ... @@ -167,7 +167,6 @@ module Delayed
167 167 job.last_error = error.message + "\n" + error.backtrace.join("\n")
168 168 say "#{job.name} failed with #{error.class.name}: #{error.message} - #{job.attempts} failed attempts", Logger::ERROR
169 169 reschedule(job)
170   - rescue => e # don't crash here
171 170 end
172 171  
173 172 # Run the next job we can get an exclusive lock on.
... ... @@ -187,8 +186,6 @@ module Delayed
187 186 end
188 187  
189 188 run(job) if job
190   - rescue => e
191   - handle_failed_job(job, e)
192 189 end
193 190 end
194 191 end
... ...
vendor/plugins/monkey_patches/rescue_delayed_job_crashes/init.rb 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +Delayed::Worker.module_eval do
  2 + # based on https://groups.google.com/forum/#!topic/delayed_job/ZGMUFFppNgs
  3 + class Delayed::Worker::ExceptionNotification < ActionMailer::Base
  4 + def mail error
  5 + environment = Environment.default
  6 +
  7 + recipients NOOSFERO_CONF['exception_recipients']
  8 + from environment.contact_email
  9 + reply_to environment.contact_email
  10 + subject "[#{environment.name}] DelayedJob: #{error.message}"
  11 + body render(:text => error.backtrace.join("\n"))
  12 + end
  13 + end
  14 +
  15 + def handle_failed_job_with_notification(job, error)
  16 + Delayed::Worker::ExceptionNotification.deliver_mail error if NOOSFERO_CONF['exception_recipients'].present?
  17 + handle_failed_job_without_notification job, error
  18 + end
  19 + alias_method_chain :handle_failed_job, :notification
  20 +
  21 + def handle_failed_job_with_rescue(job, error)
  22 + handle_failed_job_without_rescue(job, error)
  23 + rescue => e # don't crash here
  24 + end
  25 + alias_method_chain :handle_failed_job, :rescue
  26 +
  27 + protected
  28 +
  29 + # This code must be replicated because there is no other way to pass the job
  30 + # through and use alias_method_chain as we used on the previous method
  31 + def reserve_and_run_one_job
  32 + # We get up to 5 jobs from the db. In case we cannot get exclusive access to a job we try the next.
  33 + # this leads to a more even distribution of jobs across the worker processes
  34 + job = Delayed::Job.find_available(name, 5, self.class.max_run_time).detect do |job|
  35 + if job.lock_exclusively!(self.class.max_run_time, name)
  36 + say "acquired lock on #{job.name}"
  37 + true
  38 + else
  39 + say "failed to acquire exclusive lock for #{job.name}", Logger::WARN
  40 + false
  41 + end
  42 + end
  43 +
  44 + job = Delayed::Job.new
  45 + run(job) if job
  46 + rescue => e
  47 + handle_failed_job(job, e)
  48 + end
  49 +end
... ...