Class: Seekmodo::Sdk::AutoPromoter

Inherits:
Object
  • Object
show all
Defined in:
lib/seekmodo/sdk/auto_promoter.rb

Constant Summary collapse

STATE_KEY =
"numinix.seekmodo.fsm_state"
DEFAULT_PROMOTE_AFTER_SECONDS =
3600
DEFAULT_DEMOTE_COOLDOWN_SECONDS =
900

Instance Method Summary collapse

Constructor Details

#initialize(snapshot, breaker, cache, promote_after_seconds: DEFAULT_PROMOTE_AFTER_SECONDS, demote_cooldown_seconds: DEFAULT_DEMOTE_COOLDOWN_SECONDS, clock: nil) ⇒ AutoPromoter

Returns a new instance of AutoPromoter.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/seekmodo/sdk/auto_promoter.rb', line 15

def initialize(
  snapshot,
  breaker,
  cache,
  promote_after_seconds: DEFAULT_PROMOTE_AFTER_SECONDS,
  demote_cooldown_seconds: DEFAULT_DEMOTE_COOLDOWN_SECONDS,
  clock: nil
)
  @snapshot = snapshot
  @breaker = breaker
  @cache = cache
  @promote_after_seconds = promote_after_seconds
  @demote_cooldown_seconds = demote_cooldown_seconds
  @clock = clock || -> { Time.now.to_i }
end

Instance Method Details

#current_stateObject



73
74
75
# File 'lib/seekmodo/sdk/auto_promoter.rb', line 73

def current_state
  load_state
end

#tickObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/seekmodo/sdk/auto_promoter.rb', line 31

def tick
  config = @snapshot.get
  configured_mode = config.fetch("mode", Mode::OFF).to_s
  auto_promote = config.fetch("auto_promote", true)

  if configured_mode != Mode::ACTIVE
    return result(Mode::OFF, Mode::OFF, "held", "mode is not active; auto-promoter idle")
  end
  unless auto_promote
    return result(Mode::OFF, Mode::OFF, "held", "auto_promote disabled")
  end

  state = load_state
  current = state["current_state"].to_s
  now = @clock.call
  breaker_snapshot = @breaker.snapshot
  breaker_open = breaker_snapshot["state"] == CircuitBreaker::STATE_OPEN

  if breaker_open && current == Mode::ENFORCE
    if now - state["last_transition_at"].to_i < @demote_cooldown_seconds
      return result(current, current, "held", "demote cooldown active")
    end
    write_state(Mode::SHADOW, now)
    return result(current, Mode::SHADOW, "demoted", "breaker open at enforce")
  end

  settled_for = now - state["last_transition_at"].to_i
  if breaker_open
    return result(current, current, "held", "breaker open")
  end
  if settled_for < @promote_after_seconds
    return result(current, current, "held", "settled for #{settled_for}s, need #{@promote_after_seconds}s")
  end

  next_step = next_step_up(current)
  if next_step == current
    return result(current, current, "held", "already at enforce")
  end
  write_state(next_step, now)
  result(current, next_step, "promoted", "breaker closed, settled")
end