Class: Legion::Cache::Reconnector

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/legion/cache/reconnector.rb

Constant Summary collapse

DEFAULT_INITIAL_DELAY =
1
DEFAULT_MAX_DELAY =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tier:, connect_block:, enabled_block:, settings_key: :cache) ⇒ Reconnector

Returns a new instance of Reconnector.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/cache/reconnector.rb', line 14

def initialize(tier:, connect_block:, enabled_block:, settings_key: :cache)
  @tier = tier
  @connect_block = connect_block
  @enabled_block = enabled_block
  @settings_key = settings_key
  @attempts = Concurrent::AtomicFixnum.new(0)
  @thread = nil
  @mutex = Mutex.new
  @stop_signal = Concurrent::AtomicBoolean.new(false)
  @next_retry_at = nil
end

Instance Attribute Details

#next_retry_atObject (readonly)

Returns the value of attribute next_retry_at.



55
56
57
# File 'lib/legion/cache/reconnector.rb', line 55

def next_retry_at
  @next_retry_at
end

Instance Method Details

#attemptsObject



51
52
53
# File 'lib/legion/cache/reconnector.rb', line 51

def attempts
  @attempts.value
end

#running?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/legion/cache/reconnector.rb', line 47

def running?
  @thread&.alive? == true
end

#startObject



26
27
28
29
30
31
32
33
34
# File 'lib/legion/cache/reconnector.rb', line 26

def start
  @mutex.synchronize do
    return if running?

    @stop_signal.make_false
    @thread = Thread.new { reconnect_loop }
    log.info "Legion::Cache::Reconnector[#{@tier}] started"
  end
end

#stopObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/cache/reconnector.rb', line 36

def stop
  thread_to_join = nil
  @mutex.synchronize do
    @stop_signal.make_true
    thread_to_join = @thread
    @thread = nil
  end
  thread_to_join&.join(5)
  log.info "Legion::Cache::Reconnector[#{@tier}] stopped"
end