summaryrefslogtreecommitdiff
path: root/lib/custodian/alerts/redis-state.rb
blob: cf5262053ea7417088ee02797fab4927efb42139 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
#  The redis-alerter.
#
#  This doesn't raise/clear alerts in the traditional sense, instead
# it just saves the results in a "recent tests" set inside Redis.
#
#
module Custodian

  module Alerter

    class RedisAlert < AlertFactory

      #
      # The test this alerter cares about
      #
      attr_reader :test


      #
      # The redis-object
      #
      attr_reader :redis




      #
      # Constructor - save the test-object away & instantiate
      # the redis connection.
      #
      def initialize( obj )

        begin
          require 'rubygems'
          require 'redis'
          require 'json'

          @redis = Redis.new(:host => @target )

        rescue
          puts "ERROR Loading redis rubygem!"
        end

        @test  = obj
      end



      #
      # Store an alert in redis
      #
      def raise

        return unless( @redis )

        tmp = {}
        tmp["time"] = Time.now.to_i
        tmp["type"] = @test.get_type
        tmp["target"] = @test.target
        tmp["result"] = "RAISE"
        tmp["reason"] =  @test.error()

        @redis.lpush( "recent-tests", tmp.to_json)
        @redis.ltrim( "recent-tests", 0, 100 )

      end



      #
      # Clear an alert in redis
      #
      def clear

        return unless( @redis )


        tmp = {}
        tmp["time"]   = Time.now.to_i
        tmp["type"]   = @test.get_type
        tmp["target"] = @test.target
        tmp["result"] = "OK"
        tmp["reason"] = ""

        @redis.lpush( "recent-tests", tmp.to_json)
        @redis.ltrim( "recent-tests", 0, 100 )
      end



      #
      # Store a test-duration in redis
      #
      def duration( ms )

        return unless( @redis )

        # NOP
      end


      register_alert_type "redis"

    end
  end
end