[system-health] Fix file handle leak (#10059)

- Why I did it
swsscommon.ConfigDBConnector does not automatically close connection when the instance is recycled by python. So, it should not create this instance each time calling check_services. It will cause error like Failed to read from file /var/run/hw-management/led/led_status_capability - OSError(24, 'Too many open files')

- How I did it
Only connect DB once in init

- How to verify it
Manual test
This commit is contained in:
Junchao-Mellanox 2022-02-24 17:29:59 +08:00 committed by Judy Joseph
parent b58a42a8d4
commit 00940941b2

View File

@ -51,6 +51,8 @@ class ServiceChecker(HealthChecker):
self.need_save_cache = False self.need_save_cache = False
self.config_db = None
self.load_critical_process_cache() self.load_critical_process_cache()
def get_expected_running_containers(self, feature_table): def get_expected_running_containers(self, feature_table):
@ -248,9 +250,10 @@ class ServiceChecker(HealthChecker):
Args: Args:
config (config.Config): Health checker configuration. config (config.Config): Health checker configuration.
""" """
config_db = swsscommon.ConfigDBConnector() if not self.config_db:
config_db.connect() self.config_db = swsscommon.ConfigDBConnector()
feature_table = config_db.get_table("FEATURE") self.config_db.connect()
feature_table = self.config_db.get_table("FEATURE")
expected_running_containers, self.container_feature_dict = self.get_expected_running_containers(feature_table) expected_running_containers, self.container_feature_dict = self.get_expected_running_containers(feature_table)
current_running_containers = self.get_current_running_containers() current_running_containers = self.get_current_running_containers()