"""Send commands to mpv via its UNIX socket.""" import socket import time from flask import current_app from app.config import Config SOCKET_RETRY_DELAY = 5 MAX_RETRIES = 10 def send_to_mpv(command): attempts = 0 while attempts < MAX_RETRIES: try: with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket: client_socket.connect(Config.MPV_SOCKET) client_socket.sendall(command.encode("utf-8")) current_app.logger.info("Command sent to mpv successfully.") return True except socket.error as e: attempts += 1 current_app.logger.error( f"Failed to connect to socket (attempt {attempts}/{MAX_RETRIES}): {e}. Retrying in {SOCKET_RETRY_DELAY} seconds..." ) time.sleep(SOCKET_RETRY_DELAY) current_app.logger.error( f"Exceeded maximum retries ({MAX_RETRIES}). Ignoring the request." ) return False