38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
import subprocess
|
|
import os
|
|
import sys
|
|
from fastapi import APIRouter, HTTPException, BackgroundTasks
|
|
import logging
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def restart_server():
|
|
"""Restarts the server by killing the current process."""
|
|
logger.info("Restarting server...")
|
|
# This works in Docker if a restart policy is set (e.g., restart: always)
|
|
os.kill(os.getpid(), 15) # SIGTERM
|
|
|
|
@router.post("/update-ytdlp")
|
|
async def update_ytdlp(background_tasks: BackgroundTasks):
|
|
try:
|
|
# Run pip install to upgrade yt-dlp to master
|
|
logger.info("Starting yt-dlp update...")
|
|
process = subprocess.run(
|
|
[sys.executable, "-m", "pip", "install", "--upgrade", "--force-reinstall", "git+https://github.com/yt-dlp/yt-dlp.git@master"],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True
|
|
)
|
|
logger.info(f"Update Output: {process.stdout}")
|
|
|
|
# Schedule restart after a short delay to allow response to be sent
|
|
background_tasks.add_task(restart_server)
|
|
|
|
return {"status": "success", "message": "yt-dlp updated. Server restarting..."}
|
|
except subprocess.CalledProcessError as e:
|
|
logger.error(f"Update Failed: {e.stderr}")
|
|
raise HTTPException(status_code=500, detail=f"Update failed: {e.stderr}")
|
|
except Exception as e:
|
|
logger.error(f"Unexpected Error: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|