From 85d12bd6928f7859742439c4e034cf12803efee2 Mon Sep 17 00:00:00 2001 From: Marcin Ciunelis Date: Sun, 21 Oct 2012 22:11:04 +0200 Subject: [PATCH] throttle email notificatiions --- app/models/app.rb | 8 ++++++++ app/models/notice.rb | 12 ++++++++++++ app/models/notice_observer.rb | 11 +---------- spec/controllers/apps_controller_spec.rb | 4 ++++ 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/models/app.rb b/app/models/app.rb index 8447ffe..639f390 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -103,6 +103,10 @@ class App end alias :notify_on_errs? :notify_on_errs + def notifiable? + notify_on_errs? && notification_recipients.any? + end + def notify_on_deploys !(super == false) end @@ -185,6 +189,10 @@ class App name <=> other.name end + def email_at_notices + Errbit::Config.per_app_email_at_notices ? read_attribute(:email_at_notices) : Errbit::Config.email_at_notices + end + protected def store_cached_attributes_on_problems diff --git a/app/models/notice.rb b/app/models/notice.rb index 604f21b..2db7167 100644 --- a/app/models/notice.rb +++ b/app/models/notice.rb @@ -94,6 +94,18 @@ class Notice backtrace_lines.in_app end + def similar_count + problem.notices_count + end + + def notifiable? + app.email_at_notices.include?(similar_count) + end + + def should_notify? + app.notifiable? && notifiable? + end + protected def increase_counter_cache diff --git a/app/models/notice_observer.rb b/app/models/notice_observer.rb index 8f02028..be29e91 100644 --- a/app/models/notice_observer.rb +++ b/app/models/notice_observer.rb @@ -7,16 +7,7 @@ class NoticeObserver < Mongoid::Observer notice.app.notification_service.create_notification(notice.problem) end - if notice.app.notification_recipients.any? - Mailer.err_notification(notice).deliver - end + Mailer.err_notification(notice).deliver if notice.should_notify? end - private - - def should_notify? notice - app = notice.app - app.notify_on_errs? and (app.notification_recipients.any? or !app.notification_service.nil?) and - (app.email_at_notices or Errbit::Config.email_at_notices).include?(notice.problem.notices_count) - end end diff --git a/spec/controllers/apps_controller_spec.rb b/spec/controllers/apps_controller_spec.rb index f242885..27308a8 100644 --- a/spec/controllers/apps_controller_spec.rb +++ b/spec/controllers/apps_controller_spec.rb @@ -250,6 +250,10 @@ describe AppsController do end context "changing email_at_notices" do + before do + Errbit::Config.per_app_email_at_notices = true + end + it "should parse legal csv values" do put :update, :id => @app.id, :app => { :email_at_notices => '1, 4, 7,8, 10' } @app.reload -- libgit2 0.21.2