[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.
This commit is contained in:
Calum Lind 2023-03-07 13:13:33 +00:00
parent 196086c1fb
commit 39bd97f03e
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
4 changed files with 39 additions and 33 deletions

View File

@ -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()))

View File

@ -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)

View File

@ -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

View File

@ -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'] == []