#!/usr/bin/env bash
# Real demos for P172 "How to Fix Linux 'Too Many Open Files' Errors". Made on the Mac (Docker Desktop).
# One machine: a laundromat's payment server, a stock Ubuntu 24.04 with systemd as PID 1 (lab/Dockerfile), its card
# payment app (lab/files/washpay.py, Python's standard library) a systemd service on port 8000. The container is
# started with Ubuntu's usual per-process limit for a login, 1024 open files (hard 524288), the value a real
# Ubuntu install gives; Docker's own default is far higher.
# Every command shown is typed into Jim's real interactive bash in a pty (demo_driver.py).
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p172
cleanup() { docker rm -f $P-laundromat >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t $P -f lab/Dockerfile lab/files >/dev/null
docker run -d --name $P-laundromat --hostname laundromat --privileged --cgroupns=private \
  --ulimit nofile=1024:524288 --tmpfs /run --tmpfs /run/lock $P >/dev/null
for i in $(seq 60); do docker exec $P-laundromat systemctl is-system-running 2>/dev/null | grep -qE 'running|degraded' && break; sleep 1; done
python3 demo_driver.py $P
