From 492f68e0f8e1498f7121b9ba890b0cd12ad498ba Mon Sep 17 00:00:00 2001 From: ksyasuda Date: Thu, 5 Sep 2024 04:34:00 -0700 Subject: [PATCH 1/4] Change to gunicorn and update variable names --- Dockerfile | 13 ++++++------- env.example | 6 +++--- mpv-youtube-queue-server.service | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9f185c3..7d9a4b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,16 +2,14 @@ FROM python:3.10-slim # Set environment variables for the MPV socket and server host/port -ENV MPV_SOCKET="/tmp/mpvsocket" \ - HOST_NAME="0.0.0.0" \ - PORT_NUMBER=8080 \ - LOGLEVEL="info" +ENV LISTEN_ADDRESS="0.0.0.0" \ + LISTEN_PORT=8080 # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app -COPY server.py /app/server.py +COPY server.py requirements.txt /app/ # Install any needed packages specified in requirements.txt # If there are no external dependencies, you can skip this step @@ -20,7 +18,8 @@ COPY server.py /app/server.py # Make port 8080 available to the world outside this container EXPOSE "${PORT_NUMBER}" -RUN pip3 install --no-cache-dir Flask mysql-connector-python +RUN pip3 install --no-cache-dir -r requirements.txt # Run server.py when the container launches -CMD ["python3", "server.py", "--host", "${HOST_NAME}", "--port", "${PORT_NUMBER}", "--input-ipc-server", "${MPV_SOCKET}"] +# CMD ["python3", "server.py", "--host", "${LISTEN_ADDRESS}", "--port", "${LISTEN_PORT}", "--input-ipc-server", "${MPV_SOCKET}"] +CMD gunicorn --bind "${LISTEN_ADDRESS}":"${LISTEN_PORT}" server:app diff --git a/env.example b/env.example index 4bab203..d09d758 100644 --- a/env.example +++ b/env.example @@ -1,7 +1,6 @@ -IP=0.0.0.0 # Lisen on all interfaces -PORT_NUMBER=8080 # Internal port number +LISTEN_ADDRESS=0.0.0.0 # Lisen on all interfaces +LISTEN_PORT=8080 # Internal port number MPV_SOCKET=/tmp/mpvsocket # Path to mpv socket -LOGLEVEL=info # MySQL connection info MYSQL_HOST=localhost @@ -10,3 +9,4 @@ MYSQL_PASSWORD=SecretPassword MYSQL_DATABASE=mpv MYSQL_PORT=3306 +LOGLEVEL=info diff --git a/mpv-youtube-queue-server.service b/mpv-youtube-queue-server.service index 99da8b9..4df11ad 100644 --- a/mpv-youtube-queue-server.service +++ b/mpv-youtube-queue-server.service @@ -8,8 +8,8 @@ WorkingDirectory= ExecStart= Restart=on-failure Environment="MPV_SOCKET=/tmp/mpvsocket" -Environment="HOST_NAME=0.0.0.0" -Environment="PORT_NUMBER=42069" +Environment="LISTEN_ADDRESS=0.0.0.0" +Environment="LISTEN_PORT=42069" Environment="MYSQL_HOST=http://localhost" Environment="MYSQL_USER=mpvuser" Environment="MYSQL_PASSWORD=SecretPassword" -- 2.45.2 From ba3f916dcf400738b266555f4b1c27d0e3de9672 Mon Sep 17 00:00:00 2001 From: ksyasuda Date: Thu, 5 Sep 2024 04:34:08 -0700 Subject: [PATCH 2/4] update requirements --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 9705fea..3f0fa14 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,10 @@ blinker==1.8.2 click==8.1.7 Flask==3.0.3 +gunicorn==23.0.0 itsdangerous==2.2.0 Jinja2==3.1.4 MarkupSafe==2.1.5 mysql-connector-python==9.0.0 +packaging==24.1 Werkzeug==3.0.4 -- 2.45.2 From 8a5fba0f31ecc0ff1215aea519578e7ef838dc47 Mon Sep 17 00:00:00 2001 From: ksyasuda Date: Thu, 5 Sep 2024 04:34:23 -0700 Subject: [PATCH 3/4] update logging and set watch date on server side --- server.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/server.py b/server.py index 0b89889..0c32f9e 100755 --- a/server.py +++ b/server.py @@ -15,6 +15,17 @@ logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" ) +# Ensure Flask doesn't duplicate log messages +log = logging.getLogger("werkzeug") +log.setLevel(logging.ERROR) + +# Flask app +app = Flask(__name__) + +# Flask logging configuration to use the same logger as the rest of the app +app.logger.handlers = logging.getLogger().handlers +app.logger.setLevel(logging.getLogger().level) + SOCKET_RETRY_DELAY = 5 # Time in seconds between retries to connect to the socket MAX_RETRIES = 10 # Maximum number of retries to connect to the socket @@ -29,6 +40,7 @@ MYSQL_USER: str = os.getenv("MYSQL_USER", "your_username") MYSQL_PASSWORD: str = os.getenv("MYSQL_PASSWORD", "your_password") MYSQL_PORT: int = int(os.getenv("MYSQL_PORT", "3306")) LOGLEVEL = os.getenv("LOGLEVEL", "INFO").strip().upper() + if LOGLEVEL == "DEBUG": logging.getLogger().setLevel(logging.DEBUG) elif LOGLEVEL == "WARNING": @@ -39,9 +51,6 @@ else: logging.getLogger().setLevel(logging.INFO) -app = Flask(__name__) - - def get_mysql_connection(): """Get a MySQL database connection.""" try: @@ -120,10 +129,11 @@ def add_video(): video_name: str = data.get("video_name") channel_url: str = data.get("channel_url") channel_name: str = data.get("channel_name") - watch_date: date = data.get("watch_date") + watch_date: date = date.today().strftime("%Y-%m-%d") if video_url and video_name and channel_url and channel_name and watch_date: - logging.info(f"Received data: {data}") + logging.debug(f"Received data: {data}") + logging.debug(f"Watch date: {watch_date}") # Insert the data into the MySQL database connection = get_mysql_connection() -- 2.45.2 From 959a1d01d69d07f5109a52ce17ef9f0756885c75 Mon Sep 17 00:00:00 2001 From: ksyasuda Date: Thu, 5 Sep 2024 04:34:39 -0700 Subject: [PATCH 4/4] bump version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 8acdd82..4e379d2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.1 +0.0.2 -- 2.45.2