← back to Abrams
scripts/drain-queue.sh
42 lines
#!/usr/bin/env bash
# drain-queue.sh — drain pending email queue files through George.
# Tries the local George daemon on :9850 with auth token from env.
# Called by tick.sh on every tick. Safe if George is down — leaves the queue file in place to retry.
set -euo pipefail
cd "$(dirname "$0")/.."
QDIR="data/email-queue"
SENT="data/email-sent"
mkdir -p "$QDIR" "$SENT"
GEORGE_URL="${GEORGE_URL:-http://127.0.0.1:9850}"
GEORGE_TOKEN="${GEORGE_TOKEN:-}"
STAMP="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
count_pending=$(ls "$QDIR"/*.json 2>/dev/null | wc -l | tr -d ' ')
[ "$count_pending" -eq 0 ] && { echo "{\"ts\":\"$STAMP\",\"type\":\"drain-empty\"}" >> data/build-log.jsonl; exit 0; }
drained=0; failed=0
for f in "$QDIR"/*.json; do
[ -f "$f" ] || continue
to=$(python3 -c "import sys,json;print(json.load(open('$f'))['to'])" 2>/dev/null || echo "")
subj=$(python3 -c "import sys,json;print(json.load(open('$f'))['subject'])" 2>/dev/null || echo "")
[ -z "$to" ] && { rm "$f"; continue; }
http_code=$(curl -s -o /tmp/abrams-george-resp.txt -w "%{http_code}" \
-X POST "$GEORGE_URL/api/send" \
-H "Content-Type: application/json" \
${GEORGE_TOKEN:+-H "Authorization: Bearer $GEORGE_TOKEN"} \
--data-binary "@$f" 2>/dev/null || echo "000")
if [ "$http_code" = "200" ] || [ "$http_code" = "201" ]; then
mv "$f" "$SENT/"
drained=$((drained + 1))
else
failed=$((failed + 1))
fi
done
echo "{\"ts\":\"$STAMP\",\"type\":\"drain-queue\",\"drained\":$drained,\"failed\":$failed,\"pending\":$count_pending}" >> data/build-log.jsonl
echo "drained=$drained failed=$failed pending=$count_pending"