Class: RProxy::CheckSnapshotService

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

Instance Method Summary collapse

Constructor Details

#initialize(redis, config) ⇒ CheckSnapshotService

Returns a new instance of CheckSnapshotService.



4
5
6
7
8
9
10
# File 'lib/r_proxy/check_snapshot_service.rb', line 4

def initialize(redis, config)
  @redis = redis
  @snapshot_expire_in = 15 * 60
  @usage_threshold = config.usage_threshold
  @cb_url = config.callback_url
  @logger = config.logger
end

Instance Method Details

#call(user, pass, result) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/r_proxy/check_snapshot_service.rb', line 12

def call(user, pass, result)

  return if user.nil? ||  pass.nil? || result.nil?

  s_key = "proxy:#{user}-#{pass}:snapshot"
  snapshot_value = @redis.get(s_key)

  if snapshot_value.nil? || snapshot_value.empty?
    @redis.setex(s_key, @snapshot_expire_in, result)
  else
    tmp = snapshot_value.to_i - result.to_i

    if tmp >= @usage_threshold
      begin
        connection = RProxy::CallbackService.call(@cb_url, user, pass, tmp)
        connection.assign_logger(@logger)
        @redis.setex(s_key, @snapshot_expire_in, result)
      rescue => e
        @logger.error("callback service: @id:#{s_key}, #{e.message}") if @logger
      end
    end
  end
end