Class: Grpc::Health::Checker

Inherits:
V1::Health::Service show all
Defined in:
lib/anycable/grpc_kit/health_checker.rb

Overview

Checker is implementation of the schema-specified health checking service.

Constant Summary collapse

StatusCodes =
GRPC::Core::StatusCodes
HealthCheckResponse =
V1::HealthCheckResponse

Instance Method Summary collapse

Constructor Details

#initializeChecker

Initializes the statuses of participating services



25
26
27
28
# File 'lib/anycable/grpc_kit/health_checker.rb', line 25

def initialize
  @statuses = {}
  @status_mutex = Mutex.new  # guards access to @statuses
end

Instance Method Details

#add_status(service, status) ⇒ Object

Adds the health status for a given service.



43
44
45
# File 'lib/anycable/grpc_kit/health_checker.rb', line 43

def add_status(service, status)
  @status_mutex.synchronize { @statuses["#{service}"] = status }
end

#add_statuses(service_statuses = {}) ⇒ Object

Adds health status for each service given within hash



55
56
57
58
59
# File 'lib/anycable/grpc_kit/health_checker.rb', line 55

def add_statuses(service_statuses = {})
  @status_mutex.synchronize do
    service_statuses.each_pair { |service, status| @statuses["#{service}"] = status }
  end
end

#check(req, _call) ⇒ Object

Implements the rpc IDL API method



31
32
33
34
35
36
37
38
39
40
# File 'lib/anycable/grpc_kit/health_checker.rb', line 31

def check(req, _call)
  status = nil
  @status_mutex.synchronize do
    status = @statuses["#{req.service}"]
  end
  if status.nil?
    fail GRPC::NotFound.new("Service is not found: #{req.service}")
  end
  HealthCheckResponse.new(status: status)
end

#clear_allObject

Clears alls the statuses.



67
68
69
# File 'lib/anycable/grpc_kit/health_checker.rb', line 67

def clear_all
  @status_mutex.synchronize { @statuses = {} }
end

#clear_status(service) ⇒ Object

Clears the status for the given service.



62
63
64
# File 'lib/anycable/grpc_kit/health_checker.rb', line 62

def clear_status(service)
  @status_mutex.synchronize { @statuses.delete("#{service}") }
end

#set_status_for_services(status, *services) ⇒ Object

Adds given health status for all given services



48
49
50
51
52
# File 'lib/anycable/grpc_kit/health_checker.rb', line 48

def set_status_for_services(status, *services)
  @status_mutex.synchronize do
    services.each { |service| @statuses["#{service}"] = status }
  end
end