mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 07:38:43 +01:00
Why apps hang while systemd calls them active. Every app unit has MemoryHigh=768M (MemoryMax=1G): above 768M the kernel slows the process instead of stopping it. Node does not read that. It sizes its heap from the whole machine and lets garbage pile up past the mark before it collects. Measured on Solon's production build (a database with no law acts), 930 requests eight at a time: no limit 775 MB peak (855 and 990 on other runs), 364 MB idle 512 MB heap 585 MB peak, same answers, same speed On 2026-11-28 solon stopped answering three times while "active", a favicon took 4 s, and Loki, OrangeCat and Substrata on the same box were fine; the reconciler also caught substrata, heidi and sites not answering within the same day. Not proven from the box (no session can read it; forge-ops#29 prints memory.events), but it is the mechanism the unit file itself sets up. launch.sh.tmpl: start node with --max-old-space-size at 65 % of the lower of the unit's memory.high and memory.max (768M -> 499). NODE_HEAP_MB or an own --max-old-space-size in the app's .env wins; a unit with no limit gets no flag. Setting it from inside the app does not work (v8.setFlagsFromString after start: 849 MB peak, no better than none). deploy.sh: launch.sh was written once by sync-infra and never again, so a template change reached no running app. Each deploy now renders it again with the generator's own function (lib.sh render_launch, which sync-infra uses too), keeping the box copy's port, checked with bash -n before it replaces the old file. The old file is kept and put back on a rollback. test-launch-template.sh: 8 new behavioural checks. test:ops passes. Claude-Session: https://claude.ai/code/session_019MDtmvTCDbJAYNKQpNyKzz Co-authored-by: Claude <noreply@anthropic.com>
102 lines
5.8 KiB
Cheetah
102 lines
5.8 KiB
Cheetah
#!/usr/bin/env bash
|
|
# Generated onto the box by sync-infra.sh. __PORT__ is substituted per app.
|
|
# This template is the ONE definition of the launch script — do not paste a
|
|
# second copy into deploy.sh or patch the generated file after the fact.
|
|
set -euo pipefail
|
|
# `pwd -P`, not `pwd`: /opt/<name>/app is a SYMLINK into releases/, and find(1)
|
|
# does not descend a logical symlink path. With a plain `pwd` the server.js
|
|
# search below returns nothing and the app crash-loops on every deploy.
|
|
#
|
|
# This has now cost two outages. vitareba hit it on its first release-deploy
|
|
# (2026-07-17) and the fix was applied to deploy.sh — which sed-patched the
|
|
# already-generated file — instead of to the generator that keeps emitting the
|
|
# broken version. sbb-fundbuero hit the identical failure on 2026-09-08 the
|
|
# moment sync-infra.sh ran without a subsequent deploy. Fixing the output of a
|
|
# generator does not fix the generator.
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
# The fleet's shared keys first (AI vendors, web search, the shared mail
|
|
# account; scripts/hetzner/fleet-env.sh), then the app's own .env, so an app
|
|
# that holds its own key for any of them keeps it. An EMPTY line in the app's
|
|
# file does not erase a fleet key: the app templates ship `RESEND_API_KEY=`, and
|
|
# that blank kept password-recovery mail off on diplodoctor, solon and heidi
|
|
# while the fleet held the key (2026-10-31). To switch fleet keys off for one
|
|
# app, name them in its .env: FLEET_KEYS_OFF="RESEND_API_KEY GROQ_API_KEY".
|
|
# load-build-env.sh does the same for the build; test-fleet-env-merge.sh runs both.
|
|
__fleet_keys=""
|
|
if [ -f /opt/fleet/env ]; then
|
|
set -a; source /opt/fleet/env; set +a
|
|
for __fk in $(sed -nE 's/^[[:space:]]*(export[[:space:]]+)?([A-Za-z_][A-Za-z0-9_]*)=.*/\2/p' /opt/fleet/env); do
|
|
printf -v "__fleet_$__fk" '%s' "${!__fk-}"; __fleet_keys="$__fleet_keys $__fk"
|
|
done
|
|
fi
|
|
if [ -f "$HERE/.env" ]; then set -a; source "$HERE/.env"; set +a; fi
|
|
for __fk in $__fleet_keys; do
|
|
__fv="__fleet_$__fk"; [ -n "${!__fk-}" ] || export "$__fk=${!__fv}"; unset "$__fv"
|
|
done
|
|
for __fk in ${FLEET_KEYS_OFF-}; do unset "$__fk"; done
|
|
unset __fleet_keys __fk __fv
|
|
# One usage log for the whole box (@bitbaum/ai-kit `usage-log`): each model
|
|
# call an app makes is one line under AI_USAGE_DIR, with the app's name, the
|
|
# vendor, the model and what came of it, never the content. About $567 went
|
|
# to Google on 10-11 Nov 2026 and nobody could say which app spent it. An
|
|
# app's own .env may point elsewhere or name itself; neither is set where the
|
|
# directory is missing, so a box without it runs exactly as before.
|
|
__usage_dir="${FLEET_USAGE_DIR:-/opt/fleet/ai-usage}"
|
|
if [ -z "${AI_USAGE_DIR-}" ] && [ -d "$__usage_dir" ] && [ -w "$__usage_dir" ]; then
|
|
export AI_USAGE_DIR="$__usage_dir"
|
|
fi
|
|
if [ -z "${AI_APP_ID-}" ]; then
|
|
__app="${HERE%/releases/*}"; __app="${__app%/app}"; __app="${__app##*/}"
|
|
[ -n "$__app" ] && export AI_APP_ID="$(printf '%s' "$__app" | tr 'a-z-' 'A-Z_')"
|
|
fi
|
|
unset __usage_dir __app
|
|
# HOSTNAME defaults to loopback; an app sets BIND_HOST in its .env (sourced
|
|
# just above, so it wins) to bind elsewhere. Solon needed BIND_HOST=0.0.0.0
|
|
# (2026-09-25, ~30 min of 500s): Next 16 standalone plus any middleware rewrite
|
|
# (next-intl as-needed: /hire -> /en/hire) behind Caddy's X-Forwarded-Proto:
|
|
# https failed every rewritten page with "Failed to proxy
|
|
# https://localhost:4018/en ... EPROTO". Next builds the rewrite URL with host
|
|
# `localhost` but compares it to an origin built from HOSTNAME, and it
|
|
# normalises 127.0.0.1 -> localhost, so the app cannot fix this in its own
|
|
# code. 0.0.0.0 is safe ONLY because ufw on the box allows 22/80/443 alone:
|
|
# the app ports are not reachable publicly (verified for :4004 and :4018).
|
|
# Open an app port in ufw and this override becomes a public bind.
|
|
export NODE_ENV=production PORT=__PORT__ HOSTNAME="${BIND_HOST:-127.0.0.1}"
|
|
# Node's heap limit follows the memory this unit is given. The unit has
|
|
# MemoryHigh (sync-infra.sh: 768M), above which the kernel slows the process
|
|
# down instead of stopping it. Node does not read that: it sizes its heap from
|
|
# the whole machine and lets garbage pile up far past 768M before collecting.
|
|
# Measured on Solon's production build, 930 requests eight at a time: 775 MB at
|
|
# the peak with no limit, 585 MB with a 512 MB heap, same answers, same speed,
|
|
# 364 MB idle either way. On 2026-11-28 solon stopped answering three times
|
|
# while its unit stayed "active" and a favicon took 4 s; substrata, heidi and
|
|
# sites each had such a spell within a day. So: 65 % of the lower of
|
|
# memory.high and memory.max, the rest left for what is not heap. An app sets
|
|
# NODE_HEAP_MB in its .env for a number of its own, or a whole NODE_OPTIONS
|
|
# with its own --max-old-space-size; a unit with no limit gets no flag.
|
|
if [[ " ${NODE_OPTIONS-} " != *max-old-space-size* ]]; then
|
|
__heap="${NODE_HEAP_MB-}"
|
|
if [ -z "$__heap" ]; then
|
|
__cg="${LAUNCH_CGROUP_ROOT:-/sys/fs/cgroup}$(sed -n 's/^0:://p' "${LAUNCH_PROC_CGROUP:-/proc/self/cgroup}" 2>/dev/null | head -1)"
|
|
__low=""
|
|
for __f in memory.high memory.max; do
|
|
__v="$(cat "$__cg/$__f" 2>/dev/null || true)"
|
|
if [[ "$__v" =~ ^[0-9]+$ ]]; then
|
|
if [ -z "$__low" ] || [ "$__v" -lt "$__low" ]; then __low="$__v"; fi
|
|
fi
|
|
done
|
|
if [ -n "$__low" ]; then
|
|
__heap=$(( __low * 65 / 100 / 1048576 ))
|
|
if [ "$__heap" -lt 256 ]; then __heap=256; fi
|
|
fi
|
|
fi
|
|
if [[ "$__heap" =~ ^[0-9]+$ ]]; then
|
|
export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--max-old-space-size=$__heap"
|
|
fi
|
|
fi
|
|
unset __heap __cg __low __f __v
|
|
SERVER_JS=$(find "$HERE" -maxdepth 4 -name server.js -not -path "*node_modules*" | head -1)
|
|
if [ -n "$SERVER_JS" ]; then exec /usr/bin/node "$SERVER_JS"; fi
|
|
# Non-Next services define their own entrypoint via START_CMD in .env
|
|
exec bash -c "${START_CMD:?no server.js found and START_CMD unset}"
|