[Audio] Connect to lavalink in the background (#2335)

Also:
- restart and reconnect if connection settings change
  - shutdown and restart if not configured to use external
- show a message in [p]play et al. when the connection hasn't been made
- move the JAR download to manager so audio.py can access it
- only start if no process exists

Resolves #2306
This commit is contained in:
Caleb Johnson
2019-02-15 19:35:21 -05:00
committed by Toby Harradine
parent d13bf37845
commit b633a33137
3 changed files with 108 additions and 46 deletions

View File

@@ -8,6 +8,10 @@ import re
from subprocess import Popen, DEVNULL
from typing import Optional, Tuple
from aiohttp import ClientSession
import redbot.core
_JavaVersion = Tuple[int, int]
log = logging.getLogger("red.audio.manager")
@@ -111,6 +115,10 @@ async def start_lavalink_server(loop):
start_cmd = "java {} -jar {}".format(extra_flags, LAVALINK_JAR_FILE.resolve())
global proc
if proc and proc.poll() is None:
return # already running
proc = Popen(
shlex.split(start_cmd, posix=os.name == "posix"),
cwd=str(LAVALINK_DOWNLOAD_DIR),
@@ -126,11 +134,39 @@ async def start_lavalink_server(loop):
def shutdown_lavalink_server():
log.info("Shutting down lavalink server.")
global shutdown
shutdown = True
global proc
if proc is not None:
log.info("Shutting down lavalink server.")
proc.terminate()
proc.wait()
proc = None
async def download_lavalink(session):
from . import LAVALINK_DOWNLOAD_URL, LAVALINK_JAR_FILE
with LAVALINK_JAR_FILE.open(mode="wb") as f:
async with session.get(LAVALINK_DOWNLOAD_URL) as resp:
while True:
chunk = await resp.content.read(512)
if not chunk:
break
f.write(chunk)
async def maybe_download_lavalink(loop, cog):
from . import LAVALINK_DOWNLOAD_DIR, LAVALINK_JAR_FILE, BUNDLED_APP_YML_FILE, APP_YML_FILE
jar_exists = LAVALINK_JAR_FILE.exists()
current_build = redbot.VersionInfo.from_json(await cog.config.current_version())
if not jar_exists or current_build < redbot.core.version_info:
log.info("Downloading Lavalink.jar")
LAVALINK_DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)
async with ClientSession(loop=loop) as session:
await download_lavalink(session)
await cog.config.current_version.set(redbot.core.version_info.to_json())
shutil.copyfile(str(BUNDLED_APP_YML_FILE), str(APP_YML_FILE))