Class: Highwatermark::HighWaterMark

Inherits:
Object
  • Object
show all
Defined in:
lib/highwatermark.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, state_type) ⇒ HighWaterMark

def initialize(path,state_type,tag)



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
# File 'lib/highwatermark.rb', line 6

def initialize(path,state_type)
# path: is th file path for store state or the redis configure
# state_type: could be <redis/file/memory>
# tag: is the tag that will be used in state file or redis 


require 'yaml'
  require 'pp'
@path = path
@state_type = state_type
			  # @tag = tag

@data = {}
if @state_type =='file'
	if File.exists?(path)
	@data = YAML.load_file(path)
      pp @data
	if @data == false || @data == []
		# this happens if an users created an empty file accidentally
		@data = {}
	elsif !@data.is_a?(Hash)
		raise "state_file on #{@path.inspect} is invalid"
	end
else
	@data = {}
end
  			elsif @state_type =='memory'
@data = {}
  			elsif @state_type =='redis'
require 'redis'
$redis = if File.exists?(path)
	redis_config = YAML.load_file(path)
	# Connect to Redis using the redis_config host and port
	if path
	    begin
            pp "In redis #{path} Host #{redis_config['host']} port #{redis_config['port']}"
  			$redis = Redis.new(host: redis_config['host'], port: redis_config['port'])
				rescue Exception => e
					pp e.message
          pp e.backtrace.inspect
	    end
	end
else
 		Redis.new
end
		@data = {}
	end # end of checking @state_type

  if @data['last_records']==nil
    @data['last_records'] = {}
  end
  
end

Instance Method Details

#last_records(tag) ⇒ Object

end of intitialize



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/highwatermark.rb', line 60

def last_records(tag)
  if @state_type == 'file'
    # return @data[@tag]
    # pp @data['last_records'][tag]
    return @data['last_records'][tag]

  elsif @state_type =='memory'
    return @data['last_records'][tag]
  elsif @state_type =='redis'
    begin
      alertStart=$redis.get(tag)
      return alertStart
    rescue Exception => e
      pp e.message
      pp e.backtrace.inspect
    end
	end
end

#update_records(time, tag) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/highwatermark.rb', line 79

def update_records(time, tag)
    if @state_type == 'file'
      # @data[@tag] = time
			@data['last_records'][tag] = time
			# $log.info  @data
			File.open(@path, 'w') {|f|
			  f.write YAML.dump(@data)
			}
    elsif @state_type =='memory'
      @data['last_records'][tag] = time
      
    elsif @state_type =='redis'
      begin
        alertStart=$redis.set(tag,time)
      rescue Exception => e
        pp e.message
        pp e.backtrace.inspect
      end
    end

end