Fixing error when stop and return too fast, because of that there may be some threads downloading songs that will try to put songs in a already closed queue

This commit is contained in:
Rafael Vargas
2023-01-25 13:07:39 -03:00
parent 8dfa3579ae
commit afb223eadd
12 changed files with 51 additions and 15 deletions

View File

@@ -1,4 +1,6 @@
from abc import ABC, abstractmethod
from Parallelism.Commands import VCommands
from multiprocessing import Queue
from typing import List, Union
from discord.ext.commands import Context
from discord import Client, Guild, ClientUser, Interaction, Member, User
@@ -27,6 +29,12 @@ class AbstractHandler(ABC):
else:
self.__author = ctx.user
def putCommandInQueue(self, queue: Queue, command: VCommands) -> None:
try:
queue.put(command)
except Exception as e:
print(f'[ERROR PUTTING COMMAND IN QUEUE] -> {e}')
@abstractmethod
async def run(self) -> HandlerResponse:
pass

View File

@@ -50,7 +50,7 @@ class JumpMusicHandler(AbstractHandler):
# Send a command to the player to skip the music
command = VCommands(VCommandsType.SKIP, None)
queue = processInfo.getQueueToPlayer()
queue.put(command)
self.putCommandInQueue(queue, command)
processLock.release()
return HandlerResponse(self.ctx)

View File

@@ -23,7 +23,7 @@ class PauseHandler(AbstractHandler):
# Send Pause command to be execute by player process
command = VCommands(VCommandsType.PAUSE, None)
queue = processInfo.getQueueToPlayer()
queue.put(command)
self.putCommandInQueue(queue, command)
embed = self.embeds.PLAYER_PAUSED()
return HandlerResponse(self.ctx, embed)

View File

@@ -75,7 +75,8 @@ class PlayHandler(AbstractHandler):
processLock.release()
queue = processInfo.getQueueToPlayer()
playCommand = VCommands(VCommandsType.PLAY, None)
queue.put(playCommand)
self.putCommandInQueue(queue, playCommand)
else:
processManager.resetProcess(self.guild, self.ctx)
embed = self.embeds.PLAYER_RESTARTED()
@@ -135,7 +136,7 @@ class PlayHandler(AbstractHandler):
acquired = processLock.acquire(timeout=self.config.ACQUIRE_LOCK_TIMEOUT)
if acquired:
playlist.add_song(song)
queue.put(playCommand)
self.putCommandInQueue(queue, playCommand)
processLock.release()
else:
processManager.resetProcess(self.guild, self.ctx)

View File

@@ -44,7 +44,7 @@ class PrevHandler(AbstractHandler):
# Send a prev command, together with the user voice channel
prevCommand = VCommands(VCommandsType.PREV, self.author.voice.channel.id)
queue = processInfo.getQueueToPlayer()
queue.put(prevCommand)
self.putCommandInQueue(queue, prevCommand)
embed = self.embeds.RETURNING_SONG()
return HandlerResponse(self.ctx, embed)

View File

@@ -23,7 +23,7 @@ class ResetHandler(AbstractHandler):
command = VCommands(VCommandsType.RESET, None)
queue = processInfo.getQueueToPlayer()
queue.put(command)
self.putCommandInQueue(queue, command)
return HandlerResponse(self.ctx)
else:

View File

@@ -23,7 +23,7 @@ class ResumeHandler(AbstractHandler):
# Send Resume command to be execute by player process
command = VCommands(VCommandsType.RESUME, None)
queue = processInfo.getQueueToPlayer()
queue.put(command)
self.putCommandInQueue(queue, command)
embed = self.embeds.PLAYER_RESUMED()
return HandlerResponse(self.ctx, embed)

View File

@@ -29,7 +29,7 @@ class SkipHandler(AbstractHandler):
# Send a command to the player process to skip the music
command = VCommands(VCommandsType.SKIP, None)
queue = processInfo.getQueueToPlayer()
queue.put(command)
self.putCommandInQueue(queue, command)
embed = self.embeds.SKIPPING_SONG()
return HandlerResponse(self.ctx, embed)

View File

@@ -23,7 +23,7 @@ class StopHandler(AbstractHandler):
# Send command to player process stop
command = VCommands(VCommandsType.STOP, None)
queue = processInfo.getQueueToPlayer()
queue.put(command)
self.putCommandInQueue(queue, command)
embed = self.embeds.STOPPING_PLAYER()
return HandlerResponse(self.ctx, embed)