aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/processor.rb
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2012-01-30 12:28:14 +0000
committerPatrick J Cherry <patrick@bytemark.co.uk>2012-01-30 12:28:14 +0000
commit8ed82944ebd764f1986b3155990b895045b584bc (patch)
tree6524da2b6bb3895a09625d6d9ec65963a4d5d2ec /lib/mauve/processor.rb
parent3367b57cd9e9a4656ba926d9fca1c21f8f7a4dd0 (diff)
Bundled timer in with processor. What could go wrong..?
Diffstat (limited to 'lib/mauve/processor.rb')
-rw-r--r--lib/mauve/processor.rb89
1 files changed, 74 insertions, 15 deletions
diff --git a/lib/mauve/processor.rb b/lib/mauve/processor.rb
index c8a1dfb..96c44aa 100644
--- a/lib/mauve/processor.rb
+++ b/lib/mauve/processor.rb
@@ -76,25 +76,87 @@ module Mauve
#
# flush the queue
#
- main_loop
+ do_processor
end
private
- # This is the main loop that does the processing.
- #
def main_loop
-
- sz = Server.packet_buffer_size
+ do_processor
+ do_timer unless timer_should_stop?
+ end
- sz.times do
- Timer.instance.freeze if Timer.instance.alive? and !Timer.instance.frozen?
+ def do_timer
+ #
+ # Get the next alert.
+ #
+ next_alert = Alert.find_next_with_event
+
+ #
+ # If we didn't find an alert, or the alert we found is due in the future,
+ # look for the next alert_changed object.
+ #
+ if next_alert.nil? or next_alert.due_at > Time.now
+ next_alert_changed = AlertChanged.find_next_with_event
+ end
+ if next_alert_changed.nil? and next_alert.nil?
+ next_to_notify = nil
+
+ elsif next_alert.nil? or next_alert_changed.nil?
+ next_to_notify = (next_alert || next_alert_changed)
+
+ else
+ next_to_notify = ( next_alert.due_at < next_alert_changed.due_at ? next_alert : next_alert_changed )
+
+ end
+
+ #
+ # Nothing to notify?
+ #
+ if next_to_notify.nil?
+ #
+ # Sleep indefinitely
+ #
+ logger.info("Nothing to notify about -- snoozing for a while.")
+ sleep_loops = 600
+ else
#
- # Hmm.. timer not frozen.
+ # La la la nothing to do.
#
- break unless Timer.instance.frozen?
+ logger.info("Next to notify: #{next_to_notify} #{next_to_notify.is_a?(AlertChanged) ? "(reminder)" : "(heartbeat)"} -- snoozing until #{next_to_notify.due_at.iso8601}")
+ sleep_loops = ((next_to_notify.due_at - Time.now).to_f / 0.1).round.to_i
+ end
+
+ sleep_loops = 0 if sleep_loops.nil? or sleep_loops < 1
+ #
+ # Ah-ha! Sleep with a break clause.
+ #
+ sleep_loops.times do
+ #
+ # Start again if the situation has changed.
+ #
+ break if timer_should_stop?
+
+ #
+ # This is a rate-limiting step for alerts.
+ #
+ Kernel.sleep 0.1
+ end
+
+ return if timer_should_stop? or next_to_notify.nil?
+
+ next_to_notify.poll
+ end
+
+ # This is processor loop
+ #
+ def do_processor
+
+ sz = Server.packet_buffer_size
+
+ sz.times do
data, client, received_at = Server.packet_pop
#
@@ -136,14 +198,11 @@ module Mauve
ensure
@transmission_id_cache[update.transmission_id.to_s] = Time.now
end
-
end
+ end
- ensure
- #
- # Thaw the timer
- #
- Timer.instance.thaw if Timer.instance.frozen?
+ def timer_should_stop?
+ (Server.packet_buffer_size > 0)
end
end