Skip to content

Commit

Permalink
Clear all data from DB table when the daemon stops (#228)
Browse files Browse the repository at this point in the history
Description
Clear all data from DB table when the daemon stops

Motivation and Context
Clean up when the daemon stops

How Has This Been Tested?
Check Thermal and physical info data if they are cleared after stopping the thermalctld.
  • Loading branch information
sujinmkang authored Dec 2, 2021
1 parent da3dbcc commit 8bad10a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
31 changes: 23 additions & 8 deletions sonic-thermalctld/scripts/thermalctld
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,19 @@ class FanUpdater(logger.Logger):
self.drawer_table = swsscommon.Table(state_db, FanUpdater.FAN_DRAWER_INFO_TABLE_NAME)
self.phy_entity_table = swsscommon.Table(state_db, PHYSICAL_ENTITY_INFO_TABLE)

def deinit(self):
"""
Deinitializer of FanUpdater
:return:
"""
for name in self.fan_status_dict.keys():
self.table._del(name)
def __del__(self):
if self.table:
table_keys = self.table.getKeys()
for tk in table_keys:
self.table._del(tk)
if self.drawer_table:
drawer_keys = self.drawer_table.getKeys()
for dtk in drawer_keys:
self.drawer_table._del(dtk)
if self.phy_entity_table:
phy_entity_keys = self.phy_entity_table.getKeys()
for pek in phy_entity_keys:
self.phy_entity_table._del(pek)

def _log_on_status_changed(self, normal_status, normal_log, abnormal_log):
"""
Expand Down Expand Up @@ -550,6 +556,16 @@ class TemperatureUpdater(logger.Logger):
except Exception as e:
self.chassis_table = None

def __del__(self):
if self.table:
table_keys = self.table.getKeys()
for tk in table_keys:
self.table._del(tk)
if self.phy_entity_table:
phy_entity_keys = self.phy_entity_table.getKeys()
for pek in phy_entity_keys:
self.phy_entity_table._del(pek)

def deinit(self):
"""
Deinitializer of TemperatureUpdater
Expand Down Expand Up @@ -778,7 +794,6 @@ class ThermalMonitor(ProcessTaskBase):
while not self.task_stopping_event.wait(self.wait_time):
self.main()

self.fan_updater.deinit()
self.temperature_updater.deinit()

self.logger.log_info("Stop thermal monitoring loop")
Expand Down
10 changes: 0 additions & 10 deletions sonic-thermalctld/tests/test_thermalctld.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,6 @@ class TestFanUpdater(object):
"""
Test cases to cover functionality in FanUpdater class
"""
def test_deinit(self):
fan_updater = thermalctld.FanUpdater(MockChassis(), multiprocessing.Event())
fan_updater.fan_status_dict = {'key1': 'value1', 'key2': 'value2'}
fan_updater.table._del = mock.MagicMock()

fan_updater.deinit()
assert fan_updater.table._del.call_count == 2
expected_calls = [mock.call('key1'), mock.call('key2')]
fan_updater.table._del.assert_has_calls(expected_calls, any_order=True)

@mock.patch('thermalctld.try_get', mock.MagicMock(return_value=thermalctld.NOT_AVAILABLE))
@mock.patch('thermalctld.update_entity_info', mock.MagicMock())
def test_refresh_fan_drawer_status_fan_drawer_get_name_not_impl(self):
Expand Down

0 comments on commit 8bad10a

Please sign in to comment.