Skip to content

Commit 2974c3c

Browse files
authored
Merge pull request #26 from scalableminds/grpc-health-in-client-proposal
refactor python client
2 parents 586ac62 + 0e1e566 commit 2974c3c

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

client/fossildb-client

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@ import fossildbapi_pb2_grpc as proto_rpc
1010
from grpc_health.v1 import health_pb2
1111
from grpc_health.v1 import health_pb2_grpc
1212

13-
def main():
14-
15-
commands = {
16-
'backup': lambda stub:
17-
stub.Backup(proto.BackupRequest()),
18-
'restore': lambda stub:
19-
stub.RestoreFromBackup(proto.RestoreFromBackupRequest()),
20-
'health': None
21-
}
2213

14+
def parse_args(commands):
2315
parser = argparse.ArgumentParser()
2416
parser.add_argument(
2517
'address', metavar='address', default='localhost', nargs='?',
@@ -32,39 +24,48 @@ def main():
3224
help='command to execute, one of {}'.format(list(commands.keys())))
3325

3426
args = parser.parse_args()
35-
3627
if args.command not in commands:
3728
print("command {} is not available".format(args.command))
3829
parser.print_help()
3930
exit(20)
4031

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)
4160
full_address = '{}:{}'.format(args.address, args.port)
4261

4362
print('Connecting to FossilDB at', full_address)
44-
4563
channel = grpc.insecure_channel(full_address)
46-
stub = proto_rpc.FossilDBStub(channel)
47-
healthStub = health_pb2_grpc.HealthStub(channel)
48-
49-
if args.command == 'health':
50-
try :
51-
reply = healthStub.Check(health_pb2.HealthCheckRequest(service=''))
52-
print(reply)
53-
UNKNOWN = 0;
54-
SERVING = 1;
55-
NOT_SERVING = 2;
56-
if reply.status != SERVING:
57-
raise Exception(reply.status)
58-
except Exception as e:
59-
print('Health check unsuccessful. FossilDB offline?')
60-
print(e)
61-
sys.exit(1)
62-
63-
else:
64-
reply = commands[args.command](stub)
65-
print(reply)
66-
if not reply.success:
67-
sys.exit(1)
64+
65+
reply = commands[args.command](channel)
66+
print(reply)
67+
if hasattr(reply, 'success') and not reply.success:
68+
sys.exit(1)
6869

6970

7071
if __name__ == '__main__':

0 commit comments

Comments
 (0)