Class: EventQ::NonceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/eventq/eventq_base/nonce_manager.rb

Class Method Summary collapse

Class Method Details

.complete(nonce) ⇒ Object

if the message was successfully procesed, lock for another lifespan length so it isn’t reprocessed



62
63
64
65
66
67
68
69
70
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 62

def self.complete(nonce)
  return true if !configured?

  with_redis_connection do |conn|
    conn.expire(nonce, lifespan)
  end

  true
end

.configure(server:, timeout: 10000, lifespan: 3600, pool_size: 5, pool_timeout: 5) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 6

def self.configure(server:,timeout:10000,lifespan:3600, pool_size: 5, pool_timeout: 5)
  @server_url = server
  @timeout = timeout
  @lifespan = lifespan
  @pool_size = pool_size
  @pool_timeout = pool_timeout

  @redis_pool = begin
    require 'connection_pool'
    require 'redis'

    ConnectionPool.new(size: @pool_size, timeout: @pool_timeout) do
      Redis.new(url: @server_url)
    end
  end
  @configured = true
end

.configured?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 93

def self.configured?
  @configured == true
end

.failed(nonce) ⇒ Object

if it failed, unlock immediately so that retries can kick in



73
74
75
76
77
78
79
80
81
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 73

def self.failed(nonce)
  return true if !configured?

  with_redis_connection do |conn|
    conn.del(nonce)
  end

  true
end

.lifespanObject



32
33
34
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 32

def self.lifespan
  @lifespan
end

.lock(nonce) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 44

def self.lock(nonce)
  # act as if successfully locked if not nonce manager configured - makes it a no-op
  return true if !configured?

  successfully_locked = false
  with_redis_connection do |conn|
    successfully_locked = conn.set(nonce, 1, ex: lifespan, nx: true)
  end

  if !successfully_locked
    EventQ.log(:info, "[#{self.class}] - Message has already been processed: #{nonce}")
  end

  successfully_locked
end

.pool_sizeObject



36
37
38
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 36

def self.pool_size
  @pool_size
end

.pool_timeoutObject



40
41
42
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 40

def self.pool_timeout
  @pool_timeout
end

.resetObject



83
84
85
86
87
88
89
90
91
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 83

def self.reset
  @server_url = nil
  @timeout = nil
  @lifespan = nil
  @pool_size = nil
  @pool_timeout = nil
  @configured = false
  @redis_pool.reload(&:close)
end

.server_urlObject



24
25
26
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 24

def self.server_url
  @server_url
end

.timeoutObject



28
29
30
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 28

def self.timeout
  @timeout
end

.with_redis_connectionObject



99
100
101
102
103
104
105
106
107
# File 'lib/eventq/eventq_base/nonce_manager.rb', line 99

def self.with_redis_connection
  if !configured?
    raise NonceManagerNotConfiguredError, 'Unable to checkout redis connection from pool, nonce manager has not been configured. Call .configure on NonceManager.'
  end

  @redis_pool.with do |conn|
    yield conn
  end
end