~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
116 lines · 3.1 KB Raw History
#!/usr/bin/env bash
# Start/stop/status the local hono-plc did:plc directory used by the e2e scripts.
set -euo pipefail

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO_DIR=$(cd "$SCRIPT_DIR/.." && pwd)

HONO_PLC_DIR=${HONO_PLC_DIR:-/Users/johnandersen777/src/publicdomainrelay/atproto-market/hono-plc}
PORT=${PORT:-2587}
PLC_HOST=${PLC_HOST:-127.0.0.1}
PLC_URL=${PLC_URL:-http://127.0.0.1:$PORT}
PLC_HEALTH_URL=${PLC_HEALTH_URL:-http://127.0.0.1:$PORT/health}
STATE_ROOT=${STATE_ROOT:-$REPO_DIR/.state}
STATE_DIR=${STATE_DIR:-$STATE_ROOT/plc}
PID_FILE=$STATE_DIR/plc.pid
LOG_FILE=$STATE_DIR/plc.log

log() { printf '%s\n' "$*" >&2; }
die() { printf 'error: %s\n' "$*" >&2; exit 1; }

pid_alive() {
  [ -f "$PID_FILE" ] || return 1
  local pid
  pid=$(cat "$PID_FILE" 2>/dev/null) || return 1
  [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null
}

healthy() {
  curl -fsS --max-time 2 -o /dev/null "$PLC_HEALTH_URL" 2>/dev/null
}

start() {
  if pid_alive; then
    log "hono-plc already running (pid $(cat "$PID_FILE"))"
    printf '%s\n' "$PLC_URL"
    return 0
  fi
  command -v deno >/dev/null 2>&1 || die "deno is required but was not found on PATH"
  [ -f "$HONO_PLC_DIR/mod.ts" ] || die "no mod.ts in $HONO_PLC_DIR (set HONO_PLC_DIR to a hono-plc checkout)"
  if healthy; then
    die "something is already serving $PLC_HEALTH_URL (port $PORT is in use)"
  fi
  mkdir -p "$STATE_DIR"
  : >"$LOG_FILE"
  log "starting hono-plc from $HONO_PLC_DIR on $PLC_URL"
  (
    cd "$HONO_PLC_DIR" || exit 1
    exec env PORT="$PORT" HOSTNAME="$PLC_HOST" nohup deno run -A mod.ts
  ) >>"$LOG_FILE" 2>&1 &
  local pid=$!
  printf '%s\n' "$pid" >"$PID_FILE"
  local i
  for i in $(seq 1 240); do
    if healthy; then
      log "hono-plc ready (pid $pid, log $LOG_FILE)"
      printf '%s\n' "$PLC_URL"
      return 0
    fi
    if ! kill -0 "$pid" 2>/dev/null; then
      rm -f "$PID_FILE"
      log "hono-plc exited during startup; last log lines:"
      tail -n 30 "$LOG_FILE" >&2 || true
      die "startup failed"
    fi
    sleep 0.25
  done
  kill "$pid" 2>/dev/null || true
  rm -f "$PID_FILE"
  die "timed out after 60s waiting for $PLC_HEALTH_URL (see $LOG_FILE)"
}

stop() {
  if ! pid_alive; then
    rm -f "$PID_FILE"
    log "hono-plc is not running"
    return 0
  fi
  local pid i
  pid=$(cat "$PID_FILE")
  kill -TERM "$pid" 2>/dev/null || true
  for i in $(seq 1 80); do
    kill -0 "$pid" 2>/dev/null || break
    sleep 0.25
  done
  if kill -0 "$pid" 2>/dev/null; then
    log "hono-plc did not exit after SIGTERM; sending SIGKILL"
    kill -KILL "$pid" 2>/dev/null || true
  fi
  wait "$pid" 2>/dev/null || true
  rm -f "$PID_FILE"
  log "stopped hono-plc pid $pid"
}

status() {
  if pid_alive; then
    local health=unhealthy
    if healthy; then health=healthy; fi
    printf 'running pid=%s url=%s health=%s\n' "$(cat "$PID_FILE")" "$PLC_URL" "$health"
    return 0
  fi
  printf 'not running (state dir %s)\n' "$STATE_DIR"
  return 1
}

usage() {
  printf 'usage: %s start|stop|status\n' "$0" >&2
  exit 2
}

[ $# -eq 1 ] || usage
case "$1" in
start) start ;;
stop) stop ;;
status) status ;;
*) usage ;;
esac