Files
mtusi2026lab2/scripts/restart.cmd
T
2026-10-23 17:24:27 +03:00

76 lines
2.6 KiB
Batchfile

@echo off
rem Windows-native twin of scripts/restart.sh, same docker compose subcommands.
rem
rem Why it exists: tests/resilience.py launches the hook with Popen([script]) and
rem no shell. On native Windows that only works for .exe/.cmd/.bat - a .sh file
rem fails with WinError 193 (%1 is not a valid Win32 application). Linux and WSL
rem hosts use scripts/restart.sh instead; see REPORT.md.
rem
rem The hook restarts the API and every state store it depends on, keeping the
rem schema and the data: no volumes removed, no flush, no re-seed. Sessions live
rem in Valkey with AOF, so the same token keeps working after the restart.
rem Usage: scripts\restart.cmd
setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0.."
rem Docker CLI finds its plugins in %ProgramW6432%\Docker\cli-plugins. The
rem environment `make` builds under MSYS drops that variable, and then every
rem `docker compose` call is answered with "unknown command: docker compose".
rem Restoring it keeps the hook working whichever shell started the test.
if not defined PROGRAMW6432 for %%d in ("%SystemDrive%\Program Files") do (
if exist "%%~d\Docker\cli-plugins" set "PROGRAMW6432=%%~d"
)
where docker >nul 2>&1
if errorlevel 1 (
echo docker is required 1>&2
exit /b 2
)
echo === services before restart ===
docker compose ps
if errorlevel 1 exit /b 1
echo === restart api postgres valkey (grace 10s) ===
docker compose restart --timeout 10 api postgres valkey
if errorlevel 1 (
rem One retry: a transient daemon error here leaves the stack in an unknown
rem state, and repeating the operator action is the natural next step.
echo restart attempt failed, retrying once 1>&2
ping -n 4 127.0.0.1 >nul
docker compose restart --timeout 10 api postgres valkey
if errorlevel 1 exit /b 1
)
echo === services after restart ===
docker compose ps
if errorlevel 1 exit /b 1
rem Every service must be reported running; order is not assumed, and the check
rem is retried because docker needs a moment to publish the new container state.
rem `timeout` is avoided on purpose: it fails without a console, and the caller
rem runs this hook with redirected output.
set "remaining=30"
:verify
set "missing="
for %%r in (api postgres valkey) do (
set "found="
for /f "delims=" %%s in ('docker compose ps --status running --services') do (
if /i "%%s"=="%%r" set "found=1"
)
if not defined found set "missing=!missing! %%r"
)
if not defined missing goto verified
if "!remaining!"=="0" goto still_down
set /a remaining-=1
ping -n 2 127.0.0.1 >nul
goto verify
:still_down
echo services not running after 30s:!missing! 1>&2
exit /b 1
:verified
echo restart complete
exit /b 0