From e3770adda81074c47a0c556bdb3054f9f2396892 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 30 Sep 2019 19:07:30 +0200 Subject: [PATCH] 5.0a61: Possibly fix a race condition in play? I have no way to test it --- royalnet/__main__.py | 13 +++++++--- royalnet/audio/ytdldiscord.py | 7 ------ royalnet/bots/discord.py | 1 + royalnet/bots/generic.py | 9 ++++++- royalnet/commands/royalgames/play.py | 4 +-- royalnet/commands/royalgames/zawarudo.py | 4 +-- royalnet/network/networkserver.py | 32 +++++++++++++++--------- royalnet/version.py | 2 +- 8 files changed, 43 insertions(+), 29 deletions(-) diff --git a/royalnet/__main__.py b/royalnet/__main__.py index 2fd5281d..cd37a0c4 100644 --- a/royalnet/__main__.py +++ b/royalnet/__main__.py @@ -17,17 +17,20 @@ import keyring help="The names of the command pack modules that should be imported.") @click.option("-n", "--network-address", type=str, default=None, help="The Network server URL to connect to.") -@click.option("--local-network-server/--no-local-network-server", default=True, - help="Locally run a Network server, bind it to port 44444.") +@click.option("-l", "--local-network-server", is_flag=True, default=False, + help="Locally run a Network server and bind it to port 44444. Overrides -n.") @click.option("-s", "--secrets-name", type=str, default="__default__", help="The name in the keyring that the secrets are stored with.") +@click.option("-v", "--verbose", is_flag=True, default=False, + help="Print all possible debug information.") def run(telegram: typing.Optional[bool], discord: typing.Optional[bool], database: typing.Optional[str], command_packs: typing.List[str], network_address: typing.Optional[str], local_network_server: bool, - secrets_name: str): + secrets_name: str, + verbose: bool): # Get the network password network_password = keyring.get_password(f"Royalnet/{secrets_name}", "network") @@ -62,7 +65,7 @@ def run(telegram: typing.Optional[bool], server_process = multiprocessing.Process(name="Network Server", target=r.network.NetworkServer("0.0.0.0", 44444, - network_password).run_blocking(), + network_password).run_blocking, daemon=True) server_process.start() network_address = "ws://127.0.0.1:44444/" @@ -106,6 +109,7 @@ def run(telegram: typing.Optional[bool], secrets_name=secrets_name) telegram_process = multiprocessing.Process(name="Telegram Interface", target=telegram_bot.run_blocking, + args=(verbose,), daemon=True) telegram_process.start() @@ -118,6 +122,7 @@ def run(telegram: typing.Optional[bool], secrets_name=secrets_name) discord_process = multiprocessing.Process(name="Discord Interface", target=discord_bot.run_blocking, + args=(verbose,), daemon=True) discord_process.start() diff --git a/royalnet/audio/ytdldiscord.py b/royalnet/audio/ytdldiscord.py index 1a5bb93f..ba0b48a9 100644 --- a/royalnet/audio/ytdldiscord.py +++ b/royalnet/audio/ytdldiscord.py @@ -63,13 +63,6 @@ class YtdlDiscord: dfiles.append(dfile) return dfiles - @classmethod - def create_and_ready_from_url(cls, url, **ytdl_args) -> typing.List["YtdlDiscord"]: - dfiles = cls.create_from_url(url, **ytdl_args) - for dfile in dfiles: - dfile.ready_up() - return dfiles - @property def info(self) -> typing.Optional[YtdlInfo]: return self.ytdl_file.info diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index ce0d65db..a83431fd 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -217,6 +217,7 @@ class DiscordBot(GenericBot): guild_music_data = self.music_data[guild] for dfile in dfiles: log.debug(f"Adding {dfile} to music_data") + dfile.ready_up() guild_music_data.add(dfile) if guild_music_data.now_playing is None: await self.advance_music_data(guild) diff --git a/royalnet/bots/generic.py b/royalnet/bots/generic.py index 6eca5297..2c6163a2 100644 --- a/royalnet/bots/generic.py +++ b/royalnet/bots/generic.py @@ -190,6 +190,13 @@ class GenericBot: """A blocking coroutine that should make the bot start listening to commands and requests.""" raise NotImplementedError() - def run_blocking(self): + def run_blocking(self, verbose=False): + if verbose: + core_logger = logging.getLogger("royalnet") + core_logger.setLevel(logging.DEBUG) + stream_handler = logging.StreamHandler() + stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{") + core_logger.addHandler(stream_handler) + core_logger.debug("Logging setup complete.") self._initialize() self.loop.run_until_complete(self.run()) diff --git a/royalnet/commands/royalgames/play.py b/royalnet/commands/royalgames/play.py index e397020d..a20d705a 100644 --- a/royalnet/commands/royalgames/play.py +++ b/royalnet/commands/royalgames/play.py @@ -39,9 +39,9 @@ class PlayNH(NetworkHandler): } # Start downloading if data["url"].startswith("http://") or data["url"].startswith("https://"): - dfiles: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, data["url"], **ytdl_args) + dfiles: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_from_url, data["url"], **ytdl_args) else: - dfiles = await asyncify(YtdlDiscord.create_and_ready_from_url, f"ytsearch:{data['url']}", **ytdl_args) + dfiles = await asyncify(YtdlDiscord.create_from_url, f"ytsearch:{data['url']}", **ytdl_args) await bot.add_to_music_data(dfiles, guild) # Create response dictionary response = { diff --git a/royalnet/commands/royalgames/zawarudo.py b/royalnet/commands/royalgames/zawarudo.py index d0941427..3918ab93 100644 --- a/royalnet/commands/royalgames/zawarudo.py +++ b/royalnet/commands/royalgames/zawarudo.py @@ -44,10 +44,10 @@ class ZawarudoNH(NetworkHandler): "outtmpl": f"./downloads/{datetime.datetime.now().timestamp()}_%(title)s.%(ext)s" } # Start downloading - zw_start: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, + zw_start: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_from_url, "https://scaleway.steffo.eu/jojo/zawarudo_intro.mp3", **ytdl_args) - zw_end: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_and_ready_from_url, + zw_end: typing.List[YtdlDiscord] = await asyncify(YtdlDiscord.create_from_url, "https://scaleway.steffo.eu/jojo/zawarudo_outro.mp3", **ytdl_args) old_playlist = bot.music_data[guild] diff --git a/royalnet/network/networkserver.py b/royalnet/network/networkserver.py index 5968a3c0..2dfabdd6 100644 --- a/royalnet/network/networkserver.py +++ b/royalnet/network/networkserver.py @@ -40,10 +40,7 @@ class NetworkServer: self.port: int = port self.required_secret: str = required_secret self.identified_clients: typing.List[ConnectedClient] = [] - if loop is None: - self._loop = asyncio.get_event_loop() - else: - self._loop = loop + self.loop = loop def find_client(self, *, nid: str = None, link_type: str = None) -> typing.List[ConnectedClient]: assert not (nid and link_type) @@ -91,7 +88,7 @@ class NetworkServer: pass # Otherwise, route the package to its destination # noinspection PyAsyncCall - self._loop.create_task(self.route_package(package)) + self.loop.create_task(self.route_package(package)) def find_destination(self, package: Package) -> typing.List[ConnectedClient]: """Find a list of destinations for the package. @@ -129,14 +126,25 @@ class NetworkServer: await destination.send(specific_package) async def serve(self): - await websockets.serve(self.listener, host=self.address, port=self.port) + log.debug(f"Serving on ws://{self.address}:{self.port}") + await websockets.serve(self.listener, host=self.address, port=self.port, loop=self.loop) + log.debug(f"Serve has finished?!") async def run(self): - log.debug(f"Starting main server loop for on ws://{self.address}:{self.port}") + if self.loop is None: + self.loop = asyncio.get_event_loop() + log.debug(f"Starting main server loop on ws://{self.address}:{self.port}") # noinspection PyAsyncCall - self._loop.create_task(self.serve()) - # Just to be sure it has started on Linux - await asyncio.sleep(0.5) + await self.serve() - def run_blocking(self): - self._loop.run_until_complete(self.run()) + def run_blocking(self, verbose=False): + if verbose: + core_logger = _logging.getLogger("royalnet") + core_logger.setLevel(_logging.DEBUG) + stream_handler = _logging.StreamHandler() + stream_handler.formatter = _logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{") + core_logger.addHandler(stream_handler) + core_logger.debug("Logging setup complete.") + if self.loop is None: + self.loop = asyncio.get_event_loop() + self.loop.run_until_complete(self.run()) diff --git a/royalnet/version.py b/royalnet/version.py index 18cacb2f..d9322848 100644 --- a/royalnet/version.py +++ b/royalnet/version.py @@ -1 +1 @@ -semantic = "5.0a60" +semantic = "5.0a61"