Files

26 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Shared helpers: JDK 25, ports, start/stop of the stub and the app. Stop is by recorded PID only.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
export JAVA_HOME="${CHATUI_JAVA_HOME:-/tmp/tools/jdk-25.0.5+8}"
export PATH="$JAVA_HOME/bin:$PATH"
APP_PORT=${APP_PORT:-38180}
STUB_PORT=${STUB_PORT:-38181}
RUN=target/run; mkdir -p "$RUN" output
build() { [ -f target/cp.txt ] || mvn -B -q dependency:build-classpath -Dmdep.outputFile=target/cp.txt >/dev/null
mvn -B -q -DskipTests package >/dev/null; }
start_stub() { # $1 = delay ms
setsid nohup sh -c 'echo $$ > "$1"; shift; exec java "$@"' sh "$RUN/stub.pid" -cp "target/classes:$(cat target/cp.txt)" \
com.ankurm.chatui.stub.OpenAiStubServer "$STUB_PORT" "${1:-20}" > "$RUN/stub.log" 2>&1 < /dev/null &
for i in $(seq 1 30); do curl -s -o /dev/null "localhost:$STUB_PORT/stub/stats" && return; sleep 0.5; done; }
start_app() { # extra args are passed to the app (e.g. --chat.backpressure=limitRate)
setsid nohup sh -c 'echo $$ > "$1"; shift; exec java "$@"' sh "$RUN/app.pid" -jar target/chat-ui-htmx-1.0.0.jar \
--server.port="$APP_PORT" "$@" > "$RUN/app.log" 2>&1 < /dev/null &
for i in $(seq 1 60); do curl -s -o /dev/null "localhost:$APP_PORT/" && return; sleep 1; done; }
stop_pid() { [ -f "$RUN/$1.pid" ] && kill "$(cat "$RUN/$1.pid")" 2>/dev/null || true; rm -f "$RUN/$1.pid"; sleep 1; }
stop_all() { stop_pid app; stop_pid stub; }