From 117633f6ef505b072fdfdd88120b99feda928290 Mon Sep 17 00:00:00 2001 From: vindard <17693119+vindard@users.noreply.github.com> Date: Mon, 18 Dec 2023 11:00:15 -0400 Subject: [PATCH] build(core): swap in stoppable trigger runner script --- dev/BUCK | 5 ++++ dev/Tiltfile | 2 +- dev/bin/run-stoppable-trigger.sh | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100755 dev/bin/run-stoppable-trigger.sh diff --git a/dev/BUCK b/dev/BUCK index d70e2b09e61..648fd4593b3 100644 --- a/dev/BUCK +++ b/dev/BUCK @@ -80,3 +80,8 @@ sh_binary( name = "setup-hydra-client", main = "bin/setup-hydra-client.sh", ) + +sh_binary( + name = "stoppable-trigger", + main = "bin/run-stoppable-trigger.sh", +) diff --git a/dev/Tiltfile b/dev/Tiltfile index 881e0977fa5..4f667d4dcac 100644 --- a/dev/Tiltfile +++ b/dev/Tiltfile @@ -282,7 +282,7 @@ local_resource( "api-trigger", labels = ["core"], cmd = "buck2 build {}".format(api_trigger_target), - serve_cmd = "buck2 run {}".format(api_trigger_target), + serve_cmd = "buck2 run //dev:stoppable-trigger {}".format(api_trigger_target), serve_env = core_serve_env, allow_parallel = True, readiness_probe = probe( diff --git a/dev/bin/run-stoppable-trigger.sh b/dev/bin/run-stoppable-trigger.sh new file mode 100755 index 00000000000..b70799f94cc --- /dev/null +++ b/dev/bin/run-stoppable-trigger.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +buck_target="$1" + +REPO_ROOT=$(git rev-parse --show-toplevel) +BATS_DIR="${REPO_ROOT}/bats" + +TRIGGER_PID_FILE="$BATS_DIR/.trigger_pid" + +CONTROL_FILE="$BATS_DIR/.stop_trigger" +rm "$CONTROL_FILE" + +start_trigger() { + buck2 run "$buck_target" & + echo "$!" > "$TRIGGER_PID_FILE" +} + +trigger_is_running() { + SERVER_PID=$(head -n 1 "$TRIGGER_PID_FILE") + if ps -p "$SERVER_PID" > /dev/null; then + return 0 + fi + return 1 +} + +while true; do + if [[ -f "$CONTROL_FILE" ]]; then + if trigger_is_running > /dev/null; then + echo "Stopping server..." + kill -15 $SERVER_PID + if [[ "$?" == "0" ]]; then + echo "Successfully stopped trigger" + else + echo "Could not stop trigger, exiting..." + exit 1 + fi + fi + else + if ! trigger_is_running > /dev/null; then + start_trigger + fi + fi + sleep 2 +done