From 2e601cb575ca97f1a1097f12d0edfae241a70263 Mon Sep 17 00:00:00 2001 From: Jared Pace Date: Tue, 3 Aug 2010 23:05:41 -0500 Subject: [PATCH] Deliver notification emails when notices threshold is met --- app/mailers/mailer.rb | 15 +++++++++++++++ app/models/notice.rb | 12 ++++++++++++ app/views/mailer/error_notification.text.erb | 8 ++++++++ config/config.yml | 5 ++++- config/routes.rb | 3 +++ spec/models/notice_spec.rb | 12 ++++++++++++ 6 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/mailers/mailer.rb create mode 100644 app/views/mailer/error_notification.text.erb diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb new file mode 100644 index 0000000..6f0c691 --- /dev/null +++ b/app/mailers/mailer.rb @@ -0,0 +1,15 @@ +class Mailer < ActionMailer::Base + default :from => App.email_from + default_url_options[:host] = App.host + + def error_notification(notice) + @notice = notice + @project = notice.err.project + + mail({ + :to => @project.watchers.map(&:email), + :subject => "[#{@project.name}] #{@notice.err.message}" + }) + end + +end \ No newline at end of file diff --git a/app/models/notice.rb b/app/models/notice.rb index f75cd56..94b80f8 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -11,6 +11,8 @@ class Notice embedded_in :err, :inverse_of => :notices + after_create :deliver_notification, :if => :should_notify? + validates_presence_of :backtrace, :server_environment, :notifier def self.from_xml(hoptoad_xml) @@ -34,4 +36,14 @@ class Notice }) end + def deliver_notification + Mailer.error_notification(self).deliver + end + + protected + + def should_notify? + App.email_at_notices.include?(err.notices.count) && err.project.watchers.any? + end + end \ No newline at end of file diff --git a/app/views/mailer/error_notification.text.erb b/app/views/mailer/error_notification.text.erb new file mode 100644 index 0000000..4e7f608 --- /dev/null +++ b/app/views/mailer/error_notification.text.erb @@ -0,0 +1,8 @@ +An error has just occurred in <%= @notice.err.environment %>: <%= @notice.err.message %> + +This error has occurred <%= pluralize @notice.err.notices.count, 'time' %>. You should really look into it here: + + <%= error_notice_url(@notice.err, @notice) %> + +Your loyal servant, +Hypnotoad \ No newline at end of file diff --git a/config/config.yml b/config/config.yml index c9d6c04..9a67d1d 100644 --- a/config/config.yml +++ b/config/config.yml @@ -1 +1,4 @@ -email_at_notices: [1, 10, 100] \ No newline at end of file +host: hypnotoad.example.com + +email_at_notices: [1, 10, 100] +email_from: hypnotoad@example.com \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9717614..0b789df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,5 +5,8 @@ Hypnotoad::Application.routes.draw do # match '/deploys.txt' => 'deploys#create' resources :notices + resources :errors do + resources :notices + end end diff --git a/spec/models/notice_spec.rb b/spec/models/notice_spec.rb index f29e5f2..b559f68 100644 --- a/spec/models/notice_spec.rb +++ b/spec/models/notice_spec.rb @@ -79,4 +79,16 @@ describe Notice do end end + describe "email notifications" do + App.email_at_notices.each do |threshold| + it "sends an email notification after #{threshold} notice(s)" do + error = Factory(:err) + error.notices.stub(:count).and_return(threshold) + Mailer.should_receive(:error_notification). + and_return(mock('email', :deliver => true)) + Factory(:notice, :err => error) + end + end + end + end \ No newline at end of file -- libgit2 0.21.2