Fix the audit's concurrency, stale state and delivery findings
Some checks failed
Validate and deploy Sokko G / validate (push) Failing after 19m40s
Validate and deploy Sokko G / deploy (push) Has been skipped

Share uploads now reserve their slot, bytes and per-IP count before the
disk check, so simultaneous uploads can no longer exceed the limits.
Writes to a share's metadata run one after another, and a revocation can
no longer be undone by a pending read count.

The TCG page ignores a state reload that started before or during a
mutation, the last mutation wins, and returning to the page triggers a
single reload. The card cache confirms writes only once the IndexedDB
transaction completes and shows only the current player's copy. The
administration panel loads on demand, which shrinks the TCG chunk from
311.6 to 191.8 kB.

A new /api/health route answers 503 when the cards or shares API failed
to start. The deploy script ships the commit's production dependencies
with the build, waits up to 30 s for a healthy server instead of trusting
systemctl is-active, and restores node_modules on rollback. Both Forgejo
workflows check the route on the public site. The updated script applies
once reinstalled on the server.

Unused helpers, the ignored join role and duplicate button selectors are
removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-20 21:38:12 +01:00
parent 0571a935d6
commit 067a4a8fd6
20 changed files with 391 additions and 73 deletions

View file

@ -10,10 +10,14 @@ readonly NPM_BIN="$NODE_HOME/bin/npm"
readonly SERVICE_NAME="sokko-g"
readonly LOCK_FILE="/run/lock/sokko-g-deploy.lock"
readonly STATE_DIR="/var/lib/sokko-g"
readonly SERVICE_ENV_FILE="/etc/sokko-g/cards.env"
# Délai laissé au service pour répondre sur /api/health après son redémarrage.
readonly HEALTH_TIMEOUT_SECONDS=30
build_root=""
worktree=""
backup_dist=""
backup_node_modules=""
usage() {
echo "Usage: deploy-sokko-g --commit <full-sha>" >&2
@ -46,10 +50,50 @@ restore_previous_release() {
if [[ -n "$backup_dist" && -d "$backup_dist" ]]; then
as_app_user mv "$backup_dist" "$APP_ROOT/website/dist"
fi
# Sans copie précédente (première livraison de ce type), les nouvelles dépendances restent : l'ancien code n'a rien d'autre.
if [[ -n "$backup_node_modules" && -d "$backup_node_modules" ]]; then
as_app_user rm -rf -- "$APP_ROOT/node_modules"
as_app_user mv "$backup_node_modules" "$APP_ROOT/node_modules"
fi
as_app_user git -C "$APP_ROOT" checkout --detach "$previous_commit"
systemctl restart "$SERVICE_NAME" || true
}
# Valeur d'une variable telle que le service la voit : l'environnement systemd l'emporte sur le .env du site.
service_env_value() {
local name=$1
local file value=""
for file in "$APP_ROOT/.env" "$SERVICE_ENV_FILE"; do
[[ -r "$file" ]] || continue
value="$(sed -n "s/^[[:space:]]*$name[[:space:]]*=[[:space:]]*//p" "$file" | tail -n 1 | sed -e 's/^["'"'"']//' -e 's/["'"'"'][[:space:]]*$//' -e 's/[[:space:]]*$//')" || true
done
printf '%s' "$value"
}
# `systemctl is-active` répond dès le lancement du processus : seul /api/health prouve que le serveur a chargé son code
# et ses dépendances, et que chaque API demandée a démarré.
wait_until_healthy() {
local host port url deadline body
host="$(service_env_value HOST)"
port="$(service_env_value PORT)"
[[ -z "$host" || "$host" == "0.0.0.0" || "$host" == "::" ]] && host="127.0.0.1"
url="http://$host:${port:-5173}/api/health"
deadline=$((SECONDS + HEALTH_TIMEOUT_SECONDS))
while (( SECONDS < deadline )); do
# Le contenu compte : sans cette route, le serveur répondrait par la page de l'application.
if body="$(curl --fail --silent --max-time 3 "$url" 2>/dev/null)" && [[ $body == *'"ok":true'* ]]; then
systemctl is-active --quiet "$SERVICE_NAME"
return
fi
sleep 1
done
echo "$url did not answer successfully within ${HEALTH_TIMEOUT_SECONDS}s." >&2
curl --silent --max-time 3 "$url" >&2 || true
return 1
}
write_state() {
local name=$1
local value=$2
@ -70,6 +114,7 @@ readonly requested_commit=$2
[[ $requested_commit =~ ^[0-9a-f]{40}$ ]] || usage
command -v flock >/dev/null
command -v curl >/dev/null
[[ -x "$NODE_BIN" && -x "$NPM_BIN" ]] || {
echo "Node 24.11.1 and npm 11.17.0 must be installed under $NODE_HOME." >&2
exit 1
@ -110,15 +155,26 @@ as_app_user env \
PATH="$NODE_HOME/bin:/usr/bin:/bin" \
"$NPM_BIN" --prefix "$worktree" run build
[[ -f "$worktree/website/dist/index.html" ]]
# Le service utilisera exactement les dépendances installées par npm ci pour ce commit, sans les outils de développement.
as_app_user env \
PATH="$NODE_HOME/bin:/usr/bin:/bin" \
"$NPM_BIN" --prefix "$worktree" prune --omit=dev
[[ -d "$worktree/node_modules" ]]
# Le worktree est sur le même système de fichiers que le site : chaque remplacement est un simple renommage.
as_app_user git -C "$APP_ROOT" checkout --detach "$requested_commit"
backup_dist="$build_root/previous-dist"
if [[ -d "$APP_ROOT/website/dist" ]]; then
as_app_user mv "$APP_ROOT/website/dist" "$backup_dist"
fi
as_app_user mv "$worktree/website/dist" "$APP_ROOT/website/dist"
backup_node_modules="$build_root/previous-node_modules"
if [[ -d "$APP_ROOT/node_modules" ]]; then
as_app_user mv "$APP_ROOT/node_modules" "$backup_node_modules"
fi
as_app_user mv "$worktree/node_modules" "$APP_ROOT/node_modules"
if ! systemctl restart "$SERVICE_NAME" || ! systemctl is-active --quiet "$SERVICE_NAME"; then
if ! systemctl restart "$SERVICE_NAME" || ! wait_until_healthy; then
echo "The new release did not start; restoring $previous_commit." >&2
restore_previous_release "$previous_commit"
exit 1