Files
upstream-twenty/packages/twenty-server/scripts/command-background.sh
upstream-mirror e7da97194a
Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
snapshot of https://github.com/twentyhq/twenty.git (main, 2026-09-17; blobs > 131072B omitted)
2026-09-17 23:28:13 +05:00

139 lines
4.2 KiB
Bash
Executable File

#!/bin/sh
set -eu
LOG_FILE="${TWENTY_COMMAND_LOG_FILE:-/tmp/twenty-command.log}"
PID_FILE="${TWENTY_COMMAND_PID_FILE:-/tmp/twenty-command.pid}"
LOCK_FILE="$PID_FILE.lock"
TAIL_LINES=20
export LOG_FILE PID_FILE
command_pid() { [ -s "$PID_FILE" ] && cat "$PID_FILE" || return 1; }
stream() { exec tail -f "$LOG_FILE"; }
is_our_command() {
[ -r "/proc/$1/cmdline" ] || return 0
tr '\0' ' ' < "/proc/$1/cmdline" 2>/dev/null | grep -q 'dist/command/command'
}
is_running() {
pid=$(command_pid) && kill -0 "$pid" 2>/dev/null && is_our_command "$pid"
}
detach_hint() {
echo "Ctrl+C detaches the stream only. Use 'yarn command:prod:background:stop' to stop the run."
}
show_tail() {
[ -f "$LOG_FILE" ] || return 0
echo "Tail of $LOG_FILE:" >&2
tail -n "$TAIL_LINES" "$LOG_FILE"
}
last_run() {
code=$(grep '^EXIT=' "$LOG_FILE" 2>/dev/null | tail -n 1 | cut -d= -f2)
case "$code" in
0) echo "completed" ;;
130) echo "stopped by SIGINT (graceful only if the command handles it)" ;;
143) echo "stopped by SIGTERM (graceful only if the command handles it)" ;;
137) echo "killed (SIGKILL), partial work possible" ;;
"") echo "left no exit code — killed, or never started" ;;
*) echo "failed (exit $code)" ;;
esac
}
start() {
[ $# -ge 1 ] || {
echo "usage: yarn command:prod:background <command> [args]" >&2; exit 1
}
command -v setsid > /dev/null || {
echo "Failed to start: setsid not found, cannot detach the run from this terminal" >&2
echo "Use 'yarn command:prod $1' to run it in the foreground instead." >&2
exit 1
}
# The lock fd is inherited by the detached run and held until it exits, so
# two concurrent starts cannot both pass the liveness check.
exec 9>> "$LOCK_FILE"
if command -v flock > /dev/null; then
flock -n 9 || {
echo "Failed to start: already running (pid $(command_pid || echo unknown)), one background command at a time" >&2
exit 1
}
fi
if is_running; then
echo "Failed to start: already running (pid $(command_pid)), one background command at a time" >&2
exit 1
fi
: > "$LOG_FILE"; rm -f "$PID_FILE"
# Not exec: the wrapper has to outlive node to record EXIT=, and stop has to
# signal node alone, never the process group this would otherwise share.
setsid sh -c '
node dist/command/command "$@" &
echo $! > "$PID_FILE"
wait $!
echo "EXIT=$?"
' command-background "$@" < /dev/null >> "$LOG_FILE" 2>&1 &
i=0
while [ ! -s "$PID_FILE" ] && [ "$i" -lt 10 ]; do i=$((i + 1)); sleep 1; done
is_running || {
echo "Failed to start: see $LOG_FILE" >&2
tail -n "$TAIL_LINES" "$LOG_FILE" >&2
exit 1
}
echo "Running (pid $(command_pid)), logging to $LOG_FILE"
detach_hint
# Release the lock here so only the run itself holds it: a reader left
# attached to the stream must not block the next start.
exec 9>&-
stream
}
logs() {
if is_running; then
echo "Running (pid $(command_pid)), following $LOG_FILE" >&2
detach_hint >&2
stream
fi
[ -f "$LOG_FILE" ] || { echo "Not running, no log at $LOG_FILE" >&2; exit 1; }
echo "Not running, last run $(last_run)" >&2
show_tail
}
stop() {
is_running || { echo "Not running, nothing to stop" >&2; rm -f "$PID_FILE"; exit 1; }
pid=$(command_pid)
case "${1:-}" in
"")
kill -TERM "$pid"
echo "SIGTERM sent to $pid, a command that handles it stops at its next boundary (exit 143)" >&2
sleep 1
show_tail
echo "Follow with 'yarn command:prod:background:logs'. Still stuck: 'stop --now'. Last resort: 'stop --force'." >&2
;;
--now)
kill -TERM "$pid"; sleep 2; kill -TERM "$pid" 2>/dev/null || true
echo "Second SIGTERM sent to $pid, immediate exit with the step in progress left unfinished" >&2
sleep 1
show_tail
;;
--force)
kill -KILL "$pid"
echo "SIGKILL sent to $pid, no graceful boundary so a multi-transaction command may leave partial work" >&2
show_tail
;;
*) echo "usage: stop [--now|--force]" >&2; exit 1 ;;
esac
}
case "${1:-}" in
start) shift; start "$@" ;;
logs) logs ;;
stop) shift; stop "$@" ;;
*) echo "usage: $0 {start <command> [args]|logs|stop [--now|--force]}" >&2; exit 1 ;;
esac