videogen/deploy/megaphone/run-weekly.sh
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

83 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Megaphone weekly run (box 1, systemd timer menivor-megaphone.timer).
# 1. disk guard (skip at >= 85%), 2. prod smoke (web sign-in + API key + free formats),
# 3. this week's Bidstage tender counts from bidstage-pg (read-only),
# 4. render + publish through Menivor's public API (python -m app.domains.megaphone run).
set -euo pipefail
ENV_FILE="${MEGAPHONE_ENV_FILE:-/etc/products/menivor-megaphone.env}"
API_CONTAINER="${MEGAPHONE_API_CONTAINER:-menivor-api-1}"
BIDSTAGE_PG="${MEGAPHONE_BIDSTAGE_PG:-bidstage-pg}"
MAX_DISK_PCT="${MEGAPHONE_MAX_DISK_PCT:-85}"
LOG_DIR="${MEGAPHONE_LOG_DIR:-/srv/menivor/megaphone}"
# Host-side env file (not the container one) so the disk-guard skip path below
# can alert without a running docker container to read MENIVOR_OWNER_NTFY_URL from.
HOST_ENV_FILE="${MEGAPHONE_HOST_ENV_FILE:-/etc/products/menivor.env}"
mkdir -p "$LOG_DIR"
use="$(df --output=pcent / | tail -1 | tr -dc 0-9)"
if [ "$use" -ge "$MAX_DISK_PCT" ]; then
stamp="$(date -u +%Y%m%dT%H%M%SZ)"
msg="megaphone: disk ${use}% >= ${MAX_DISK_PCT}%, skipping this run ($stamp)"
echo "$msg" >&2
# Was previously a silent no-op: the run-19 skip left no file and no alert,
# so the weekly pipeline stalled for 12+ days before anyone noticed. Always
# leave evidence on disk and page the owner channel when we skip.
printf '{"skipped":"disk","disk_pct":%s,"stamp":"%s"}\n' "$use" "$stamp" > "$LOG_DIR/skip-$stamp.json"
find "$LOG_DIR" -type f -mtime +60 -delete
url="$(grep -E '^MENIVOR_OWNER_NTFY_URL=' "$HOST_ENV_FILE" 2>/dev/null | head -1 | cut -d= -f2-)"
[ -n "$url" ] && curl -fsS -m 20 -d "$msg - no Megaphone videos published this week" "$url" >/dev/null || true
exit 0
fi
rows="$(mktemp)"
trap 'rm -f "$rows"' EXIT
PGU="$(docker exec "$BIDSTAGE_PG" printenv POSTGRES_USER)"
PGD="$(docker exec "$BIDSTAGE_PG" printenv POSTGRES_DB)"
docker exec "$BIDSTAGE_PG" psql -U "$PGU" -d "$PGD" -v ON_ERROR_STOP=1 -Atc "
WITH k AS (
SELECT DISTINCT t.id, t.country, t.buyer_name, substr(c, 5, 2) AS div
FROM tenders t, unnest(t.codes) AS c
WHERE t.status = 'open' AND (t.deadline_at IS NULL OR t.deadline_at > now())
AND t.published_at > now() - interval '7 days'
AND c LIKE 'cpv:%' AND t.country IS NOT NULL
)
SELECT coalesce(json_agg(x), '[]'::json) FROM (
SELECT k.country, k.div, count(*) AS n,
(SELECT label FROM code_labels WHERE scheme = 'cpv' AND code = k.div || '000000') AS label,
(SELECT array_agg(b.buyer_name ORDER BY b.c DESC, b.buyer_name) FROM (
SELECT k2.buyer_name, count(*) AS c FROM k k2
WHERE k2.country = k.country AND k2.div = k.div AND k2.buyer_name IS NOT NULL
GROUP BY k2.buyer_name ORDER BY count(*) DESC, k2.buyer_name LIMIT 8) b) AS buyers
FROM k GROUP BY k.country, k.div ORDER BY count(*) DESC LIMIT 60
) x;" > "$rows" || echo '[]' > "$rows"
stamp="$(date -u +%Y%m%dT%H%M%SZ)"
if ! docker exec --env-file "$ENV_FILE" "$API_CONTAINER" python -m app.domains.megaphone smoke \
> "$LOG_DIR/smoke-$stamp.json" 2> "$LOG_DIR/smoke-$stamp.log"; then
echo "megaphone: SMOKE FAILED (see $LOG_DIR/smoke-$stamp.*)" >&2
url="$(docker exec "$API_CONTAINER" printenv MENIVOR_OWNER_NTFY_URL 2>/dev/null || true)"
[ -n "$url" ] && curl -fsS -m 20 -d "Menivor prod smoke FAILED $stamp: $(cat "$LOG_DIR/smoke-$stamp.json" 2>/dev/null)" "$url" >/dev/null || true
fi
status=0
docker exec -i --env-file "$ENV_FILE" "$API_CONTAINER" python -m app.domains.megaphone run --bidstage-json - \
< "$rows" > "$LOG_DIR/run-$stamp.json" 2> "$LOG_DIR/run-$stamp.log" || status=$?
if [ "$status" -ne 0 ]; then
url="$(grep -E '^MENIVOR_OWNER_NTFY_URL=' "$HOST_ENV_FILE" 2>/dev/null | head -1 | cut -d= -f2-)"
[ -n "$url" ] && curl -fsS -m 20 -d "megaphone: weekly run FAILED (exit $status) $stamp - see $LOG_DIR/run-$stamp.log on box" "$url" >/dev/null || true
fi
find "$LOG_DIR" -type f -mtime +60 -delete
cat "$LOG_DIR/run-$stamp.json"
# Distribution: post any newly-published videos to Postiz (see
# distribute-postiz.sh's own header for why this exists and how it's gated).
# Runs even when the render/publish step above failed (new videos from a
# prior successful run may still be unposted) and is non-fatal either way -
# a Postiz hiccup must never flip this script's own exit status, which is
# still `$status` from the render/publish step above.
"$(dirname "$0")/distribute-postiz.sh" || echo "megaphone->postiz: distribute step failed (non-fatal, see its own log)" >&2
exit "$status"