Skip to content

Commit

Permalink
[WebUI] Fix error stopping daemon in connection manager
Browse files Browse the repository at this point in the history
The wrong number of arguments was being parsed from get_host_info.
  • Loading branch information
cas-- committed Aug 24, 2024
1 parent 80985c0 commit 1e5f248
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 53 deletions.
19 changes: 10 additions & 9 deletions deluge/tests/test_web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,28 @@ def test_set_config(self):

@defer.inlineCallbacks
def get_host_status(self):
host = list(self.deluge_web.web_api._get_host(self.host_id))
host = list(self.deluge_web.web_api.hostlist.get_host_info(self.host_id))
host[3] = 'Online'
host[4] = '2.0.0.dev562'
status = yield self.deluge_web.web_api.get_host_status(self.host_id)
assert status == tuple(status)

def test_get_host(self):
assert not self.deluge_web.web_api._get_host('invalid_id')
conn = list(self.deluge_web.web_api.hostlist.get_hosts_info()[0])
assert self.deluge_web.web_api._get_host(conn[0]) == conn[0:4]
def test_get_hosts(self):
hosts = self.deluge_web.web_api.hostlist.get_hosts_info()
assert self.deluge_web.web_api.get_hosts() == hosts

def test_add_host(self):
conn = ['abcdef', '10.0.0.1', 0, 'user123', 'pass123']
assert not self.deluge_web.web_api._get_host(conn[0])
assert not self.deluge_web.web_api.hostlist.get_host_info(conn[0])
# Add valid host
result, host_id = self.deluge_web.web_api.add_host(
conn[1], conn[2], conn[3], conn[4]
)
assert result
conn[0] = host_id
assert self.deluge_web.web_api._get_host(conn[0]) == conn[0:4]
assert (
list(self.deluge_web.web_api.hostlist.get_host_info(conn[0])) == conn[0:4]
)

# Add already existing host
ret = self.deluge_web.web_api.add_host(conn[1], conn[2], conn[3], conn[4])
Expand All @@ -112,10 +113,10 @@ def test_add_host(self):
def test_remove_host(self):
conn = ['connection_id', '', 0, '', '']
self.deluge_web.web_api.hostlist.config['hosts'].append(conn)
assert self.deluge_web.web_api._get_host(conn[0]) == conn[0:4]
assert self.deluge_web.web_api.hostlist.get_host_info(conn[0]) == conn[0:4]
# Remove valid host
assert self.deluge_web.web_api.remove_host(conn[0])
assert not self.deluge_web.web_api._get_host(conn[0])
assert not self.deluge_web.web_api.hostlist.get_host_info(conn[0])
# Remove non-existing host
assert not self.deluge_web.web_api.remove_host(conn[0])

Expand Down
56 changes: 12 additions & 44 deletions deluge/ui/web/json_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
from deluge import component, httpdownloader
from deluge.common import AUTH_LEVEL_DEFAULT, get_magnet_info, is_magnet
from deluge.configmanager import get_config_dir
from deluge.decorators import maybe_coroutine
from deluge.error import NotAuthorizedError
from deluge.i18n import get_languages
from deluge.ui.client import Client, client
from deluge.ui.client import client
from deluge.ui.common import FileTree2, TorrentInfo
from deluge.ui.coreconfig import CoreConfig
from deluge.ui.hostlist import HostList
Expand Down Expand Up @@ -744,18 +745,6 @@ def add_torrents(self, torrents):
deferreds.append(d)
return DeferredList(deferreds, consumeErrors=False)

def _get_host(self, host_id):
"""Information about a host from supplied host id.
Args:
host_id (str): The id of the host.
Returns:
list: The host information, empty list if not found.
"""
return list(self.hostlist.get_host_info(host_id))

@export
def get_hosts(self):
"""
Expand Down Expand Up @@ -838,39 +827,18 @@ def start_daemon(self, port):
client.start_daemon(port, get_config_dir())

@export
def stop_daemon(self, host_id):
"""
Stops a running daemon.
:param host_id: the hash id of the host
:type host_id: string
"""
main_deferred = Deferred()
host = self._get_host(host_id)
if not host:
main_deferred.callback((False, _('Daemon does not exist')))
return main_deferred

@maybe_coroutine
async def stop_daemon(self, host_id):
try:
await self.hostlist.connect_host(host_id)
except Exception as err:
msg = f'Error occurred stopping daemon: {err}'
result = (False, msg)
else:
client.daemon.shutdown()
result = (True,)

def on_connect(connected, c):
if not connected:
main_deferred.callback((False, _('Daemon not running')))
return
c.daemon.shutdown()
main_deferred.callback((True,))

def on_connect_failed(reason):
main_deferred.callback((False, reason))

host, port, user, password = host[1:5]
c = Client()
d = c.connect(host, port, user, password)
d.addCallback(on_connect, c)
d.addErrback(on_connect_failed)
except Exception:
main_deferred.callback((False, 'An error occurred'))
return main_deferred
return Deferred().callback(result)

@export
def get_config(self):
Expand Down

0 comments on commit 1e5f248

Please sign in to comment.