Class: FeatBit::StatusProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/featbit/status.rb

Instance Method Summary collapse

Constructor Details

#initialize(initial = Status::STARTING, logger: nil) ⇒ StatusProvider

Returns a new instance of StatusProvider.



14
15
16
17
18
19
20
21
22
# File 'lib/featbit/status.rb', line 14

def initialize(initial = Status::STARTING, logger: nil)
  @status = initial
  @message = nil
  @listeners = {}
  @next_listener_id = 0
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @logger = logger
end

Instance Method Details

#add_listener(callable = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/featbit/status.rb', line 53

def add_listener(callable = nil, &block)
  listener = callable || block
  return nil unless listener.respond_to?(:call)

  @mutex.synchronize do
    @next_listener_id += 1
    @listeners[@next_listener_id] = listener
    @next_listener_id
  end
rescue StandardError
  nil
end

#messageObject



28
29
30
# File 'lib/featbit/status.rb', line 28

def message
  @mutex.synchronize { @message }
end

#ready?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/featbit/status.rb', line 32

def ready?
  status == Status::READY
end

#remove_listener(id) ⇒ Object



66
67
68
69
70
# File 'lib/featbit/status.rb', line 66

def remove_listener(id)
  @mutex.synchronize { !@listeners.delete(id).nil? }
rescue StandardError
  false
end

#statusObject



24
25
26
# File 'lib/featbit/status.rb', line 24

def status
  @mutex.synchronize { @status }
end

#update(new_status, message: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/featbit/status.rb', line 72

def update(new_status, message: nil)
  listeners = @mutex.synchronize do
    changed = @status != new_status || @message != message
    @status = new_status
    @message = message
    @condition.broadcast
    changed ? @listeners.values.dup : []
  end
  listeners.each do |listener|
    listener.call(new_status, message)
  rescue StandardError => e
    safe_log(:warn, "FeatBit status listener failed: #{e.message}")
  end
  true
rescue StandardError => e
  safe_log(:warn, "FeatBit status update failed: #{e.message}")
  false
end

#wait_until_ready(timeout = 5.0) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/featbit/status.rb', line 36

def wait_until_ready(timeout = 5.0)
  deadline = monotonic_time + timeout.to_f
  @mutex.synchronize do
    until @status == Status::READY
      return false if [Status::FAILED, Status::CLOSED].include?(@status)

      remaining = deadline - monotonic_time
      return false unless remaining.positive?

      @condition.wait(@mutex, remaining)
    end
  end
  true
rescue StandardError
  false
end