host92 lines · 3.0 KB · raw
1#!/usr/bin/env bash
2# Starts, stops and restarts one host of the backend for the cross-host checks, each as its own process
3# group so nothing else running on the machine is touched.
4#
5#     nix develop ~/whiskers#backend -c backend/worker/tools/host <native|wrangler|celld> <start|stop|restart|fresh|url>
6#
7#   start    starts it on its port with whatever state it already has, and waits until it answers
8#   stop     stops it (state stays)
9#   restart  stop, start
10#   fresh    stop, erase its state, start
11#   url      prints its address
12#
13# Ports: celld 8801, wrangler 8802, native 8803. State and logs: $WHISKERS_SPIKE_STATE
14# (default /tmp/whiskers-cellflare); `celld dev` keeps its state in backend/worker/.celld/dev and
15# `wrangler dev` in $WHISKERS_SPIKE_STATE/wrangler. Run inside `nix develop ~/whiskers#backend`.
16set -euo pipefail
17here="$(cd "$(dirname "$0")/.." && pwd)"
18root="$(cd "$here/../.." && pwd)"
19state="${WHISKERS_SPIKE_STATE:-/tmp/whiskers-cellflare}"
20mkdir -p "$state"
21which="${1:?native, wrangler or celld}"
22action="${2:?start, stop, restart, fresh or url}"
23
24case "$which" in
25  celld)    port=8801 ;;
26  wrangler) port=8802 ;;
27  native)   port=8803 ;;
28  *) echo "unknown host $which" >&2; exit 2 ;;
29esac
30url="http://127.0.0.1:$port"
31pidfile="$state/$which.pid"
32log="$state/$which.log"
33
34running() { [ -f "$pidfile" ] && kill -0 "$(cat "$pidfile")" 2>/dev/null; }
35
36stop() {
37  if running; then
38    pgid="$(cat "$pidfile")"
39    kill -TERM -- "-$pgid" 2>/dev/null || true
40    for _ in $(seq 1 100); do kill -0 "$pgid" 2>/dev/null || break; sleep 0.1; done
41    kill -KILL -- "-$pgid" 2>/dev/null || true
42  fi
43  rm -f "$pidfile"
44}
45
46erase() {
47  case "$which" in
48    celld)    rm -rf "$here/.celld" ;;
49    wrangler) rm -rf "$state/wrangler" ;;
50    native)   rm -rf "$state/native-home" ;;
51  esac
52}
53
54start() {
55  running && return 0
56  echo "----- start $(date -u +%FT%TZ)" >> "$log"
57  case "$which" in
58    celld)
59      cd "$here"
60      setsid celld dev . --port "$port" --logs >> "$log" 2>&1 &
61      ;;
62    wrangler)
63      cd "$here"
64      setsid wrangler dev --local --port "$port" --persist-to "$state/wrangler" >> "$log" 2>&1 &
65      ;;
66    native)
67      mkdir -p "$state/native-home"
68      HOME="$state/native-home" WHISKERS_MODEL_URL=http://127.0.0.1:1 WHISKERS_LOG="${WHISKERS_LOG:-info}" \
69        setsid "$root/target/debug/whiskersd" "127.0.0.1:$port" >> "$log" 2>&1 &
70      ;;
71  esac
72  echo $! > "$pidfile"
73  for _ in $(seq 1 600); do
74    if curl -s --noproxy '*' -o /dev/null -X POST -d '{"facts":[],"forgotten":[]}' -H 'x-whiskers-household: readiness-probe' "$url/memory/sync" 2>/dev/null; then
75      echo "$which ready at $url"
76      return 0
77    fi
78    running || { echo "$which exited; see $log" >&2; return 1; }
79    sleep 0.5
80  done
81  echo "$which did not answer within 300 s; see $log" >&2
82  return 1
83}
84
85case "$action" in
86  start) start ;;
87  stop) stop ;;
88  restart) stop; start ;;
89  fresh) stop; erase; : > "$log"; start ;;
90  url) echo "$url" ;;
91  *) echo "unknown action $action" >&2; exit 2 ;;
92esac