Module: ApiCache

Included in:
AplApiConfig
Defined in:
lib/apl-library/api_cache.rb

Constant Summary collapse

@@sleep_time =
30
@@apl_config_service =
'apl_silencer'
@@api_config =
[]
@@api_sync_thread =
nil
@@error_count =
0

Instance Method Summary collapse

Instance Method Details

#api_configObject



84
85
86
# File 'lib/apl-library/api_cache.rb', line 84

def api_config
  @@api_config
end

#api_config=(api_config) ⇒ Object



88
89
90
# File 'lib/apl-library/api_cache.rb', line 88

def api_config=(api_config)
  @@api_config = api_config
end

#check_api_disabled(bu, method, uri) ⇒ Object



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
# File 'lib/apl-library/api_cache.rb', line 57

def check_api_disabled(bu, method, uri)
  bu = bu.downcase if bu
  method = method.downcase if method
  uri = uri.downcase if uri

  # Specific URI is blocked
  return true if (@@api_config.select {|c| c['bu'] == bu and c['method'] == method and c['uri'] == uri}).size > 0

  # Specific URI across BU is blocked
  return true if (@@api_config.select {|c| c['bu'] == nil and c['method'] == method and c['uri'] == uri}).size > 0

  # One entire type of HTTP method is blocked
  return true if (@@api_config.select {|c| c['bu'] == bu and c['method'] == method and c['uri'] == nil}).size > 0

  # One entire type of HTTP method is blocked across BU
  return true if (@@api_config.select {|c| c['bu'] == nil and c['method'] == method and c['uri'] == nil}).size > 0

  # Entire BU is blocked
  return true if (@@api_config.select {|c| c['bu'] == bu and c['method'] == nil and c['uri'] == nil}).size > 0

  # Entire service is blocked
  return true if (@@api_config.select {|c| c['bu'] == nil and c['method'] == nil and c['uri'] == nil}).size > 0

  return false
end

#get_api_config_cacheObject



53
54
55
# File 'lib/apl-library/api_cache.rb', line 53

def get_api_config_cache
  @@api_config
end

#get_api_configurationsObject



43
44
45
46
# File 'lib/apl-library/api_cache.rb', line 43

def get_api_configurations
  url = "/api_configurator/configurations/#{SERVICE_NAME}"
  @connector.get(url)
end

#setup_api_config_cache(sleep_time = 30) ⇒ Object



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
# File 'lib/apl-library/api_cache.rb', line 8

def setup_api_config_cache(sleep_time = 30)
  @@sleep_time = sleep_time
  @connector = RemoteServiceConnector.new(@@apl_config_service)
  is_initialization = true

  @@api_sync_thread = Thread.new do
    while true
      begin
        unless is_initialization
          response = get_api_configurations

          logger.info "debug response we get is #{ response }"
          update_api_config_cache(response)
        end
        is_initialization = false
      rescue Exception => e
        logger.error "Exception while running api_sync_thread. Message - #{e.message}"
        @@error_count = @@error_count + 1;
        if(@@error_count == 10)
          @@api_config = []
          @@error_count = 0
        end
      end
      sleep(@@sleep_time)
    end
  end
  logger.info "Started api_sync_thread - #{@@api_sync_thread}"

  # Graceful termination of api_sync_thread
  at_exit do
    logger.info "Shutting down api_sync_thread - #{@@api_sync_thread}"
    @@api_sync_thread.exit if @@api_sync_thread
  end
end

#update_api_config_cache(api_config) ⇒ Object



48
49
50
51
# File 'lib/apl-library/api_cache.rb', line 48

def update_api_config_cache(api_config)
  @@api_config = api_config
  logger.info "debug updating The cache config with#{@@api_config}"
end