Files
hevc-encoder/start_dashboard.bat
2026-06-16 22:25:33 +01:00

204 lines
5.2 KiB
Batchfile

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ROOT_DIR=%~dp0"
set "SERVER_DIR=%ROOT_DIR%server"
set "DASH_DIR=%ROOT_DIR%dashboard"
set "SERVER_PY=%SERVER_DIR%\venv\Scripts\python.exe"
set "DASH_PY=%DASH_DIR%\venv\Scripts\python.exe"
set "CHOICE="
set "WORKER_PID="
call :banner
call :check_deps
if "%~1"=="" (
call :menu
) else (
set "CHOICE=%~1"
call :dispatch
)
exit /b %ERRORLEVEL%
:main
exit /b 0
:banner
echo.
echo HEVC Dashboard Launcher
echo -----------------------
echo Location: %ROOT_DIR%
echo.
exit /b 0
:check_deps
set "MISSING="
for %%C in (python ffmpeg ffprobe) do (
where %%C >nul 2>&1
if errorlevel 1 (
set "MISSING=!MISSING! %%C"
echo %%C - MISSING
) else (
echo %%C - OK
)
)
echo.
if defined MISSING (
echo Missing:%MISSING%
where choco >nul 2>&1
if not errorlevel 1 (
echo Chocolatey found. Install the missing packages manually if needed.
) else (
echo Please install the missing dependencies manually (python, ffmpeg, ffprobe)
)
)
exit /b 0
:menu
echo.
echo Options:
echo 1^) Setup venvs only
echo 2^) Start Worker API only
echo 3^) Start Dashboard only
echo 4^) Start Worker API + Dashboard
echo 5^) Cleanup venvs and pycache
echo 6^) Exit
echo.
set /p "CHOICE=Choose an option [4]: "
if not defined CHOICE set "CHOICE=4"
call :dispatch
exit /b %ERRORLEVEL%
:dispatch
if /I "%CHOICE%"=="--setup" set "CHOICE=1"
if /I "%CHOICE%"=="--worker" set "CHOICE=2"
if /I "%CHOICE%"=="--dashboard" set "CHOICE=3"
if /I "%CHOICE%"=="--cleanup" set "CHOICE=5"
if "%CHOICE%"=="1" (
call :setup_venvs
exit /b %ERRORLEVEL%
)
if "%CHOICE%"=="2" (
call :start_worker
exit /b %ERRORLEVEL%
)
if "%CHOICE%"=="3" (
call :start_dashboard
exit /b %ERRORLEVEL%
)
if "%CHOICE%"=="4" (
call :setup_venvs
if errorlevel 1 exit /b %ERRORLEVEL%
call :start_worker
if errorlevel 1 exit /b %ERRORLEVEL%
call :wait_for_worker
call :start_dashboard
echo.
pause
exit /b %ERRORLEVEL%
)
if "%CHOICE%"=="5" (
call :cleanup_workspace
echo Cleanup complete.
exit /b 0
)
if "%CHOICE%"=="6" (
echo Bye.
exit /b 0
)
echo Invalid choice: %CHOICE%
exit /b 1
:setup_venvs
if not exist "%SERVER_DIR%" mkdir "%SERVER_DIR%"
if not exist "%SERVER_DIR%\venv\Scripts\python.exe" (
echo Creating server venv...
python -m venv "%SERVER_DIR%\venv"
if not exist "%SERVER_DIR%\venv\Scripts\python.exe" (
echo Failed to create server virtual environment.
exit /b 1
)
"%SERVER_DIR%\venv\Scripts\python.exe" -m pip install -q -r "%SERVER_DIR%\requirements.txt"
) else (
echo Server venv already exists. Skipping creation.
)
if not exist "%SERVER_DIR%\venv\Scripts\python.exe" (
echo Failed to create server virtual environment.
exit /b 1
)
if not exist "%DASH_DIR%" mkdir "%DASH_DIR%"
if not exist "%DASH_DIR%\venv\Scripts\python.exe" (
echo Creating dashboard venv...
python -m venv "%DASH_DIR%\venv"
if not exist "%DASH_DIR%\venv\Scripts\python.exe" (
echo Failed to create dashboard virtual environment.
exit /b 1
)
"%DASH_DIR%\venv\Scripts\python.exe" -m pip install -q -r "%DASH_DIR%\requirements.txt"
) else (
echo Dashboard venv already exists. Skipping creation.
)
if not exist "%DASH_DIR%\venv\Scripts\python.exe" (
echo Failed to create dashboard virtual environment.
exit /b 1
)
echo Virtual environments ready.
exit /b 0
:start_worker
if not exist "%SERVER_PY%" (
echo Server venv missing. Run option 1 first.
exit /b 1
)
echo Starting Worker API (detached)...
start "HEVC Worker API" /D "%SERVER_DIR%" cmd /c ""%SERVER_DIR%\venv\Scripts\activate.bat" && python worker_api.py --port 5001 > worker.log 2>&1"
exit /b 0
:wait_for_worker
set "WORKER_READY="
for /L %%I in (1,1,30) do (
powershell -NoProfile -Command "try { $r = Invoke-WebRequest -UseBasicParsing -TimeoutSec 1 http://127.0.0.1:5001/api/status; if ($r.StatusCode -eq 200) { exit 0 } } catch { exit 1 }" >nul 2>&1
if not errorlevel 1 (
set "WORKER_READY=1"
goto :wait_for_worker_done
)
timeout /t 1 /nobreak >nul
)
:wait_for_worker_done
if not defined WORKER_READY (
echo Worker did not confirm readiness, continuing anyway.
)
exit /b 0
:start_dashboard
if not exist "%DASH_PY%" (
echo Dashboard venv missing. Run option 1 first.
exit /b 1
)
set "WORKER_URL=http://127.0.0.1:5001"
start "" "http://localhost:5000"
echo Starting Dashboard (foreground)... Press Ctrl+C to stop.
"%DASH_PY%" "%DASH_DIR%\dashboard.py"
exit /b 0
:cleanup_workspace
echo Stopping launcher-related Python processes...
powershell -NoProfile -Command "Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -match 'worker_api\.py|dashboard\.py' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }"
echo Removing server and dashboard venvs...
if exist "%SERVER_DIR%\venv" rmdir /s /q "%SERVER_DIR%\venv"
if exist "%DASH_DIR%\venv" rmdir /s /q "%DASH_DIR%\venv"
echo Removing __pycache__ folders...
powershell -NoProfile -Command "Get-ChildItem -Path '%SERVER_DIR%','%DASH_DIR%' -Directory -Recurse -Filter '__pycache__' -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
exit /b 0