Phase 8: widgets and polish

- Live job card widget (progress bar, in-widget cancel via widget_query)
  and live invoice widget (QR drawn from a bitmap, countdown, paid state),
  Bot API 10.4 sendWidget, private chats only
- Automatic fallback to the ordinary message when sendWidget fails, with
  a breaker that pauses widgets after repeated failures or on servers
  without the method; TELEGRAM_WIDGETS
- Prometheus metrics on the core (/metrics) and on every worker: queue
  depth and wait, slots, heartbeats, job outcomes, errors per site,
  deliveries, cache, invoices, Lightning, ledger check, widgets
- Example Prometheus config and alert rules, Caddyfile, dashboard notes
- Load test: cmd/loadtest and deploy/loadtest (make loadtest)
- Browser check of the widgets' HTML, including decoding a drawn QR
- docs/DEPLOY.md (production in under an hour) and docs/MONITORING.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 18:24:36 +00:00
co-authored by Claude Opus 5.5
parent f1f9c6156c
commit 3c153af97f
45 changed files with 3158 additions and 20 deletions
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# A yt-dlp stand-in for load tests. It never touches the network. Every URL
# is its own video (the ID is a checksum of the URL), so the cache only
# helps where the load test asks for the same link twice.
#
# LOAD_SIZE bytes per file (default 2000000)
# LOAD_TIME seconds per download (default 2)
# LOAD_PROBE seconds per lookup (default 0.2)
set -u
args=("$@")
dir=""
probe=0
url="${args[$((${#args[@]} - 1))]}"
for ((i = 0; i < ${#args[@]}; i++)); do
case "${args[$i]}" in
-P) dir="${args[$((i + 1))]}" ;;
-J) probe=1 ;;
--version) echo "2026.10.26"; exit 0 ;;
-U|--update) echo "yt-dlp is up to date"; exit 0 ;;
esac
done
id=$(printf '%s' "$url" | cksum | cut -d' ' -f1)
size=${LOAD_SIZE:-2000000}
if [[ $probe -eq 1 ]]; then
sleep "${LOAD_PROBE:-0.2}"
echo '{"id": "v'"$id"'", "title": "Load test '"$id"'", "extractor": "loadtest", "extractor_key": "Loadtest", "webpage_url": "'"$url"'", "duration": 60, "uploader": "Load", "is_live": false, "formats": [{"format_id": "a", "ext": "m4a", "vcodec": "none", "acodec": "mp4a.40.2", "filesize": '"$((size / 4))"'}, {"format_id": "v", "ext": "mp4", "vcodec": "avc1", "acodec": "mp4a.40.2", "height": 720, "width": 1280, "filesize": '"$size"'}]}'
exit 0
fi
echo 'INFO|{"id": "v'"$id"'", "title": "Load test '"$id"'", "duration": 60, "extractor": "loadtest", "extractor_key": "Loadtest", "uploader": "Load", "is_live": false}'
echo "[download] Destination: $dir/media.mp4"
steps=4
for ((s = 1; s <= steps; s++)); do
sleep "$(awk "BEGIN{print ${LOAD_TIME:-2}/$steps}")"
echo "DLP|downloading|$((size * s / steps))|$size|NA"
done
echo "DLP|finished|$size|$size|NA"
head -c "$size" /dev/zero > "$dir/media.mp4"
echo 'RESULT|{"width": 1280, "height": 720, "duration": 60, "ext": "mp4", "filepath": "'"$dir/media.mp4"'"}'
+102
View File
@@ -0,0 +1,102 @@
#!/usr/bin/env bash
# Load test: many simulated users against a real core and real workers,
# with Telegram replaced by cmd/faketelegram and yt-dlp by a stand-in that
# takes a set time per download and never touches the network. What it
# measures is the bot itself: the queue, the workers, the cache, delivery.
#
# deploy/loadtest/run.sh # 50 users, embedded worker
# USERS=200 LINKS=5 WORKERS=4 deploy/loadtest/run.sh
#
# Settings (environment):
# USERS (50), LINKS (3), RAMP (20s), SHARE (0.2) the load
# WORKERS (0) remote workers; 0 uses the core's embedded worker
# SLOTS (4) downloads at once, per worker
# LOAD_TIME (2) seconds one download takes
# LOAD_SIZE (2000000) bytes per file
# DB (sqlite) or postgres, with PG_DSN set
# KEEP=1 keep logs and the database afterwards
#
# Needs: go, curl. Prints a summary, and the core's /metrics for the queue.
set -euo pipefail
ROOT=$(cd "$(dirname "$0")/../.." && pwd)
WORK=$(mktemp -d "${TMPDIR:-/tmp}/dlbot-load.XXXXXX")
FAKE_PORT=${FAKE_PORT:-18190}
CORE_PORT=${CORE_PORT:-18180}
USERS=${USERS:-50}
LINKS=${LINKS:-3}
RAMP=${RAMP:-20s}
SHARE=${SHARE:-0.2}
WORKERS=${WORKERS:-0}
SLOTS=${SLOTS:-4}
FAKE="http://127.0.0.1:$FAKE_PORT"
CORE="http://127.0.0.1:$CORE_PORT"
PIDS=()
cleanup() {
for pid in "${PIDS[@]:-}"; do kill "$pid" 2>/dev/null || true; done
wait 2>/dev/null || true
if [ -n "${KEEP:-}" ]; then echo "logs kept in $WORK"; else rm -rf "$WORK"; fi
}
trap cleanup EXIT
echo "==> building"
(cd "$ROOT" && go build -o "$WORK/bin/" ./cmd/core ./cmd/worker ./cmd/faketelegram ./cmd/loadtest)
db_env=(DB_DRIVER=sqlite DB_DSN="$WORK/core.db")
if [ "${DB:-sqlite}" = postgres ]; then
: "${PG_DSN:?set PG_DSN to an empty Postgres database}"
db_env=(DB_DRIVER=postgres DB_DSN="$PG_DSN")
fi
embedded=true
tokens=""
if [ "$WORKERS" -gt 0 ]; then
embedded=false
for i in $(seq 1 "$WORKERS"); do
tokens="${tokens:+$tokens,}load-$i:dlw_load_test_token_for_worker_${i}_0000000000"
done
fi
echo "==> starting the fake Telegram and the core ($([ "$WORKERS" -gt 0 ] && echo "$WORKERS remote workers" || echo "embedded worker"), $SLOTS slots each)"
"$WORK/bin/faketelegram" -listen "127.0.0.1:$FAKE_PORT" >"$WORK/fake.log" 2>&1 &
PIDS+=($!)
(cd "$WORK" && exec env -i PATH="$PATH" HOME="$WORK" \
BOT_TOKEN="123456789:FAKEfakeFAKEfakeFAKEfakeFAKEfake-01" TELEGRAM_API_URL="$FAKE" \
HTTP_LISTEN="127.0.0.1:$CORE_PORT" "${db_env[@]}" STORAGE_LOCAL_DIR="$WORK/files" \
WORKER_EMBEDDED=$embedded WORKER_TOKENS="$tokens" WORKER_CONCURRENCY="$SLOTS" WORKER_TEMP_DIR="$WORK/tmp-core" \
YTDLP_PATH="$ROOT/deploy/loadtest/fake-yt-dlp.sh" YTDLP_AUTO_UPDATE=0 PROBE_MODE=$([ "$WORKERS" -gt 0 ] && echo worker || echo local) \
LOAD_TIME="${LOAD_TIME:-2}" LOAD_SIZE="${LOAD_SIZE:-2000000}" \
FREE_DAILY_DOWNLOADS=100000 FREE_DAILY_MB=0 FREE_ACTIVE_JOBS=1 FREE_MAX_SIZE_MB=50 \
RATE_USER_PER_MINUTE=600 RATE_GROUP_PER_MINUTE=600 QUEUE_FREE_MAX_LEN=100000 \
TELEGRAM_UPDATE_WORKERS=32 TELEGRAM_WIDGETS=false TELEGRAM_SET_PROFILE=false \
LOG_FORMAT=text LOG_LEVEL=warn \
"$WORK/bin/core" >"$WORK/core.log" 2>&1) &
PIDS+=($!)
for _ in $(seq 1 100); do curl -fsS "$CORE/healthz" >/dev/null 2>&1 && break; sleep 0.3; done
curl -fsS "$CORE/healthz" >/dev/null || { echo "the core did not start:"; tail -20 "$WORK/core.log"; exit 1; }
for i in $(seq 1 "$WORKERS"); do
(cd "$WORK" && exec env -i PATH="$PATH" HOME="$WORK" \
CORE_URL="$CORE" WORKER_TOKEN="dlw_load_test_token_for_worker_${i}_0000000000" WORKER_CONCURRENCY="$SLOTS" \
WORKER_TEMP_DIR="$WORK/tmp-$i" WORKER_HEALTH_LISTEN=off YTDLP_PATH="$ROOT/deploy/loadtest/fake-yt-dlp.sh" \
YTDLP_AUTO_UPDATE=0 LOAD_TIME="${LOAD_TIME:-2}" LOAD_SIZE="${LOAD_SIZE:-2000000}" LOG_FORMAT=text LOG_LEVEL=warn \
"$WORK/bin/worker" >"$WORK/worker-$i.log" 2>&1) &
PIDS+=($!)
done
echo "==> $USERS users, $LINKS links each, started over $RAMP, $(awk "BEGIN{print $SHARE*100}")% for one popular link"
status=0
"$WORK/bin/loadtest" -fake "$FAKE" -users "$USERS" -links "$LINKS" -ramp "$RAMP" -share "$SHARE" || status=$?
echo
echo "==> core metrics"
curl -fsS "$CORE/metrics" | grep -E '^dlbot_(queue_wait_seconds_(sum|count)|jobs_finished_total|cache_hits_total|cache_misses_total|deliveries_total|download_errors_total|worker_heartbeat_age_seconds)' || true
if grep -q 'level=ERROR' "$WORK/core.log"; then
echo
echo "==> errors in the core log"
grep 'level=ERROR' "$WORK/core.log" | head -20
status=1
fi
exit $status