Skip to content

Commit 4dc2d0a

Browse files
sujinmkangqiluo-msft
authored andcommitted
Clear all data from DB table when the daemon stops (#228)
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.
1 parent 1df28d7 commit 4dc2d0a

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

sonic-thermalctld/scripts/thermalctld

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,19 @@ class FanUpdater(logger.Logger):
206206
self.drawer_table = swsscommon.Table(state_db, FanUpdater.FAN_DRAWER_INFO_TABLE_NAME)
207207
self.phy_entity_table = swsscommon.Table(state_db, PHYSICAL_ENTITY_INFO_TABLE)
208208

209-
def deinit(self):
210-
"""
211-
Deinitializer of FanUpdater
212-
:return:
213-
"""
214-
for name in self.fan_status_dict.keys():
215-
self.table._del(name)
209+
def __del__(self):
210+
if self.table:
211+
table_keys = self.table.getKeys()
212+
for tk in table_keys:
213+
self.table._del(tk)
214+
if self.drawer_table:
215+
drawer_keys = self.drawer_table.getKeys()
216+
for dtk in drawer_keys:
217+
self.drawer_table._del(dtk)
218+
if self.phy_entity_table:
219+
phy_entity_keys = self.phy_entity_table.getKeys()
220+
for pek in phy_entity_keys:
221+
self.phy_entity_table._del(pek)
216222

217223
def _log_on_status_changed(self, normal_status, normal_log, abnormal_log):
218224
"""
@@ -544,6 +550,16 @@ class TemperatureUpdater(logger.Logger):
544550
chassis_state_db = daemon_base.db_connect("CHASSIS_STATE_DB")
545551
self.chassis_table = swsscommon.Table(chassis_state_db, table_name)
546552

553+
def __del__(self):
554+
if self.table:
555+
table_keys = self.table.getKeys()
556+
for tk in table_keys:
557+
self.table._del(tk)
558+
if self.phy_entity_table:
559+
phy_entity_keys = self.phy_entity_table.getKeys()
560+
for pek in phy_entity_keys:
561+
self.phy_entity_table._del(pek)
562+
547563
def deinit(self):
548564
"""
549565
Deinitializer of TemperatureUpdater
@@ -732,7 +748,6 @@ class ThermalMonitor(ProcessTaskBase):
732748
while not self.task_stopping_event.wait(self.wait_time):
733749
self.main()
734750

735-
self.fan_updater.deinit()
736751
self.temperature_updater.deinit()
737752

738753
self.logger.log_info("Stop thermal monitoring loop")

sonic-thermalctld/tests/test_thermalctld.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,6 @@ class TestFanUpdater(object):
148148
"""
149149
Test cases to cover functionality in FanUpdater class
150150
"""
151-
def test_deinit(self):
152-
fan_updater = thermalctld.FanUpdater(MockChassis(), multiprocessing.Event())
153-
fan_updater.fan_status_dict = {'key1': 'value1', 'key2': 'value2'}
154-
fan_updater.table._del = mock.MagicMock()
155-
156-
fan_updater.deinit()
157-
assert fan_updater.table._del.call_count == 2
158-
expected_calls = [mock.call('key1'), mock.call('key2')]
159-
fan_updater.table._del.assert_has_calls(expected_calls, any_order=True)
160-
161151
@mock.patch('thermalctld.try_get', mock.MagicMock(return_value=thermalctld.NOT_AVAILABLE))
162152
@mock.patch('thermalctld.update_entity_info', mock.MagicMock())
163153
def test_refresh_fan_drawer_status_fan_drawer_get_name_not_impl(self):

0 commit comments

Comments
 (0)