From 39bd97f03e270f834446c032a2cb0d23d90c2313 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Tue, 7 Mar 2023 13:13:33 +0000 Subject: [PATCH] [Core] Fix getting libtorrent alert type Encountered a problem with dht_error alert not returning the correct alert name using Python type. This should likely be fixed in libtorrent but we should be using the alert.what method to determine alert type/name. Since the alert name does not include the `_alert` suffix, strip this when registering alerts. --- deluge/core/alertmanager.py | 6 +++- deluge/core/core.py | 2 +- deluge/core/torrentmanager.py | 48 +++++++++++++++---------------- deluge/tests/test_alertmanager.py | 16 +++++++---- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/deluge/core/alertmanager.py b/deluge/core/alertmanager.py index 9871c1f03..51a7f2993 100644 --- a/deluge/core/alertmanager.py +++ b/deluge/core/alertmanager.py @@ -79,6 +79,9 @@ class AlertManager(component.Component): Can be supplied with or without `_alert` suffix. handler: Callback function when the alert is raised. """ + if alert_type and alert_type.endswith('_alert'): + alert_type = alert_type[: -len('_alert')] + self.handlers[alert_type].append(handler) log.debug('Registered handler for alert %s', alert_type) @@ -111,7 +114,8 @@ class AlertManager(component.Component): ) for alert in alerts: - alert_type = type(alert).__name__ + alert_type = alert.what() + # Display the alert message if log.isEnabledFor(logging.DEBUG): log.debug('%s: %s', alert_type, decode_bytes(alert.message())) diff --git a/deluge/core/core.py b/deluge/core/core.py index d5d95a3ac..198410e31 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -199,7 +199,7 @@ class Core(component.Component): self.session_status_timer_interval = 0.5 self.session_status_timer = task.LoopingCall(self.session.post_session_stats) self.alertmanager.register_handler( - 'session_stats_alert', self._on_alert_session_stats + 'session_stats', self._on_alert_session_stats ) self.session_rates_timer_interval = 2 self.session_rates_timer = task.LoopingCall(self._update_session_rates) diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 4a68f9864..a758d5c62 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -202,34 +202,32 @@ class TorrentManager(component.Component): # Register alert functions alert_handles = [ - 'external_ip_alert', - 'performance_alert', - 'add_torrent_alert', - 'metadata_received_alert', - 'torrent_finished_alert', - 'torrent_paused_alert', - 'torrent_checked_alert', - 'torrent_resumed_alert', - 'tracker_reply_alert', - 'tracker_announce_alert', - 'tracker_warning_alert', - 'tracker_error_alert', - 'file_renamed_alert', - 'file_error_alert', - 'file_completed_alert', - 'storage_moved_alert', - 'storage_moved_failed_alert', - 'state_update_alert', - 'state_changed_alert', - 'save_resume_data_alert', - 'save_resume_data_failed_alert', - 'fastresume_rejected_alert', + 'external_ip', + 'performance', + 'add_torrent', + 'metadata_received', + 'torrent_finished', + 'torrent_paused', + 'torrent_checked', + 'torrent_resumed', + 'tracker_reply', + 'tracker_announce', + 'tracker_warning', + 'tracker_error', + 'file_renamed', + 'file_error', + 'file_completed', + 'storage_moved', + 'storage_moved_failed', + 'state_update', + 'state_changed', + 'save_resume_data', + 'save_resume_data_failed', + 'fastresume_rejected', ] for alert_handle in alert_handles: - on_alert_func = getattr( - self, ''.join(['on_alert_', alert_handle.replace('_alert', '')]) - ) + on_alert_func = getattr(self, ''.join(['on_alert_', alert_handle])) self.alerts.register_handler(alert_handle, on_alert_func) # Define timers diff --git a/deluge/tests/test_alertmanager.py b/deluge/tests/test_alertmanager.py index 5e63864e8..683b011b4 100644 --- a/deluge/tests/test_alertmanager.py +++ b/deluge/tests/test_alertmanager.py @@ -21,15 +21,19 @@ class TestAlertManager(BaseTestCase): def test_register_handler(self): def handler(alert): - return + ... - self.am.register_handler('dummy_alert', handler) - assert self.am.handlers['dummy_alert'] == [handler] + self.am.register_handler('dummy1', handler) + self.am.register_handler('dummy2_alert', handler) + assert self.am.handlers['dummy1'] == [handler] + assert self.am.handlers['dummy2'] == [handler] def test_deregister_handler(self): def handler(alert): - return + ... - self.am.register_handler('dummy_alert', handler) + self.am.register_handler('dummy1', handler) + self.am.register_handler('dummy2_alert', handler) self.am.deregister_handler(handler) - assert self.am.handlers['dummy_alert'] == [] + assert self.am.handlers['dummy1'] == [] + assert self.am.handlers['dummy2'] == []