aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/notifier.rb
blob: e0692f623b8ae9d639d473fa4539c6cb5fbf4f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'mauve/mauve_thread'
require 'mauve/notifiers'
require 'mauve/notifiers/xmpp'

module Mauve

  class Notifier < MauveThread

    DEFAULT_XMPP_MESSAGE = "Mauve server started."
    
    include Singleton

    attr_accessor :buffer, :sleep_interval

    def initialize
      @buffer = Queue.new
    end

    def main_loop

      # 
      # Cycle through the buffer.
      #
      sz = @buffer.size
  
      logger.debug("Notifier buffer is #{sz} in length") if sz > 1 

      (sz > 10 ? 10 : sz).times do
        person, level, alert = @buffer.pop
        begin
          person.do_send_alert(level, alert) 
        rescue StandardError => ex
          logger.debug ex.to_s
          logger.debug ex.backtrace.join("\n")
        end
      end

    end

    def start
      super

      Configuration.current.notification_methods['xmpp'].connect if Configuration.current.notification_methods['xmpp']
    end

    def stop
      Configuration.current.notification_methods['xmpp'].close

      super
    end

    class << self

      def enq(a)
        instance.buffer.enq(a)
      end

      alias push enq

    end

  end

end