Class: QueuePulse::State

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

Overview

Tiny wrapper over the configured cache for the two pieces of state QueuePulse needs: the failed-execution high-water mark, and per-fingerprint cooldowns.

Backed by Rails.cache (or MemoryStore fallback). If the cache evicts our keys we fail safe: at worst a duplicate alert, never a crash. (Requirements US-2.2, US-3.3, non-functional #3.)

Constant Summary collapse

NAMESPACE =
"queue_pulse"
HWM_KEY =
"#{NAMESPACE}:failed_high_water_mark"

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ State

Returns a new instance of State.



14
15
16
# File 'lib/queue_pulse/state.rb', line 14

def initialize(cache)
  @cache = cache
end

Instance Method Details

#alerted_recently?(fingerprint) ⇒ Boolean

True if this fingerprint was alerted within its cooldown window.

Returns:

  • (Boolean)


30
31
32
# File 'lib/queue_pulse/state.rb', line 30

def alerted_recently?(fingerprint)
  !@cache.read(cooldown_key(fingerprint)).nil?
end

#failed_high_water_markObject



18
19
20
21
22
23
# File 'lib/queue_pulse/state.rb', line 18

def failed_high_water_mark
  value = @cache.read(HWM_KEY)
  value && Integer(value)
rescue ArgumentError, TypeError
  nil
end

#mark_alerted(fingerprint, ttl) ⇒ Object



34
35
36
# File 'lib/queue_pulse/state.rb', line 34

def mark_alerted(fingerprint, ttl)
  @cache.write(cooldown_key(fingerprint), 1, expires_in: ttl)
end

#set_failed_high_water_mark(id) ⇒ Object



25
26
27
# File 'lib/queue_pulse/state.rb', line 25

def set_failed_high_water_mark(id)
  @cache.write(HWM_KEY, id.to_i)
end