aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2011-08-18 18:11:10 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2011-08-18 18:11:10 +0100
commit6aba0c3d8e2d30797755dc8b968fe338e13b8df4 (patch)
treea9c3a469146a47fe88e3a56c8bf40c0ed9b66288 /lib/mauve
parentc30daf56d4aab9e41a993d82406cba947c8efe32 (diff)
* Fixed up Alert to postpone raises/clears within initial sleep period for old alerts
* Fixed up AlertChanged to do the same * Added a migration method for the new AlertHistories table * added singleton cleanup for tests * moar tests * cleaned up Alert a bit
Diffstat (limited to 'lib/mauve')
-rw-r--r--lib/mauve/alert.rb87
-rw-r--r--lib/mauve/alert_changed.rb13
-rw-r--r--lib/mauve/person.rb6
-rw-r--r--lib/mauve/server.rb7
4 files changed, 72 insertions, 41 deletions
diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb
index 28dfcf1..22f6784 100644
--- a/lib/mauve/alert.rb
+++ b/lib/mauve/alert.rb
@@ -87,12 +87,10 @@ module Mauve
validates_with_method :check_dates
- def inspect
+ def to_s
"#<Alert #{id}, alert_id #{alert_id}, source #{source}>"
end
- alias to_s inspect
-
#
# This is to stop datamapper inserting duff dates into the database.
#
@@ -163,20 +161,8 @@ module Mauve
def subject; attribute_get(:subject) || attribute_get(:source) || "not set" ; end
def detail; attribute_get(:detail) || "_No detail set._" ; end
- # def subject=(subject); set_changed_if_different( :subject, subject ); end
- # def summary=(summary); set_changed_if_different( :summary, summary ); end
-
- # def source=(source); attribute_set( :source, source ); end
- # def detail=(detail); attribute_set( :detail, detail ); end
-
protected
- #def set_changed_if_different(attribute, value)
- # return if self.__send__(attribute) == value
- # self.update_type ||= "changed"
- # attribute_set(attribute.to_sym, value)
- #end
-
#
# This allows us to take a copy of the changes before we save.
#
@@ -208,6 +194,7 @@ module Mauve
#
if (@attributes_before_save.has_key?(:update_type) and !is_a_new_alert) or
(self.update_type == "raised" and (is_a_new_alert or is_a_change))
+
self.notify
h = History.new(:alerts => [self], :type => "update")
@@ -265,27 +252,61 @@ module Mauve
end
def raise!(at = Time.now)
- self.acknowledged_by = nil
- self.acknowledged_at = nil
- self.will_unacknowledge_at = nil
- self.raised_at = at if self.raised_at.nil?
- self.will_raise_at = nil
- self.cleared_at = nil
- # Don't clear will_clear_at
- self.update_type = "raised" if self.update_type.nil? or self.update_type != "changed" or self.original_attributes[Alert.properties[:update_type]] == "cleared"
+ #
+ # OK if this is an alert updated in the last run, do not raise, just postpone.
+ #
+ if (self.will_raise_at or self.will_unacknowledge_at) and
+ Server.instance.in_initial_sleep? and
+ self.updated_at and
+ self.updated_at < Server.instance.started_at
+
+ postpone_until = Server.instance.started_at + Server.instance.initial_sleep
+
+ if self.will_raise_at and self.will_raise_at <= Time.now
+ self.will_raise_at = postpone_until
+ end
+
+ if self.will_unacknowledge_at and self.will_unacknowledge_at <= Time.now
+ self.will_unacknowledge_at = postpone_until
+ end
+
+ logger.info("Postponing raise of #{self} until #{postpone_until} as it was last updated in a prior run of Mauve.")
+ else
+ self.acknowledged_by = nil
+ self.acknowledged_at = nil
+ self.will_unacknowledge_at = nil
+ self.raised_at = at if self.raised_at.nil?
+ self.will_raise_at = nil
+ self.cleared_at = nil
+ # Don't clear will_clear_at
+ self.update_type = "raised" if self.update_type.nil? or self.update_type != "changed" or self.original_attributes[Alert.properties[:update_type]] == "cleared"
+ end
logger.error("Couldn't save #{self}") unless save
end
def clear!(at = Time.now)
- self.acknowledged_by = nil
- self.acknowledged_at = nil
- self.will_unacknowledge_at = nil
- self.raised_at = nil
- # Don't clear will_raise_at
- self.cleared_at = at if self.cleared_at.nil?
- self.will_clear_at = nil
- self.update_type = "cleared"
+ #
+ # Postpone clearance if we're in the sleep period.
+ #
+ if self.will_clear_at and
+ Server.instance.in_initial_sleep? and
+ self.updated_at and
+ self.updated_at < Server.instance.started_at
+
+ self.will_clear_at = Server.instance.started_at + Server.instance.initial_sleep
+
+ logger.info("Postponing clear of #{self} until #{self.will_clear_at} as it was last updated in a prior run of Mauve.")
+ else
+ self.acknowledged_by = nil
+ self.acknowledged_at = nil
+ self.will_unacknowledge_at = nil
+ self.raised_at = nil
+ # Don't clear will_raise_at
+ self.cleared_at = at if self.cleared_at.nil?
+ self.will_clear_at = nil
+ self.update_type = "cleared"
+ end
logger.error("Couldn't save #{self}") unless save
end
@@ -301,7 +322,7 @@ module Mauve
raise! if (will_unacknowledge_at and will_unacknowledge_at <= Time.now) or
(will_raise_at and will_raise_at <= Time.now)
clear! if will_clear_at && will_clear_at <= Time.now
- logger.info("Polled #{self.inspect}")
+ logger.info("Polled #{self.to_s}")
end
@@ -518,7 +539,7 @@ module Mauve
#
# Record the fact we received an update.
#
- logger.info("Received update from #{ip_source} for #{alert_db.inspect}")
+ logger.info("Received update from #{ip_source} for #{alert_db}")
end
diff --git a/lib/mauve/alert_changed.rb b/lib/mauve/alert_changed.rb
index 9b74396..2871aee 100644
--- a/lib/mauve/alert_changed.rb
+++ b/lib/mauve/alert_changed.rb
@@ -61,11 +61,22 @@ module Mauve
if alert.acknowledged?
- logger.info("Alert already acknowledged. Clearing reminder due for #{self.alert}.")
+ logger.info("Alert already acknowledged. Clearing reminder due for #{self.alert}.")
self.remind_at = nil
return save
end
+ #
+ # Postpone reminders from previous runs, if needed.
+ #
+ if Server.instance.in_initial_sleep? and
+ self.at < Server.instance.started_at
+
+ self.remind_at = Server.instance.started_at + Server.instance.initial_sleep
+ logger.info("Postponing reminder for #{self.alert} until #{self.remind_at} since this reminder was updated in a prior run of Mauve.")
+ return save
+ end
+
alert_group.notify(alert)
#
# Need to make sure this reminder is cleared.
diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb
index 3831529..64ed07e 100644
--- a/lib/mauve/person.rb
+++ b/lib/mauve/person.rb
@@ -163,12 +163,6 @@ module Mauve
else
logger.info "Starting to send notifications again for #{username}." if was_suppressed
-
- end
-
- if Server.instance.started_at > alert.updated_at and (Server.instance.started_at + Server.instance.initial_sleep) > Time.now
- logger.info("Alert last updated in prior run of mauve -- ignoring for initial sleep period.")
- return true
end
#
diff --git a/lib/mauve/server.rb b/lib/mauve/server.rb
index 138636e..eb5872b 100644
--- a/lib/mauve/server.rb
+++ b/lib/mauve/server.rb
@@ -65,6 +65,10 @@ module Mauve
@initial_sleep = s
end
+ def in_initial_sleep?
+ Time.now < self.started_at + self.initial_sleep
+ end
+
def logger
@logger ||= Log4r::Logger.new(self.class.to_s)
end
@@ -92,7 +96,8 @@ module Mauve
# m.auto_migrate! if m.respond_to?("auto_migrate!")
end
- Mauve::AlertEarliestDate.create_view!
+ AlertHistory.migrate!
+ AlertEarliestDate.create_view!
return nil
end