From 0d06542c123b331dae465e4d226579c5b7a2faa3 Mon Sep 17 00:00:00 2001 From: sunkencity Date: Sat, 4 Dec 2010 17:06:14 +0100 Subject: [PATCH 1/2] Added possibility to send notifications from background processes --- README | 11 ++++++++--- lib/exception_notifier/notifier.rb | 17 +++++++++++++++++ .../background_exception_notification.text.erb | 13 +++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 lib/exception_notifier/views/exception_notifier/background_exception_notification.text.erb diff --git a/README b/README index c5f355c1..3b347533 100644 --- a/README +++ b/README @@ -20,6 +20,11 @@ As of Rails 3 ExceptionNotifier is used as a rack middleware :sender_address => %{"notifier" }, :exception_recipients => %w{exceptions@example.com} +If you need to send exception notifications from a background process like +delayed job you must manually send the exeption with: + + ExceptionNotifier::Notifier.background_exception_notification(e) + == Customization By default, the notification email includes four parts: request, session, @@ -66,11 +71,11 @@ After an exception notification has been delivered the rack environment variable == Rails 2.3 stable and earlier -If you are running Rails 2.3 then see the branch for that: - +If you are running Rails 2.3 then see the branch for that: + http://github.com/rails/exception_notification/tree/2-3-stable -If you are running pre-rack Rails then see this tag: +If you are running pre-rack Rails then see this tag: http://github.com/rails/exception_notification/tree/pre-2-3 diff --git a/lib/exception_notifier/notifier.rb b/lib/exception_notifier/notifier.rb index 01234f43..e8f67386 100644 --- a/lib/exception_notifier/notifier.rb +++ b/lib/exception_notifier/notifier.rb @@ -58,6 +58,23 @@ def exception_notification(env, exception) end end + def background_exception_notification(exception) + + if @notifier = Rails.application.config.middleware.detect { |x| x.klass == ExceptionNotifier } + @options = (@notifier.args.first || {}).reverse_merge(self.class.default_options) + subject = "#{@options[:email_prefix]} (#{exception.class}) #{exception.message.inspect}" + + @exception = exception + @backtrace = exception.backtrace || [] + @sections = %w{backtrace} + + mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format| + format.text { render "#{mailer_name}/background_exception_notification" } + end.deliver + end + end + + private def clean_backtrace(exception) diff --git a/lib/exception_notifier/views/exception_notifier/background_exception_notification.text.erb b/lib/exception_notifier/views/exception_notifier/background_exception_notification.text.erb new file mode 100644 index 00000000..c27dc21f --- /dev/null +++ b/lib/exception_notifier/views/exception_notifier/background_exception_notification.text.erb @@ -0,0 +1,13 @@ +A <%= @exception.class %> occurred in background: + + <%= @exception.message %> + <%= @backtrace.first %> + + <% sections = @sections.map do |section| + summary = render(section).strip + unless summary.blank? + title = render("title", :title => section).strip + "#{title}\n\n#{summary.gsub(/^/, " ")}\n\n" + end + end %> + <%= raw sections.join %> From b54ba10cea78f62758d3d71aa5f114cea658db3a Mon Sep 17 00:00:00 2001 From: sunkencity Date: Sat, 4 Dec 2010 17:09:38 +0100 Subject: [PATCH 2/2] typo --- README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README b/README index 3b347533..f75377c4 100644 --- a/README +++ b/README @@ -21,7 +21,7 @@ As of Rails 3 ExceptionNotifier is used as a rack middleware :exception_recipients => %w{exceptions@example.com} If you need to send exception notifications from a background process like -delayed job you must manually send the exeption with: +delayed job you must manually send the exception with: ExceptionNotifier::Notifier.background_exception_notification(e)