Commit 2e601cb575ca97f1a1097f12d0edfae241a70263

Authored by Jared Pace
1 parent 4c86d517
Exists in master and in 1 other branch production

Deliver notification emails when notices threshold is met

app/mailers/mailer.rb 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +class Mailer < ActionMailer::Base
  2 + default :from => App.email_from
  3 + default_url_options[:host] = App.host
  4 +
  5 + def error_notification(notice)
  6 + @notice = notice
  7 + @project = notice.err.project
  8 +
  9 + mail({
  10 + :to => @project.watchers.map(&:email),
  11 + :subject => "[#{@project.name}] #{@notice.err.message}"
  12 + })
  13 + end
  14 +
  15 +end
0 \ No newline at end of file 16 \ No newline at end of file
app/models/notice.rb
@@ -11,6 +11,8 @@ class Notice @@ -11,6 +11,8 @@ class Notice
11 11
12 embedded_in :err, :inverse_of => :notices 12 embedded_in :err, :inverse_of => :notices
13 13
  14 + after_create :deliver_notification, :if => :should_notify?
  15 +
14 validates_presence_of :backtrace, :server_environment, :notifier 16 validates_presence_of :backtrace, :server_environment, :notifier
15 17
16 def self.from_xml(hoptoad_xml) 18 def self.from_xml(hoptoad_xml)
@@ -34,4 +36,14 @@ class Notice @@ -34,4 +36,14 @@ class Notice
34 }) 36 })
35 end 37 end
36 38
  39 + def deliver_notification
  40 + Mailer.error_notification(self).deliver
  41 + end
  42 +
  43 + protected
  44 +
  45 + def should_notify?
  46 + App.email_at_notices.include?(err.notices.count) && err.project.watchers.any?
  47 + end
  48 +
37 end 49 end
38 \ No newline at end of file 50 \ No newline at end of file
app/views/mailer/error_notification.text.erb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +An error has just occurred in <%= @notice.err.environment %>: <%= @notice.err.message %>
  2 +
  3 +This error has occurred <%= pluralize @notice.err.notices.count, 'time' %>. You should really look into it here:
  4 +
  5 + <%= error_notice_url(@notice.err, @notice) %>
  6 +
  7 +Your loyal servant,
  8 +Hypnotoad
0 \ No newline at end of file 9 \ No newline at end of file
config/config.yml
1 -email_at_notices: [1, 10, 100]  
2 \ No newline at end of file 1 \ No newline at end of file
  2 +host: hypnotoad.example.com
  3 +
  4 +email_at_notices: [1, 10, 100]
  5 +email_from: hypnotoad@example.com
3 \ No newline at end of file 6 \ No newline at end of file
config/routes.rb
@@ -5,5 +5,8 @@ Hypnotoad::Application.routes.draw do @@ -5,5 +5,8 @@ Hypnotoad::Application.routes.draw do
5 # match '/deploys.txt' => 'deploys#create' 5 # match '/deploys.txt' => 'deploys#create'
6 6
7 resources :notices 7 resources :notices
  8 + resources :errors do
  9 + resources :notices
  10 + end
8 11
9 end 12 end
spec/models/notice_spec.rb
@@ -79,4 +79,16 @@ describe Notice do @@ -79,4 +79,16 @@ describe Notice do
79 end 79 end
80 end 80 end
81 81
  82 + describe "email notifications" do
  83 + App.email_at_notices.each do |threshold|
  84 + it "sends an email notification after #{threshold} notice(s)" do
  85 + error = Factory(:err)
  86 + error.notices.stub(:count).and_return(threshold)
  87 + Mailer.should_receive(:error_notification).
  88 + and_return(mock('email', :deliver => true))
  89 + Factory(:notice, :err => error)
  90 + end
  91 + end
  92 + end
  93 +
82 end 94 end
83 \ No newline at end of file 95 \ No newline at end of file