Skip to content

Commit bd00f0a

Browse files
committed
Add boot test for sagittarius grpc server
1 parent e0a9cec commit bd00f0a

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

.gitlab/ci/container-boot.gitlab-ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ container:boot:sagittarius:rails-web:
5858
--retry-delay 3
5959
--retry-connrefused
6060
http://sagittarius:3000/health/liveness
61+
62+
container:boot:sagittarius:grpc:
63+
extends:
64+
- .container:boot:sagittarius
65+
variables:
66+
SAGITTARIUS_CMD: "bin/grpc_server"
67+
before_script:
68+
- bundle install
69+
- !reference [.container:boot:sagittarius, before_script]
70+
script:
71+
- scripts/grpc_check_health --host sagittarius:50051 --service liveness --retry 20
72+
- scripts/grpc_check_health --host sagittarius:50051 --service readiness --retry 20

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
source "https://rubygems.org"
44

55
gem "erb"
6+
7+
gem "grpc", "~> 1.73"
8+
9+
gem "optparse", "~> 0.6.0"

Gemfile.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
bigdecimal (3.2.2)
45
erb (5.0.1)
6+
google-protobuf (4.31.1)
7+
bigdecimal
8+
rake (>= 13)
9+
googleapis-common-protos-types (1.20.0)
10+
google-protobuf (>= 3.18, < 5.a)
11+
grpc (1.73.0)
12+
google-protobuf (>= 3.25, < 5.0)
13+
googleapis-common-protos-types (~> 1.0)
14+
optparse (0.6.0)
15+
rake (13.3.0)
516

617
PLATFORMS
718
ruby
819

920
DEPENDENCIES
1021
erb
22+
grpc (~> 1.73)
23+
optparse (~> 0.6.0)
1124

1225
BUNDLED WITH
1326
2.6.6

scripts/grpc_check_health

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'grpc'
4+
require 'optparse'
5+
require 'grpc/health/v1/health_services_pb'
6+
7+
options = {
8+
retries: 5,
9+
backoff: 3,
10+
service: '',
11+
}
12+
13+
optparse = OptionParser.new do |opts|
14+
opts.banner = 'Usage: grpc_check_health [options]'
15+
16+
opts.on('-h', '--host [HOST]', String, 'Host of the grpc server to check') do |host|
17+
options[:host] = host
18+
end
19+
20+
opts.on('-r', '--retries [RETRIES]', Integer, "Amount of retries (Default: #{options[:retries]})") do |retries|
21+
options[:retries] = retries
22+
end
23+
24+
opts.on('-b', '--backoff [BACKOFF]', Integer, "Backoff in seconds for each attempt (Default: #{options[:backoff]})") do |backoff|
25+
options[:backoff] = backoff
26+
end
27+
28+
opts.on('-s', '--service [SERVICE]', String, "Service check health for (Default: '#{options[:service]}')") do |service|
29+
options[:service] = service
30+
end
31+
end
32+
optparse.parse!
33+
34+
missing = %i[host].select{ |param| options[param].nil? }
35+
unless missing.empty?
36+
puts "Missing arguments: #{missing.join(', ')}"
37+
puts
38+
puts optparse
39+
exit
40+
end
41+
42+
RetryException = Class.new(StandardError)
43+
44+
attempt = 0
45+
46+
stub = Grpc::Health::V1::Health::Stub.new(options[:host], :this_channel_is_insecure)
47+
begin
48+
message = Grpc::Health::V1::HealthCheckRequest.new(service: options[:service])
49+
50+
response = stub.check(message)
51+
52+
if response.status == :SERVING
53+
puts "Service '#{options[:service]}' on #{options[:host]} is #{response.status}"
54+
else
55+
raise RetryException
56+
end
57+
rescue GRPC::BadStatus, RetryException => e
58+
if e.is_a?(GRPC::BadStatus)
59+
message = e.inspect
60+
else
61+
message = "Service #{options[:service]} on #{options[:host]} is not SERVING (status is #{response&.status})"
62+
end
63+
64+
attempt += 1
65+
if attempt <= options[:retries]
66+
puts message
67+
puts "Retrying in #{options[:backoff]}s"
68+
sleep options[:backoff]
69+
retry
70+
end
71+
72+
abort message
73+
end

0 commit comments

Comments
 (0)