Refactoring the check backend, create consumers
[sdc.git] / catalog-be / src / main / resources / scripts / sdcBePy / common / healthCheck.py
1 #!/usr/bin/env python3
2 import sys
3 import time
4 from argparse import ArgumentParser
5 from datetime import datetime
6
7 from sdcBePy.common.bColors import BColors
8 from sdcBePy.common.sdcBeProxy import SdcBeProxy
9
10 colors = BColors()
11
12 RETRY_TIME = 10
13 RETRY_ATTEMPTS = 10
14
15
16 def check_backend(sdc_be_proxy=None, reply_append_count=1, be_host=None, be_port=None, scheme=None, debug=False):
17     if sdc_be_proxy is None:
18         sdc_be_proxy = SdcBeProxy(be_host, be_port, scheme, debug=debug)
19
20     for i in range(1, reply_append_count + 1):
21         if sdc_be_proxy.check_backend() == 200:
22             print('[INFO]: Backend is up and running')
23             return True
24         else:
25             print('[WARRING]: ' + datetime.now().strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL
26                   + ' Backend not responding, try #' + str(i) + colors.END_C)
27             time.sleep(RETRY_TIME)
28
29     return False
30
31
32 def run(be_host, be_port, protocol):
33     if not check_backend(reply_append_count=RETRY_ATTEMPTS, be_host=be_host, be_port=be_port, scheme=protocol):
34         print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL + ' Backend is DOWN :-(' + colors.END_C)
35         sys.exit()
36
37
38 def get_args():
39     parser = ArgumentParser()
40
41     parser.add_argument('-i', '--ip', required=True)
42     parser.add_argument('-p', '--port', required=True)
43     parser.add_argument('--https', action='store_true')
44
45     args = parser.parse_args()
46
47     return [args.ip, args.port, 'https' if args.https else 'http']
48
49
50 def main():
51     run(*get_args())
52
53
54 if __name__ == '__main__':
55     main()