2 # -*- coding: utf-8 -*-
5 # Copyright 2018 Telefonica S.A.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
20 from time
import time
, sleep
21 from sys
import stderr
23 from osm_lcm
.lcm_utils
import LcmException
26 """ This module is used for health check. A file called time_last_ping is used
27 This contains the last time where something is received from kafka
31 def get_health_check_file(config_file
=None):
33 health_check_file
= "/app/storage/time_last_ping"
35 return health_check_file
36 # If config_input is dictionary
37 if isinstance(config_file
, dict) and config_file
.get("storage"):
38 health_check_file
= config_file
["storage"]["path"] + "/time_last_ping"
39 # If config_input is file
40 elif isinstance(config_file
, str):
41 with
open(config_file
) as f
:
42 # read file as yaml format
43 conf
= yaml
.safe_load(f
)
44 # Ensure all sections are not empty
45 if conf
.get("storage"):
46 health_check_file
= conf
["storage"]["path"] + "/time_last_ping"
48 return health_check_file
49 except (IOError, FileNotFoundError
, TypeError, AttributeError, KeyError) as error
:
51 f
"Error occured while getting the health check file location: {error}"
55 def health_check(config_file
=None, ping_interval_pace
=120):
56 health_check_file
= get_health_check_file(config_file
)
61 with
open(health_check_file
, "r") as f
:
62 last_received_ping
= f
.read()
65 time() - float(last_received_ping
) < 2 * ping_interval_pace
66 ): # allow one ping not received every two
68 except Exception as e
:
75 if __name__
== "__main__":