701e5b6f073413ac4d63b42d329682d5617e0fe8
[osm/LCM.git] / osm_lcm / lcm_hc.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 ##
5 # Copyright 2018 Telefonica S.A.
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18 ##
19
20 from os import path
21 from time import time, sleep
22 from sys import stderr
23
24 """ This module is used for helth check. A file called time_last_ping is used
25 This contains the last time where something is received from kafka
26 """
27
28
29 def health_check(health_check_file=None, ping_interval_pace=120):
30 health_check_file = health_check_file or path.expanduser("~") + "/time_last_ping"
31 retry = 2
32 while retry:
33 retry -= 1
34 try:
35 with open(health_check_file, "r") as f:
36 last_received_ping = f.read()
37
38 if time() - float(last_received_ping) < 2 * ping_interval_pace: # allow one ping not received every two
39 exit(0)
40 except Exception as e:
41 print(e, file=stderr)
42 if retry:
43 sleep(6)
44 exit(1)
45
46
47 if __name__ == '__main__':
48 health_check()