[Audio] Say no to busylooping :Awesome: (#3176)

* Say no to busylooping :Awesome:

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* chrore

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* black y u do dis 2 me

Signed-off-by: Drapersniper <27962761+drapersniper@users.noreply.github.com>

* Return regardless if error is raised here
This commit is contained in:
Draper 2019-12-10 01:26:26 +00:00 committed by Michael H
parent 8cba47f382
commit c67b6cd443
3 changed files with 34 additions and 5 deletions

View File

@ -0,0 +1 @@
Added an early exist to the `while` loop in the autoplay method, this is to that if a service is blacklisted it doesn't infinitely loop causing heartbeats.

View File

@ -33,7 +33,7 @@ from redbot.core import Config, commands
from redbot.core.bot import Red from redbot.core.bot import Red
from redbot.core.i18n import Translator, cog_i18n from redbot.core.i18n import Translator, cog_i18n
from . import audio_dataclasses from . import audio_dataclasses
from .errors import InvalidTableError, SpotifyFetchError, YouTubeApiError from .errors import InvalidTableError, SpotifyFetchError, YouTubeApiError, DatabaseError
from .playlists import get_playlist from .playlists import get_playlist
from .utils import CacheLevel, Notifier, is_allowed, queue_duration, track_limit from .utils import CacheLevel, Notifier, is_allowed, queue_duration, track_limit
@ -1098,10 +1098,14 @@ class MusicCache:
track = tracks[0] track = tracks[0]
valid = not multiple valid = not multiple
tries = len(tracks)
while valid is False and multiple: while valid is False and multiple:
tries -= 1
if tries <= 0:
raise DatabaseError("No valid entry found")
track = random.choice(tracks) track = random.choice(tracks)
query = audio_dataclasses.Query.process_input(track) query = audio_dataclasses.Query.process_input(track)
await asyncio.sleep(0.001)
if not query.valid: if not query.valid:
continue continue
if query.is_local and not query.track.exists(): if query.is_local and not query.track.exists():

View File

@ -39,7 +39,13 @@ from .apis import MusicCache, HAS_SQL, _ERROR
from .checks import can_have_caching from .checks import can_have_caching
from .converters import ComplexScopeParser, ScopeParser, get_lazy_converter, get_playlist_converter from .converters import ComplexScopeParser, ScopeParser, get_lazy_converter, get_playlist_converter
from .equalizer import Equalizer from .equalizer import Equalizer
from .errors import LavalinkDownloadFailed, MissingGuild, SpotifyFetchError, TooManyMatches from .errors import (
DatabaseError,
LavalinkDownloadFailed,
MissingGuild,
SpotifyFetchError,
TooManyMatches,
)
from .manager import ServerManager from .manager import ServerManager
from .playlists import ( from .playlists import (
FakePlaylist, FakePlaylist,
@ -448,7 +454,16 @@ class Audio(commands.Cog):
) )
if autoplay and not player.queue and player.fetch("playing_song") is not None: if autoplay and not player.queue and player.fetch("playing_song") is not None:
if self.owns_autoplay is None: if self.owns_autoplay is None:
await self.music_cache.autoplay(player) try:
await self.music_cache.autoplay(player)
except DatabaseError:
notify_channel = player.fetch("channel")
if notify_channel:
notify_channel = self.bot.get_channel(notify_channel)
await self._embed_msg(
notify_channel, _("Autoplay: Couldn't get a valid track.")
)
return
else: else:
self.bot.dispatch( self.bot.dispatch(
"red_audio_should_auto_play", "red_audio_should_auto_play",
@ -2725,7 +2740,16 @@ class Audio(commands.Cog):
if not await self._currency_check(ctx, guild_data["jukebox_price"]): if not await self._currency_check(ctx, guild_data["jukebox_price"]):
return return
if self.owns_autoplay is None: if self.owns_autoplay is None:
await self.music_cache.autoplay(player) try:
await self.music_cache.autoplay(player)
except DatabaseError:
notify_channel = player.fetch("channel")
if notify_channel:
notify_channel = self.bot.get_channel(notify_channel)
await self._embed_msg(
notify_channel, _("Autoplay: Couldn't get a valid track.")
)
return
else: else:
self.bot.dispatch( self.bot.dispatch(
"red_audio_should_auto_play", "red_audio_should_auto_play",