#!/bin/sh set -eu umask 077 PG_BIN=$(pg_config --bindir) cleanup() { if [ -n "${watcher:-}" ]; then kill "$watcher" 2>/dev/null || true; fi if [ -n "${relay:-}" ]; then kill -TERM "$relay" 2>/dev/null || true; wait "$relay" 2>/dev/null || true; fi runuser -u postgres -- "$PG_BIN/pg_ctl" -D /opt/canary/pg -m fast -w stop || true } trap cleanup EXIT trap 'exit 143' TERM trap 'exit 130' INT expires=$(cat /opt/canary/expires-at) if [ "$expires" -gt 0 ] && [ "$(date +%s)" -ge "$expires" ]; then exit 0; fi runuser -u postgres -- "$PG_BIN/pg_ctl" -D /opt/canary/pg -l /opt/canary/pg/server.log -o '-c listen_addresses=127.0.0.1 -c shared_buffers=32MB -c max_connections=24 -c max_wal_size=128MB -c min_wal_size=32MB -c checkpoint_timeout=60s' -w start if ! "$PG_BIN/psql" -h 127.0.0.1 -U postgres -Atc "SELECT 1 FROM pg_database WHERE datname='relay'" | grep -q 1; then "$PG_BIN/createdb" -h 127.0.0.1 -U postgres relay fi export DATABASE_URL=postgres://postgres@127.0.0.1:5432/relay export RELAY_UPSTREAM=bsky.network RELAY_INDEX_PROFILE=bounded export RELAY_REPLAY_HISTORY=true RELAY_RAM_HISTORY=false export RELAY_RETENTION_SECONDS=120 RELAY_MAX_EVENTS_BYTES=268435456 export RELAY_EVENTS_PER_FILE=1024 RELAY_GC_INTERVAL_SECONDS=10 export RELAY_DATA_DIR=/opt/canary/data/events COLLECTION_INDEX_DIR=/opt/canary/data/index export FRAME_WORKERS=16 DB_POOL_SIZE=20 RELAY_IO_EXECUTORS=2 export MALLOC_ARENA_MAX=4 MALLOC_TRIM_THRESHOLD_=131072 cd /opt/canary /opt/canary/zlay & relay=$! ( while kill -0 "$relay" 2>/dev/null; do available=$(df -Pk /opt/canary | awk 'NR==2 {print $4}') expires=$(cat /opt/canary/expires-at) if [ "$available" -lt 131072 ] || { [ "$expires" -gt 0 ] && [ "$(date +%s)" -ge "$expires" ]; }; then echo 'canary stopping: disk reserve or lifetime limit reached' >&2 kill -TERM "$relay" sleep 15 kill -KILL "$relay" 2>/dev/null || true exit fi sleep 30 done ) & watcher=$! wait "$relay"