From 316bb0a1df778270ab5e4d0e6ccb96016e39b441 Mon Sep 17 00:00:00 2001 From: DCCONSTRUCTIONS Date: Mon, 4 May 2026 22:53:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=9D=D0=A4=D0=A0=D0=90=20-=20NODEDC=20L?= =?UTF-8?q?AUNCHER:=20supervise=20local=20BFF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 + scripts/install-local-launcher-service.sh | 83 +++++++++++++++++++++ scripts/uninstall-local-launcher-service.sh | 12 +++ 3 files changed, 98 insertions(+) create mode 100755 scripts/install-local-launcher-service.sh create mode 100755 scripts/uninstall-local-launcher-service.sh diff --git a/package.json b/package.json index b9245a5..5da6f1d 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ "type": "module", "scripts": { "dev": "node server/dev-server.mjs", + "dev:service:install": "bash scripts/install-local-launcher-service.sh", + "dev:service:uninstall": "bash scripts/uninstall-local-launcher-service.sh", + "dev:health": "curl -fsS http://launcher.local.nodedc/healthz", "build": "tsc -b && vite build", "preview": "vite preview --host 0.0.0.0", "test": "vitest run", diff --git a/scripts/install-local-launcher-service.sh b/scripts/install-local-launcher-service.sh new file mode 100755 index 0000000..9685ebe --- /dev/null +++ b/scripts/install-local-launcher-service.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash +set -euo pipefail + +LABEL="local.nodedc.launcher-bff" +PLIST="$HOME/Library/LaunchAgents/${LABEL}.plist" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" +NODE_BIN="${NODE_BIN:-$(command -v node)}" +LOG_OUT="/tmp/nodedc-launcher-bff.out.log" +LOG_ERR="/tmp/nodedc-launcher-bff.err.log" +USER_ID="$(id -u)" + +if [[ -z "${NODE_BIN}" || ! -x "${NODE_BIN}" ]]; then + echo "Node.js executable not found. Set NODE_BIN=/absolute/path/to/node and retry." >&2 + exit 1 +fi + +mkdir -p "$(dirname "${PLIST}")" + +launchctl bootout "gui/${USER_ID}/${LABEL}" >/dev/null 2>&1 || true +launchctl bootout "gui/${USER_ID}" "${PLIST}" >/dev/null 2>&1 || true + +if command -v lsof >/dev/null 2>&1; then + while IFS= read -r PID_ON_PORT; do + [[ -z "${PID_ON_PORT}" ]] && continue + COMMAND_LINE="$(ps -p "${PID_ON_PORT}" -o command= 2>/dev/null || true)" + if [[ "${COMMAND_LINE}" == *"server/dev-server.mjs"* ]]; then + kill "${PID_ON_PORT}" >/dev/null 2>&1 || true + fi + done < <(lsof -tiTCP:5173 -sTCP:LISTEN 2>/dev/null || true) +fi + +cat > "${PLIST}" < + + + + Label + ${LABEL} + ProgramArguments + + ${NODE_BIN} + ${REPO_DIR}/server/dev-server.mjs + + WorkingDirectory + ${REPO_DIR} + RunAtLoad + + KeepAlive + + SuccessfulExit + + + EnvironmentVariables + + PATH + $(dirname "${NODE_BIN}"):/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + + StandardOutPath + ${LOG_OUT} + StandardErrorPath + ${LOG_ERR} + + +PLIST + +launchctl bootstrap "gui/${USER_ID}" "${PLIST}" +launchctl enable "gui/${USER_ID}/${LABEL}" >/dev/null 2>&1 || true +launchctl kickstart -k "gui/${USER_ID}/${LABEL}" >/dev/null 2>&1 || true + +for _ in {1..20}; do + if curl -fsS --max-time 2 http://launcher.local.nodedc/healthz >/dev/null 2>&1; then + echo "NODE.DC launcher BFF is running via LaunchAgent: ${LABEL}" + echo "Health: http://launcher.local.nodedc/healthz" + echo "Logs: ${LOG_OUT} / ${LOG_ERR}" + exit 0 + fi + sleep 0.5 +done + +echo "LaunchAgent installed, but health check failed." >&2 +echo "Check logs: ${LOG_OUT} / ${LOG_ERR}" >&2 +exit 1 diff --git a/scripts/uninstall-local-launcher-service.sh b/scripts/uninstall-local-launcher-service.sh new file mode 100755 index 0000000..518e438 --- /dev/null +++ b/scripts/uninstall-local-launcher-service.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -euo pipefail + +LABEL="local.nodedc.launcher-bff" +PLIST="$HOME/Library/LaunchAgents/${LABEL}.plist" +USER_ID="$(id -u)" + +launchctl bootout "gui/${USER_ID}/${LABEL}" >/dev/null 2>&1 || true +launchctl bootout "gui/${USER_ID}" "${PLIST}" >/dev/null 2>&1 || true +rm -f "${PLIST}" + +echo "NODE.DC launcher BFF LaunchAgent removed: ${LABEL}"