Commit c9a234cad88608cadc7388aab7eb4f553bd67a2d

Authored by Braulio Bhavamitra
1 parent 0096a365

delayed_job: port exception notifications to rails 3

config/initializers/delayed_job_config.rb
@@ -23,3 +23,13 @@ end @@ -23,3 +23,13 @@ end
23 # end 23 # end
24 # alias_method_chain :handle_failed_job, :loggin 24 # alias_method_chain :handle_failed_job, :loggin
25 #end 25 #end
  26 +
  27 +# Chain delayed job's handle_failed_job method to do exception notification
  28 +Delayed::Worker.class_eval do
  29 + def handle_failed_job_with_notification job, error
  30 + handle_failed_job_without_notification job, error
  31 + ExceptionNotifier.notify_exception error, exception_recipients: NOOSFERO_CONF['exception_recipients'],
  32 + data: {job: job, handler: job.handler} rescue nil
  33 + end
  34 + alias_method_chain :handle_failed_job, :notification
  35 +end
vendor/plugins/delayed_job/lib/delayed/backend/base.rb
@@ -73,9 +73,8 @@ module Delayed @@ -73,9 +73,8 @@ module Delayed
73 ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/ 73 ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/
74 74
75 def name 75 def name
76 - @name ||= payload_object.respond_to?(:display_name) ?  
77 - payload_object.display_name :  
78 - payload_object.class.name 76 + obj = payload_object
  77 + @name ||= obj.respond_to?(:display_name) ? obj.display_name : obj.class.name
79 rescue DeserializationError 78 rescue DeserializationError
80 ParseObjectFromYaml.match(handler)[1] 79 ParseObjectFromYaml.match(handler)[1]
81 end 80 end
vendor/plugins/monkey_patches/init.rb
1 -require File.join(File.dirname(__FILE__), 'attachment_fu_validates_attachment/init')  
2 -require File.join(File.dirname(__FILE__), 'attachment_fu/init')  
3 -require File.join(File.dirname(__FILE__), 'white_list_sanitizer_unescape_before_reescape/init') 1 +require_relative 'attachment_fu_validates_attachment/init'
  2 +require_relative 'attachment_fu/init'
  3 +require_relative 'white_list_sanitizer_unescape_before_reescape/init'
vendor/plugins/monkey_patches/rescue_delayed_job_crashes/init.rb
@@ -1,57 +0,0 @@ @@ -1,57 +0,0 @@
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 job, error  
5 - environment = Environment.default  
6 -  
7 - recipients NOOSFERO_CONF['exception_recipients']  
8 - from environment.noreply_email  
9 - reply_to environment.noreply_email  
10 - subject "[#{environment.name}] DelayedJob ##{job.id}: #{error.message}"  
11 - body render(:text => "  
12 -Job:  
13 -#{job.inspect}  
14 -  
15 -Handler:  
16 -#{job.handler}  
17 -  
18 -Backtrace:  
19 -#{error.backtrace.join("\n")}  
20 - ")  
21 - end  
22 - end  
23 -  
24 - def handle_failed_job_with_notification(job, error)  
25 - Delayed::Worker::ExceptionNotification.deliver_mail job, error if NOOSFERO_CONF['exception_recipients'].present?  
26 - handle_failed_job_without_notification job, error  
27 - end  
28 - alias_method_chain :handle_failed_job, :notification  
29 -  
30 - def handle_failed_job_with_rescue(job, error)  
31 - handle_failed_job_without_rescue(job, error)  
32 - rescue => e # don't crash here  
33 - end  
34 - alias_method_chain :handle_failed_job, :rescue  
35 -  
36 - protected  
37 -  
38 - # This code must be replicated because there is no other way to pass the job  
39 - # through and use alias_method_chain as we used on the previous method  
40 - def reserve_and_run_one_job  
41 - # We get up to 5 jobs from the db. In case we cannot get exclusive access to a job we try the next.  
42 - # this leads to a more even distribution of jobs across the worker processes  
43 - job = Delayed::Job.find_available(name, 5, self.class.max_run_time).detect do |job|  
44 - if job.lock_exclusively!(self.class.max_run_time, name)  
45 - say "acquired lock on #{job.name}"  
46 - true  
47 - else  
48 - say "failed to acquire exclusive lock for #{job.name}", Logger::WARN  
49 - false  
50 - end  
51 - end  
52 -  
53 - run(job) if job  
54 - rescue => e  
55 - handle_failed_job(job, e)  
56 - end  
57 -end