Skip to content

Commit fee2ee5

Browse files
authored
Merge pull request #24 from scalableminds/grpc-health-in-client
[WIP] use grpc standard health check in python client
2 parents 7b724bf + 2974c3c commit fee2ee5

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

client/fossildb-client

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@ import sys
77
import fossildbapi_pb2 as proto
88
import fossildbapi_pb2_grpc as proto_rpc
99

10+
from grpc_health.v1 import health_pb2
11+
from grpc_health.v1 import health_pb2_grpc
1012

11-
def main():
12-
13-
commands = {
14-
'backup': lambda stub:
15-
stub.Backup(proto.BackupRequest()),
16-
'restore': lambda stub:
17-
stub.RestoreFromBackup(proto.RestoreFromBackupRequest()),
18-
'health': lambda stub:
19-
stub.Health(proto.HealthRequest())
20-
}
2113

14+
def parse_args(commands):
2215
parser = argparse.ArgumentParser()
2316
parser.add_argument(
2417
'address', metavar='address', default='localhost', nargs='?',
@@ -31,23 +24,47 @@ def main():
3124
help='command to execute, one of {}'.format(list(commands.keys())))
3225

3326
args = parser.parse_args()
34-
3527
if args.command not in commands:
3628
print("command {} is not available".format(args.command))
3729
parser.print_help()
3830
exit(20)
3931

32+
return args
33+
34+
35+
def health(channel):
36+
try :
37+
healthStub = health_pb2_grpc.HealthStub(channel)
38+
reply = healthStub.Check(health_pb2.HealthCheckRequest(service=''))
39+
STATUSMAP = health_pb2._HEALTHCHECKRESPONSE_SERVINGSTATUS.values_by_name
40+
SERVING = STATUSMAP["SERVING"].number
41+
if reply.status != SERVING:
42+
raise Exception(reply.status)
43+
except Exception as e:
44+
print('Health check unsuccessful. FossilDB offline?')
45+
print(e)
46+
sys.exit(1)
47+
return reply
48+
49+
50+
def main():
51+
commands = {
52+
'backup': lambda channel:
53+
proto_rpc.FossilDBStub(channel).Backup(proto.BackupRequest()),
54+
'restore': lambda channel:
55+
proto_rpc.FossilDBStub(channel).RestoreFromBackup(proto.RestoreFromBackupRequest()),
56+
'health': health
57+
}
58+
59+
args = parse_args(commands)
4060
full_address = '{}:{}'.format(args.address, args.port)
4161

4262
print('Connecting to FossilDB at', full_address)
43-
4463
channel = grpc.insecure_channel(full_address)
45-
stub = proto_rpc.FossilDBStub(channel)
46-
47-
reply = commands[args.command](stub)
4864

65+
reply = commands[args.command](channel)
4966
print(reply)
50-
if not reply.success:
67+
if hasattr(reply, 'success') and not reply.success:
5168
sys.exit(1)
5269

5370

client/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
argparse
22
grpcio-tools
3+
grpcio-health-checking

0 commit comments

Comments
 (0)