From c5ec98a52733ef95860642f9aa92e25fbab618fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Tue, 17 Dec 2024 01:21:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 6 +++--- Dockerfile | 1 + xiaomusic/httpserver.py | 20 +++++++++++++++++--- xiaomusic/utils.py | 27 ++++++++++++++------------- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 90ead08..f97aeaa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -76,7 +76,7 @@ jobs: - name: Package /app (lite) for amd64 run: | docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-amd64 bash -c \ - "cd /app && tar --exclude='ffmpeg' -czf /workspace/app-amd64-lite.tar.gz *" + "cd /app && tar --exclude='ffmpeg' -czf /workspace/app-amd64-lite.tar.gz .[!.]* *" - name: Package /app for arm64 run: | @@ -85,7 +85,7 @@ jobs: - name: Package /app (lite) for arm64 run: | docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-arm64 bash -c \ - "cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm64-lite.tar.gz *" + "cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm64-lite.tar.gz .[!.]* *" - name: Package /app for arm/v7 run: | @@ -94,7 +94,7 @@ jobs: - name: Package /app (lite) for arm/v7 run: | docker run --rm -v $PWD:/workspace ${TEST_TAG}-linux-arm-v7 bash -c \ - "cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm-v7-lite.tar.gz *" + "cd /app && tar --exclude='ffmpeg' -czf /workspace/app-arm-v7-lite.tar.gz .[!.]* *" # This will only push the previously built images. diff --git a/Dockerfile b/Dockerfile index e168122..abb157d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,7 @@ COPY --from=builder /app/.venv ./.venv COPY --from=builder /app/xiaomusic/ ./xiaomusic/ COPY --from=builder /app/plugins/ ./plugins/ COPY --from=builder /app/xiaomusic.py . +COPY --from=builder /app/xiaomusic/__init__.py /base_version.py RUN touch /app/.dockerenv RUN mkdir -p /etc/supervisor/conf.d diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index 9ae7ac6..12f65d3 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -45,6 +45,7 @@ from xiaomusic.utils import ( is_mp3, remove_common_prefix, remove_id3_tags, + restart_xiaomusic, try_add_access_control_param, update_version, ) @@ -584,9 +585,22 @@ async def playlistdelmusic(data: PlayListMusicObj, Verifcation=Depends(verificat # 更新版本 @app.post("/updateversion") -async def updateversion(version: str = "", Verifcation=Depends(verification)): - ret = update_version(version) - return {"ret": ret} +async def updateversion( + version: str = "", lite: bool = True, Verifcation=Depends(verification) +): + ret = update_version(version, lite) + if ret != "OK": + return {"ret": ret} + + proc = restart_xiaomusic() + + async def check_proc(): + # 等待子进程完成 + exit_code = await proc.wait() + log.info(f"Restart completed with exit code {exit_code}") + + asyncio.create_task(check_proc()) + return {"ret": "OK"} async def file_iterator(file_path, start, end): diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index 1f07cd3..66f1671 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -1018,18 +1018,21 @@ def is_docker(): return os.path.exists("/app/.dockerenv") -def _restart_xiaomusic(): - try: - # 重启 xiaomusic 程序 - subprocess.run(["supervisorctl", "restart", "xiaomusic"], check=True) - log.info("xiaomusic 重启成功") - except subprocess.CalledProcessError as e: - log.info(f"xiaomusic 重启失败: {e}") - return False, "xiaomusic 重启失败" - return True +async def restart_xiaomusic(): + # 重启 xiaomusic 程序 + sbp_args = ( + "supervisorctl", + "restart", + "xiaomusic", + ) + + cmd = " ".join(sbp_args) + log.info(f"restart_xiaomusic: {cmd}") + proc = await asyncio.create_subprocess_exec(*sbp_args) + return proc -async def update_version(version: str, lite: bool): +async def update_version(version: str, lite: bool = True): if not is_docker(): ret = "xiaomusic 更新只能在 docker 中进行" log.info(ret) @@ -1046,9 +1049,7 @@ async def update_version(version: str, lite: bool): target_directory = "/app" await download_and_extract(url, target_directory) - ok, ret = _restart_xiaomusic() - if not ok: - return ret + return "OK" def get_os_architecture():