Skip to content

Commit

Permalink
[Core] Fix getting libtorrent alert type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cas-- committed Mar 7, 2023
1 parent 196086c commit 39bd97f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
6 changes: 5 additions & 1 deletion deluge/core/alertmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def register_handler(self, alert_type: str, handler: Callable[[Any], None]) -> N
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)

Expand Down Expand Up @@ -111,7 +114,8 @@ def handle_alerts(self):
)

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()))
Expand Down
2 changes: 1 addition & 1 deletion deluge/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(
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)
Expand Down
48 changes: 23 additions & 25 deletions deluge/core/torrentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,32 @@ def __init__(self):

# 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
Expand Down
16 changes: 10 additions & 6 deletions deluge/tests/test_alertmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ def tear_down(self):

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

0 comments on commit 39bd97f

Please sign in to comment.