[V3 Audio] Harden Lavalink boot sequence (#1498)

* Do a bit of hardening

* Loop not asyncio

* Don't use new asyncio coolness

* I hate you all

* Muck up everything

* Fix version comparisons
This commit is contained in:
Will
2018-04-15 18:01:56 -04:00
committed by palmtree5
parent d9fa875d84
commit 5be967e8c5
6 changed files with 122 additions and 60 deletions

View File

@@ -1,18 +1,17 @@
from pathlib import Path
from aiohttp import ClientSession
import shutil
import asyncio
from .audio import Audio
from .manager import start_lavalink_server
from discord.ext import commands
from redbot.core.data_manager import cog_data_path
import redbot.core
LAVALINK_BUILD = 3112
LAVALINK_BUILD_URL = (
"https://ci.fredboat.com/repository/download/"
"Lavalink_Build/{}:id/Lavalink.jar?guest=1"
).format(LAVALINK_BUILD)
LAVALINK_DOWNLOAD_URL = (
"https://github.com/Cog-Creators/Red-DiscordBot/"
"releases/download/{}/Lavalink.jar"
).format(redbot.core.__version__)
LAVALINK_DOWNLOAD_DIR = cog_data_path(raw_name="Audio")
LAVALINK_JAR_FILE = LAVALINK_DOWNLOAD_DIR / "Lavalink.jar"
@@ -23,7 +22,7 @@ BUNDLED_APP_YML_FILE = Path(__file__).parent / "application.yml"
async def download_lavalink(session):
with LAVALINK_JAR_FILE.open(mode='wb') as f:
async with session.get(LAVALINK_BUILD_URL) as resp:
async with session.get(LAVALINK_DOWNLOAD_URL) as resp:
while True:
chunk = await resp.content.read(512)
if not chunk:
@@ -33,24 +32,25 @@ async def download_lavalink(session):
async def maybe_download_lavalink(loop, cog):
jar_exists = LAVALINK_JAR_FILE.exists()
current_build = await cog.config.current_build()
current_build = redbot.core.VersionInfo(*await cog.config.current_build())
if not jar_exists or current_build < LAVALINK_BUILD:
session = ClientSession(loop=loop)
if not jar_exists or current_build < redbot.core.version_info:
LAVALINK_DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)
with ClientSession(loop=loop) as session:
await download_lavalink(session)
await cog.config.current_build.set(LAVALINK_BUILD)
await download_lavalink(session)
await cog.config.current_build.set(redbot.core.version_info.to_json())
session.close()
shutil.copyfile(str(BUNDLED_APP_YML_FILE), str(APP_YML_FILE))
async def setup(bot: commands.Bot):
cog = Audio(bot)
await maybe_download_lavalink(bot.loop, cog)
await start_lavalink_server(bot.loop)
if not await cog.config.use_external_lavalink():
await maybe_download_lavalink(bot.loop, cog)
await start_lavalink_server(bot.loop)
async def _finish():
await cog.init_config()
bot.add_cog(cog)
bot.loop.create_task(_finish())
bot.add_cog(cog)
bot.loop.create_task(cog.init_config())