active_record_api-request 0.3.12 → 0.3.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 033313ffd55eed06728b0d905b0230b4d712e741975be067388881b48245f54d
4
- data.tar.gz: c5283f9e03b7fd551a434000c297bab38e1a71a5f5862775080264c2b97a17fe
3
+ metadata.gz: 3e1be4753a2cf5b8394c7313ee0b005a6228646fa71b92fa6574cc73e1825d5f
4
+ data.tar.gz: 43ae9b78126a3880af7a5796dcef31c86b11aa283f6291e078a57915414bf5be
5
5
  SHA512:
6
- metadata.gz: 4c21b2c4099b26ad55ff913a126443323639c1060b0e3934b23a82b9a76404369c070ba9989b8562247b0af12cf6a2c3390b12a76281abff51ee2e736a251891
7
- data.tar.gz: 1b034d3d746aaa71493fc9c498d43b3916089556704e931ad6d13de91294975bc62d56d2dc651f0997b01aa1ad2d93f4859f4896da007f8f83063741945e8c19
6
+ metadata.gz: '008d6a81a7346699fc0cb9f0089ba6ecc7d7cd0b33fe48238e4cedad91ff9914d0cc111df64b20b5e60ad0d2625860ae6a3e1aa36e35f44b7f73c19de3513945'
7
+ data.tar.gz: 96ad30c6ff561125efbee2238c8f4bafbbf282528326ec1d593b74d15489787db01c7530b68971a838342b7262aff14b4437a8e330bfab8de85f6c09199c2bc7
@@ -3,17 +3,17 @@ module ActiveRecordApi
3
3
  module Request
4
4
  class TokenCache
5
5
  include ActiveAttr::Model
6
- attr_accessor :debug
6
+ attr_accessor :debug
7
7
 
8
8
  def flush
9
9
  return unless defined? AUTH_REDIS_POOL
10
10
  debug_log 'flushing token cache'
11
- Honeybadger.notify(StandardError.new('token cache has been flushed'))
12
11
  AUTH_REDIS_POOL.with do |client|
13
12
  @redis_client = client
14
13
  @redis_client.del('auth_token')
15
14
  release_lock
16
15
  end
16
+ debug_log 'token cache has been flushed'
17
17
  end
18
18
 
19
19
  def fetch_token(token_proc)
@@ -21,28 +21,27 @@ module ActiveRecordApi
21
21
  end
22
22
 
23
23
  def retrieve_token_from_redis(token_proc)
24
- debug_log 'looking?'
24
+ debug_log 'looking for token in cache'
25
25
  unless defined? AUTH_REDIS_POOL
26
- debug_log 'no auth redis pool'
27
- Honeybadger.notify(StandardError.new('no auth redis pool for token cache'))
28
- return token_proc.call()
26
+ debug_log 'no auth redis pool. getting new token'
27
+ token = token_proc.call
28
+ raise "no token returned! #{token}" if token.nil?
29
+ return token
29
30
  end
30
- debug_log 'looking for token in cache'
31
31
  AUTH_REDIS_POOL.with do |client|
32
32
  @redis_client = client
33
33
  @token = nil
34
34
  @retry = 0
35
35
  while @token.nil? && @retry < 5
36
- @retry += 1
37
36
  @token = @redis_client.get('auth_token')
38
37
  debug_log "retry ##{@retry}"
39
38
  debug_log "found token: #{@token}"
40
- if @token.nil?
39
+ @retry += 1
40
+ if @token.nil? || session_missing_for?(@token)
41
41
  if acquire_lock
42
42
  begin
43
- debug_log "acquired lock"
44
- Honeybadger.notify(StandardError.new('acquired lock and fetching token for cache'))
45
- @token = token_proc.call()
43
+ debug_log 'acquired lock and fetching token for cache'
44
+ @token = token_proc.call
46
45
  raise "no token returned! #{@token}" if @token.nil?
47
46
  @redis_client.set 'auth_token', @token
48
47
  ensure
@@ -60,14 +59,36 @@ module ActiveRecordApi
60
59
  @token
61
60
  end
62
61
 
62
+ def redis_session_impossible?
63
+ return true unless defined? Redis
64
+ ENV['REDIS_SESSION_HOST'].nil? || ENV['REDIS_SESSION'].nil?
65
+ end
66
+
67
+ def redis_session
68
+ if redis_session_impossible?
69
+ debug_log 'Redis session environment variables not present.'
70
+ return nil
71
+ end
72
+
73
+ @redis_session ||= Redis.new({ url: "redis://#{ENV['REDIS_SESSION_HOST']}", db: ENV['REDIS_SESSION'] })
74
+ end
75
+
76
+ def session_missing_for?(token)
77
+ return false if redis_session.nil?
78
+ session = (redis_session.send(:get, token) if redis_session.respond_to?(:get))
79
+ debug_log("Session located for token #{token}") unless session.nil?
80
+ debug_log("Session could not be located for token #{token}") if session.nil?
81
+ session.nil?
82
+ end
83
+
63
84
  def debug_log(message)
64
- Rails.logger.info message if debug
85
+ Rails.logger.info message if debug && defined? Rails
65
86
  end
66
87
 
67
88
  def acquire_lock
68
- @redis_client.set 'auth_token_lock', unique_key, nx: true, ex: 60
89
+ @redis_client.set 'auth_token_lock', unique_key, nx: true, ex: 8
69
90
  current_lock_value = @redis_client.get 'auth_token_lock'
70
- Rails.logger.info "#{current_lock_value} == #{unique_key}" if debug
91
+ debug_log "#{current_lock_value} == #{unique_key}"
71
92
  current_lock_value == unique_key
72
93
  end
73
94
 
@@ -81,4 +102,5 @@ module ActiveRecordApi
81
102
  end
82
103
  end
83
104
  end
105
+
84
106
  #:nocov:#
@@ -1,5 +1,5 @@
1
1
  module ActiveRecordApi
2
2
  module Request
3
- VERSION = '0.3.12'.freeze
3
+ VERSION = '0.3.16'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_api-request
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.12
4
+ version: 0.3.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Full Measure Education
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-20 00:00:00.000000000 Z
11
+ date: 2021-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_attr