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:
co-authored by
Claude Opus 5.5
parent
f1f9c6156c
commit
3c153af97f
@@ -0,0 +1,15 @@
|
||||
# Caddy in front of the core: TLS from Let's Encrypt, then everything to
|
||||
# the core on localhost, except /metrics, which stays private.
|
||||
#
|
||||
# sudo apt install caddy
|
||||
# sudo cp deploy/caddy/Caddyfile /etc/caddy/Caddyfile # set your domain
|
||||
# sudo systemctl reload caddy
|
||||
#
|
||||
# Caddy has no request body limit and no read timeout by default, which
|
||||
# suits worker uploads and the long-held claim requests of workers.
|
||||
bot.example.com {
|
||||
@private path /metrics
|
||||
respond @private 404
|
||||
|
||||
reverse_proxy 127.0.0.1:8080
|
||||
}
|
||||
Executable
+40
@@ -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"'"}'
|
||||
Executable
+102
@@ -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
|
||||
@@ -0,0 +1,75 @@
|
||||
# Alert rules for the bot. Thresholds are starting points; tune them to
|
||||
# your traffic. Written for Prometheus 2.x and 3.x rule files.
|
||||
groups:
|
||||
- name: dlbot
|
||||
rules:
|
||||
- alert: DlbotCoreDown
|
||||
expr: up{job="dlbot-core"} == 0
|
||||
for: 2m
|
||||
labels: {severity: page}
|
||||
annotations:
|
||||
summary: The bot's core is not answering /metrics.
|
||||
|
||||
- alert: DlbotQueueStuck
|
||||
# Jobs wait while no slot is busy: workers are gone or cannot claim.
|
||||
expr: sum(dlbot_queue_jobs) > 0 and dlbot_jobs_running == 0
|
||||
for: 5m
|
||||
labels: {severity: page}
|
||||
annotations:
|
||||
summary: Jobs are queued but nothing is downloading.
|
||||
|
||||
- alert: DlbotQueueSlow
|
||||
expr: dlbot_queue_oldest_wait_seconds > 900
|
||||
for: 10m
|
||||
labels: {severity: warn}
|
||||
annotations:
|
||||
summary: The oldest queued job has waited over 15 minutes. Add workers or slots.
|
||||
|
||||
- alert: DlbotBooksDisagree
|
||||
expr: dlbot_wallets_mismatched > 0
|
||||
labels: {severity: page}
|
||||
annotations:
|
||||
summary: A wallet balance disagrees with its ledger. Stop and investigate before paying anything out.
|
||||
|
||||
- alert: DlbotNoWorkers
|
||||
expr: dlbot_worker_slots == 0
|
||||
for: 5m
|
||||
labels: {severity: page}
|
||||
annotations:
|
||||
summary: No download slots are connected.
|
||||
|
||||
- alert: DlbotWorkerSilent
|
||||
expr: dlbot_worker_heartbeat_age_seconds{state="active"} > 120
|
||||
for: 2m
|
||||
labels: {severity: warn}
|
||||
annotations:
|
||||
summary: Worker {{ $labels.worker }} has not checked in for two minutes.
|
||||
|
||||
- alert: DlbotWorkerTokenRejected
|
||||
expr: dlbot_worker_token_rejected == 1
|
||||
for: 1m
|
||||
labels: {severity: page}
|
||||
annotations:
|
||||
summary: The core rejects the token of {{ $labels.instance }}.
|
||||
|
||||
- alert: DlbotLightningDown
|
||||
expr: dlbot_lightning_up == 0
|
||||
for: 5m
|
||||
labels: {severity: page}
|
||||
annotations:
|
||||
summary: The payment backend (LNbits) is unreachable. Invoices cannot be created or confirmed.
|
||||
|
||||
- alert: DlbotSiteFailing
|
||||
# One site failing most downloads usually means yt-dlp needs an
|
||||
# update or the site blocks the workers' network.
|
||||
expr: sum by (site) (increase(dlbot_download_errors_total{code!~"private|login_required|not_found|too_large|too_long|cancelled"}[30m])) > 10
|
||||
for: 10m
|
||||
labels: {severity: warn}
|
||||
annotations:
|
||||
summary: Downloads from {{ $labels.site }} keep failing. Try /ytdlp update, or route the site to other workers.
|
||||
|
||||
- alert: DlbotWidgetsFallingBack
|
||||
expr: sum(increase(dlbot_widgets_total{outcome="fallback"}[1h])) > 20
|
||||
labels: {severity: info}
|
||||
annotations:
|
||||
summary: sendWidget keeps failing; the bot is using ordinary messages. Nothing breaks, but check the logs.
|
||||
@@ -0,0 +1,21 @@
|
||||
# Example Prometheus configuration for the bot. Copy it next to your
|
||||
# Prometheus, adjust the targets, and load alerts.yml with it.
|
||||
#
|
||||
# The core serves /metrics on HTTP_LISTEN (8080). Each standalone worker
|
||||
# serves /metrics on WORKER_HEALTH_LISTEN (8081). Scrape them over the
|
||||
# private network; do not route /metrics through the public proxy.
|
||||
global:
|
||||
scrape_interval: 30s
|
||||
evaluation_interval: 30s
|
||||
|
||||
rule_files:
|
||||
- alerts.yml
|
||||
|
||||
scrape_configs:
|
||||
- job_name: dlbot-core
|
||||
static_configs:
|
||||
- targets: ["core:8080"]
|
||||
|
||||
- job_name: dlbot-worker
|
||||
static_configs:
|
||||
- targets: ["worker-a:8081", "worker-b:8081"]
|
||||
Reference in New Issue
Block a user