Skip to content

Commit

Permalink
Fixes #6262, fixes #6268: correct delayed initialization of endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Aug 20, 2021
1 parent 91add83 commit e273b5c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ async def run(self):

self.download_manager = download_manager

for endpoint in self.endpoints:
rest_manager.get_endpoint(endpoint).download_manager = download_manager
rest_manager.set_attr_for_endpoints(self.endpoints, 'download_manager', download_manager, skip_missing=True)

if config.gui_test_mode:
uri = "magnet:?xt=urn:btih:0000000000000000000000000000000000000000"
await download_manager.start_download_from_uri(uri)

async def shutdown(self):
# Release endpoints
for endpoint in self.endpoints:
self.rest_manager.get_endpoint(endpoint).download_manager = None
self.rest_manager.set_attr_for_endpoints(self.endpoints, 'download_manager', None, skip_missing=True)

await self.release(RESTComponent)

self.download_manager.stop_download_states_callback()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ async def run(self):
disable_sync=config.gui_test_mode,
)
self.mds = metadata_store

for endpoint in self.endpoints:
rest_manager.get_endpoint(endpoint).mds = metadata_store
rest_manager.set_attr_for_endpoints(self.endpoints, 'mds', metadata_store, skip_missing=True)

if config.gui_test_mode:
generate_test_channels(metadata_store)

async def shutdown(self):
# Release endpoints
for endpoint in self.endpoints:
self.rest_manager.get_endpoint(endpoint).mds = None
self.rest_manager.set_attr_for_endpoints(self.endpoints, 'mds', None, skip_missing=True)
await self.release(RESTComponent)

await self.unused.wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ async def run(self):
rest_manager.get_endpoint('state').readable_status = STATE_START_TORRENT_CHECKER

await torrent_checker.initialize()
rest_manager.get_endpoint('metadata').torrent_checker = torrent_checker
rest_manager.set_attr_for_endpoints(['metadata'], 'torrent_checker', torrent_checker, skip_missing=True)

async def shutdown(self):
self.session.notifier.notify_shutdown_state("Shutting down Torrent Checker...")
self.rest_manager.get_endpoint('metadata').torrent_checker = None
self.rest_manager.set_attr_for_endpoints(['metadata'], 'torrent_checker', None, skip_missing=True)

await self.release(RESTComponent)

await self.torrent_checker.shutdown()
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MetadataStoreComponent(Component):

@classmethod
def should_be_enabled(cls, config: TriblerConfig):
return config.chant.enabled
return config.chant.enabled or config.torrent_checking.enabled

@classmethod
def make_implementation(cls, config: TriblerConfig, enable: bool):
Expand Down
12 changes: 12 additions & 0 deletions src/tribler-core/tribler_core/restapi/rest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import ssl
import traceback
from typing import List

from aiohttp import web
from aiohttp.web_exceptions import HTTPNotFound
Expand Down Expand Up @@ -97,6 +98,17 @@ def __init__(self, config: APISettings, root_endpoint: RootEndpoint, state_dir=N
def get_endpoint(self, name):
return self.root_endpoint.endpoints['/' + name]

def set_attr_for_endpoints(self, endpoints: List[str], attr_name: str, attr_value, skip_missing=False):
"""
Set attribute value for each endpoint in the list. Can be used for delayed initialization of endpoints.
"""
for endpoint_name in endpoints:
endpoint = self.root_endpoint.endpoints.get('/' + endpoint_name)
if endpoint is not None:
setattr(endpoint, attr_name, attr_value)
elif not skip_missing:
raise KeyError(f'Endpoint not found: /{endpoint_name}')

async def start(self):
"""
Starts the HTTP API with the listen port as specified in the session configuration.
Expand Down

0 comments on commit e273b5c

Please sign in to comment.