diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-04-17 21:26:49 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-04-17 21:26:49 +0100 |
commit | 4c99233f3b3112cd2be5ab6fd5d6e7c1c344406b (patch) | |
tree | ca5a7c3659b5aa3be4ecf40af95ef24870c09e7c | |
parent | 3a579efcd3ea7e4362f7abeb4138c4087e6f13c1 (diff) |
* The notification buffer is now used to store Alerts/AlertChangeds and times
to notify about, rather than notifications to be sent.
* Fixed up the debug notifier a bit more.
* Using the debug notifier extras more now.
-rw-r--r-- | lib/mauve/alert.rb | 7 | ||||
-rw-r--r-- | lib/mauve/alert_changed.rb | 6 | ||||
-rw-r--r-- | lib/mauve/notification.rb | 2 | ||||
-rw-r--r-- | lib/mauve/notifier.rb | 14 | ||||
-rw-r--r-- | lib/mauve/notifiers/debug.rb | 12 | ||||
-rw-r--r-- | test/tc_mauve_alert_changed.rb | 83 | ||||
-rw-r--r-- | test/tc_mauve_notification.rb | 49 |
7 files changed, 117 insertions, 56 deletions
diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index 706b0e3..28079a2 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -401,12 +401,7 @@ module Mauve # # @return [Boolean] Showing if an alert has been sent. def notify(at = Time.now) - if self.alert_group.nil? - logger.warn "Could not notify for #{self} since there are no matching alert groups" - false - else - self.alert_group.notify(self, at) - end + Server.notification_push([self, at]) end # Acknowledge an alert diff --git a/lib/mauve/alert_changed.rb b/lib/mauve/alert_changed.rb index 7cfdb76..0e0d257 100644 --- a/lib/mauve/alert_changed.rb +++ b/lib/mauve/alert_changed.rb @@ -98,7 +98,11 @@ module Mauve return save end - alert_group.notify(alert, Time.now) + # + # Push this notifitcation onto the queue. + # + Server.notification_push([alert, Time.now]) + # # Need to make sure this reminder is cleared. # diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb index 522f9fa..3c902a2 100644 --- a/lib/mauve/notification.rb +++ b/lib/mauve/notification.rb @@ -239,7 +239,7 @@ module Mauve if already_sent_to.include?(person.username) logger.info("Already sent notification of #{alert} to #{person.username}") else - Server.notification_push([person, level, alert]) + person.send_alert(level, alert) already_sent_to << person.username end end diff --git a/lib/mauve/notifier.rb b/lib/mauve/notifier.rb index bf36162..8a26b2c 100644 --- a/lib/mauve/notifier.rb +++ b/lib/mauve/notifier.rb @@ -86,14 +86,12 @@ module Mauve # Empty the buffer, one notification at a time. # sz.times do - person, *args = Server.notification_pop - - # - # Nil person.. that's craaazy too! - # - next if person.nil? - - person.send_alert(*args) + alert, at = Server.notification_pop + if alert.alert_group.nil? + logger.warn "Could not notify for #{alert} since there are no matching alert groups" + else + alert.alert_group.notify(alert, at) + end end end diff --git a/lib/mauve/notifiers/debug.rb b/lib/mauve/notifiers/debug.rb index 417bc94..a9afc52 100644 --- a/lib/mauve/notifiers/debug.rb +++ b/lib/mauve/notifiers/debug.rb @@ -53,27 +53,21 @@ module Mauve end def send_alert_to_debug_channels(destination, alert, all_alerts, conditions = nil) - message = if respond_to?(:prepare_message) + message = if self.respond_to?(:prepare_message) prepare_message(destination, alert, all_alerts, conditions) else [destination, alert, all_alerts].inspect end if deliver_to_file - #lock_file = "#{deliver_to_file}.lock" - #while File.exists?(lock_file) - # sleep 0.1 - #end - #FileUtils.touch(lock_file) File.open("#{deliver_to_file}", "a+") do |fh| fh.flock(File::LOCK_EX) - fh.print("#{Time.now} from #{self.class}: " + message + "\n") + fh.print YAML.dump([Time.now, self.class, destination, message]) fh.flush() end - #FileUtils.rm(lock_file) end - deliver_to_queue << [destination, alert, all_alerts, conditions] if deliver_to_queue + deliver_to_queue << [Time.now, self.class, destination, message] if deliver_to_queue if @disable_normal_delivery true # pretend it happened OK if we're just testing diff --git a/test/tc_mauve_alert_changed.rb b/test/tc_mauve_alert_changed.rb index 86f31d7..167ea75 100644 --- a/test/tc_mauve_alert_changed.rb +++ b/test/tc_mauve_alert_changed.rb @@ -6,6 +6,7 @@ require 'mauve/alert_changed' require 'mauve/configuration' require 'mauve/configuration_builder' require 'mauve/configuration_builders' +require 'mauve/notifiers' class TcMauveAlertChanged < Mauve::UnitTest include Mauve @@ -23,8 +24,15 @@ class TcMauveAlertChanged < Mauve::UnitTest def test_reminder config=<<EOF +notification_method("email") { + debug! + deliver_to_queue [] + disable_normal_delivery! +} + person("test_person") { - all { true } + email "test_person@example.com" + all { email } } alert_group("test_group") { @@ -37,20 +45,27 @@ alert_group("test_group") { EOF Configuration.current = ConfigurationBuilder.parse(config) + notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue Server.instance.setup alert = Alert.new(:source => "test", :alert_id => "test_alert", :summary => "test alert") alert.raise! - reminders = 1 + reminders = 1 notifications = 1 mins = 0 11.times do mins += 1 - assert_equal(notifications, Server.instance.notification_buffer.length) + # + # In order to send the notification and stick in the reminder, we need to + # process the buffer. + # + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + + assert_equal(notifications, notification_buffer.length) assert_equal(reminders, AlertChanged.count) Timecop.freeze(Time.now+1.minute) @@ -67,7 +82,12 @@ EOF alert.clear! notifications += 1 - assert_equal(notifications, Server.instance.notification_buffer.length) + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(notifications, notification_buffer.length) + # + # Process the buffer again + # + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } assert_equal(reminders, AlertChanged.count) Timecop.freeze(Time.now + 10.minutes) @@ -75,15 +95,23 @@ EOF # # Send NO MORE notifications. # - assert_equal(notifications, Server.instance.notification_buffer.length) + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(notifications, notification_buffer.length) assert_equal(reminders, AlertChanged.count) end def test_only_send_one_alert_on_unacknowledge config=<<EOF +notification_method("email") { + debug! + deliver_to_queue [] + disable_normal_delivery! +} + person("test_person") { - all { true } + email "test@example.com" + all { email } } alert_group("test_group") { @@ -96,21 +124,29 @@ alert_group("test_group") { EOF Configuration.current = ConfigurationBuilder.parse(config) + notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue Server.instance.setup alert = Alert.new(:source => "test", :alert_id => "test_alert", :summary => "test alert") alert.raise! - assert_equal(1,Server.instance.notification_buffer.length, "Wrong no of notifications sent after raise.") - assert_equal(1,AlertChanged.count, "Wrong no of AlertChangeds created after raise.") + + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(1, notification_buffer.length, "Wrong no of notifications sent after raise.") + assert_equal(1, AlertChanged.count, "Wrong no of AlertChangeds created after raise.") alert.acknowledge!(Configuration.current.people["test_person"], Time.now + 10.minutes) - assert_equal(2,Server.instance.notification_buffer.length, "Wrong no of notifications sent after acknowledge.") - assert_equal(2,AlertChanged.count, "Wrong no of AlertChangeds created after acknowledge.") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(2, notification_buffer.length, "Wrong no of notifications sent after raise.") + assert_equal(2, AlertChanged.count, "Wrong no of AlertChangeds created after acknowledge.") + # + # The alert has been acknowledged so send no more reminders. + # Timecop.freeze(Time.now + 10.minutes) AlertChanged.all.each{|ac| ac.poll} - assert_equal(2,Server.instance.notification_buffer.length, "Extra notifications sent when alertchangeds are polled.") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(2, notification_buffer.length, "Extra notifications sent when alertchangeds are polled.") # # OK if we poll the alert now it should be re-raised. @@ -118,19 +154,28 @@ EOF alert.poll assert(!alert.acknowledged?,"Alert not unacknowledged") assert(alert.raised?,"Alert not raised following unacknowledgment") - assert_equal(3,Server.instance.notification_buffer.length, "No re-raise notification sent.") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(3, notification_buffer.length, "No re-raise notification sent.") + # # If we poll the AlertChangeds again, no further notification should be sent. # AlertChanged.all.each{|ac| ac.poll} - assert_equal(3,Server.instance.notification_buffer.length, "Extra notifications sent when alertchangeds are polled.") - + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(3, notification_buffer.length, "Extra notifications sent when alertchangeds are polled.") end def test_only_set_one_alert_changed_on_a_reminder_after_multiple_raises_and_clears config=<<EOF +notification_method("email") { + debug! + deliver_to_queue [] + disable_normal_delivery! +} + person("office_chat") { - all { true } + email "test@example.com" + all { email } } alert_group("test_group") { @@ -147,6 +192,7 @@ EOF Configuration.current = ConfigurationBuilder.parse(config) + notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue Server.instance.setup @@ -165,7 +211,8 @@ EOF # # No notification should have been sent, since it is the middle of the night # - assert_equal(0,Server.instance.notification_buffer.length, "No notifications should have been sent.") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(0, notification_buffer.length, "No notifications should have been sent.") assert(alert.cleared?) # @@ -175,14 +222,14 @@ EOF # # Still no alerts should be sent. # - assert_equal(0,Server.instance.notification_buffer.length, "No notifications should have been sent.") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(0, notification_buffer.length, "No notifications should have been sent.") assert(alert.raised?) # # Only one AlertChanged should be set now, with a reminder time of 8.30. # assert_equal(1, AlertChanged.all(:remind_at.not => nil).length, "Too many reminders are due to be sent.") - end end diff --git a/test/tc_mauve_notification.rb b/test/tc_mauve_notification.rb index ec5eaf6..bac389f 100644 --- a/test/tc_mauve_notification.rb +++ b/test/tc_mauve_notification.rb @@ -185,16 +185,25 @@ class TcMauveNotification < Mauve::UnitTest t = Time.now config=<<EOF +notification_method("email") { + debug! + deliver_to_queue [] + disable_normal_delivery! +} + person ("test1") { - all { true } + email "test1@example.com" + all { email } } person ("test2") { - all { true } + email "test2@example.com" + all { email } } person ("test3") { - all { true } + email "test3@example.com" + all { email } } people_list "testers", %w( @@ -227,6 +236,8 @@ alert_group("default") { EOF Configuration.current = ConfigurationBuilder.parse(config) + notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue + Server.instance.setup alert = Alert.new( :alert_id => "test", @@ -240,7 +251,8 @@ EOF # # Also make sure that only 2 notifications has been sent.. # - assert_equal(2, Server.instance.notification_buffer.size, "Wrong number of notifications sent") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(2, notification_buffer.size, "Wrong number of notifications sent") # # Although there are four clauses above for notifications, test1 should be @@ -303,8 +315,9 @@ EOF ) alert.raise! - assert_equal(1, Alert.count, "Wrong number of alerts saved") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } + assert_equal(1, Alert.count, "Wrong number of alerts saved") assert_equal(1, AlertChanged.count, "Wrong number of reminders inserted") a = AlertChanged.first @@ -322,19 +335,27 @@ EOF def test_no_race_conditions_in_during config=<<EOF +notification_method("email") { + debug! + deliver_to_queue [] + disable_normal_delivery! +} + person ("test1") { - all { true } + email "test1@example.com" + all { email } } person ("test2") { - all { true } + email "test1@example.com" + all { email } } alert_group("default") { level URGENT notify("test1") { every 0 - during { sleep 1 ; hours_in_day 1..7 } + during { sleep 2 ; hours_in_day 1..7 } } notify("test2") { @@ -348,21 +369,23 @@ EOF # # Wind forward until 7:59:59am # - Timecop.travel(Time.now + 7.hours + 59.minutes + 59.seconds) Configuration.current = ConfigurationBuilder.parse(config) + notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue + Server.instance.setup + alert = Alert.new( :alert_id => "test", :source => "test", :subject => "test" ) + + Timecop.travel(Time.now + 7.hours + 59.minutes + 59.seconds) alert.raise! - a = AlertChanged.first - assert_equal("urgent", a.level, "Level is wrong for #{a.person}") - assert_equal("raised", a.update_type, "Update type is wrong for #{a.person}") + assert_nothing_raised{ Notifier.instance.__send__(:main_loop) } - assert_equal(1, Server.instance.notification_buffer.size, "Wrong number of notifications sent") + assert_equal(1, notification_buffer.size, "Wrong number of notifications sent") end |