mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[RPC] Fix for non-rpc users (#4143)
* [RPC] Fix for non-rpc users - RPC probably needs rewriting. - Also, I noticed some extremely sharp edges and a potential crash point (unrelated to the fixed issue) on windows that the side effects from have been mitigated here partially. * sysexit on initialization failure
This commit is contained in:
parent
bbd08eda3e
commit
3b5183de43
@ -1370,6 +1370,7 @@ class RedBase(
|
||||
await super().logout()
|
||||
await drivers.get_driver_class().teardown()
|
||||
try:
|
||||
if self.rpc_enabled:
|
||||
await self.rpc.close()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
from aiohttp import web
|
||||
@ -68,14 +69,28 @@ class RPC:
|
||||
|
||||
self._runner = web.AppRunner(self.app)
|
||||
self._site: Optional[web.TCPSite] = None
|
||||
self._started = False
|
||||
|
||||
async def initialize(self, port: int):
|
||||
"""
|
||||
Finalizes the initialization of the RPC server and allows it to begin
|
||||
accepting queries.
|
||||
"""
|
||||
await self._runner.setup()
|
||||
self._site = web.TCPSite(self._runner, host="127.0.0.1", port=port, shutdown_timeout=0)
|
||||
try:
|
||||
# This ensures self._started can't be assigned
|
||||
# except with both other functions
|
||||
# and isn't subject to a really really stupid but complex
|
||||
# issue on windows with catching specific
|
||||
# exceptions related to shutdown conditions in asyncio applications.
|
||||
self._started, _discard, self._site = (
|
||||
True,
|
||||
await self._runner.setup(),
|
||||
web.TCPSite(self._runner, host="127.0.0.1", port=port, shutdown_timeout=0),
|
||||
)
|
||||
except Exception as exc:
|
||||
log.exception("RPC setup failure", exc_info=exc)
|
||||
sys.exit(1)
|
||||
else:
|
||||
await self._site.start()
|
||||
log.debug("Created RPC server listener on port %s", port)
|
||||
|
||||
@ -83,6 +98,7 @@ class RPC:
|
||||
"""
|
||||
Closes the RPC server.
|
||||
"""
|
||||
if self._started:
|
||||
await self.app.shutdown()
|
||||
await self._runner.cleanup()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user