The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
`[Upgrade]` Add a detached-run wrapper for the `upgrade` command (#23497)
Paul Rastoineb69e5644135
diff --git a/packages/twenty-docker/twenty/Dockerfile b/packages/twenty-docker/twenty/Dockerfileindex dc76717f637..8157b983486 100644--- a/packages/twenty-docker/twenty/Dockerfile+++ b/packages/twenty-docker/twenty/Dockerfile@@ -115,6 +115,11 @@ COPY --chown=1000 --from=twenty-server-build /app/node_modules /app/node_modulesCOPY --chown=1000 --from=twenty-server-build /app/packages/twenty-server/package.json /app/packages/twenty-server/COPY --chown=1000 --from=twenty-server-build /app/packages/twenty-server/dist /app/packages/twenty-server/distCOPY --chown=1000 --from=twenty-server-build /app/packages/twenty-server/patches /app/packages/twenty-server/patches+# Shell entrypoints referenced by package.json scripts (upgrade:background*)+COPY --chown=1000 --from=twenty-server-build /app/packages/twenty-server/scripts /app/packages/twenty-server/scripts+# COPY preserves the committed mode, but the scripts are also invoked directly+# and not only through yarn, so do not let a dropped exec bit reach the pod+RUN chmod +x /app/packages/twenty-server/scripts/upgrade-background.sh# Workspace packages (dist + package.json; node_modules symlinks resolve to these)COPY --chown=1000 --from=twenty-server-build /app/packages/twenty-shared/package.json /app/packages/twenty-shared/diff --git a/packages/twenty-server/docs/UPGRADE_COMMANDS.md b/packages/twenty-server/docs/UPGRADE_COMMANDS.mdindex 37661592138..57b4e4117c4 100644--- a/packages/twenty-server/docs/UPGRADE_COMMANDS.md+++ b/packages/twenty-server/docs/UPGRADE_COMMANDS.md@@ -142,6 +142,48 @@ Rerun the command to resume. Nothing is rolled back, and the run picks up from tExpect the first Ctrl+C to look like it did nothing while a long step is running: it takes effect once the step ends.+### Running detached++A foreground `upgrade` dies with the shell that started it, so a dropped `kubectl exec` tunnel, a closed laptop or an expired VPN kills the run. `scripts/upgrade-background.sh` gives it its own session with no controlling terminal and streams the output from a log file instead. Use it for long upgrades over `kubectl exec` or SSH; foreground is still right locally and in CI.++```bash+yarn upgrade:background [args] # start detached, then stream the log+yarn upgrade:background:logs # re-attach from another shell+yarn upgrade:background:stop # graceful stop; --now immediate, --force SIGKILL+```++`[args]` is forwarded verbatim to `upgrade`: `-d/--dry-run`, `-v/--verbose`, `-w/--workspace-id <id>` (repeatable), `--start-from-workspace-id <id>`, `--workspace-count-limit <n>`. The two workspace selectors are mutually exclusive. `--include-slow` is not among them, it belongs to `run-instance-commands`.++Ctrl+C detaches the stream only, and `logs` takes any number of concurrent readers. `logs` reports whether a run is alive before streaming, and on a finished one prints the verdict and the log tail rather than following a log that will never grow again: a segment can run for minutes without printing, so a silent log alone cannot tell a slow step from a dead process.++`stop` has three tiers, three explicit invocations with no timed escalation between them, since a segment can take many minutes and a timer would defeat the graceful path. Each prints the log tail after signalling. Only the node process is signalled, never the process group, which would also kill the wrapper that records the exit code.++| Invocation | Signal | Effect |+| --- | --- | --- |+| `stop` | one `SIGTERM` | stops at the next boundary, exit `143` |+| `stop --now` | two spaced `SIGTERM`s | immediate exit, step in progress left unfinished |+| `stop --force` | `SIGKILL` | no boundary, a multi-transaction command may leave partial work |++The wrapper outlives node and appends an `EXIT=<code>` line, which is what `logs` translates:++| Log | Meaning |+| --- | --- |+| `EXIT=0` | completed |+| `EXIT=130` / `EXIT=143` | graceful stop on `SIGINT` / `SIGTERM` |+| `EXIT=137` | `SIGKILL`ed, from `stop --force` or an OOM kill |+| any other `EXIT=` | failed |+| no `EXIT=` line | killed without a graceful stop, wrapper included (pod replaced, host lost) |++A graceful stop is always safe to rerun: nothing is rolled back and the run resumes from the last command recorded in `upgradeMigration`, each workspace either fully done with its segment or untouched. Rerunning after a forced kill relies on commands being idempotent.++Limits of this mode:++- Ctrl+C cannot reach a detached run, so `stop` is the only graceful entry point and `130` only ever appears on foreground runs.+- Log and PID files live in `/tmp` in the container (`TWENTY_UPGRADE_LOG_FILE`, `TWENTY_UPGRADE_PID_FILE`) and are lost with the pod.+- The start refusal only sees this container. Nothing in `upgrade` prevents two concurrent sequences either: `upgradeMigration` has no in-progress state and the sequence runner takes no advisory lock. One upgrade at a time is an operational rule, not an enforced one.+- Needs `setsid`, present in the runtime image and on Linux, absent on macOS where `start` refuses and points at the foreground command.+- `terminationGracePeriodSeconds` does not apply. PID 1 in the command-runner pod is `tail -f /dev/null`, so on pod deletion the detached process is torn down without ever receiving `SIGTERM`.+## Shipping a command for a future version (deferred drops)You can write a command for a version listed in `TWENTY_NEXT_VERSIONS` — typically the second half of a zero-downtime migration, e.g. dropping a column one release after its replacement ships. Pass the target version to the generator:diff --git a/packages/twenty-server/package.json b/packages/twenty-server/package.jsonindex 834801f893c..ab99ed7001c 100644--- a/packages/twenty-server/package.json+++ b/packages/twenty-server/package.json@@ -12,6 +12,9 @@"database:init:prod": "node dist/database/scripts/setup-db.js && yarn database:migrate:prod --force --include-slow","database:migrate:prod": "node dist/command/command run-instance-commands","clickhouse:migrate:prod": "node dist/database/clickHouse/migrations/run-migrations.js",+ "upgrade:background": "./scripts/upgrade-background.sh start",+ "upgrade:background:logs": "./scripts/upgrade-background.sh logs",+ "upgrade:background:stop": "./scripts/upgrade-background.sh stop","typeorm": "../../node_modules/typeorm/.bin/typeorm"},"dependencies": {diff --git a/packages/twenty-server/scripts/upgrade-background.sh b/packages/twenty-server/scripts/upgrade-background.shnew file mode 100755index 00000000000..ce81a183245--- /dev/null+++ b/packages/twenty-server/scripts/upgrade-background.sh@@ -0,0 +1,120 @@+#!/bin/sh+set -eu++LOG_FILE="${TWENTY_UPGRADE_LOG_FILE:-/tmp/twenty-upgrade.log}"+PID_FILE="${TWENTY_UPGRADE_PID_FILE:-/tmp/twenty-upgrade.pid}"+TAIL_LINES=20+export LOG_FILE PID_FILE++upgrade_pid() { [ -s "$PID_FILE" ] && cat "$PID_FILE" || return 1; }+stream() { exec tail -f "$LOG_FILE"; }++is_our_upgrade() {+ [ -r "/proc/$1/cmdline" ] || return 0+ tr '\0' ' ' < "/proc/$1/cmdline" 2>/dev/null | grep -q 'dist/command/command'+}++is_running() {+ pid=$(upgrade_pid) && kill -0 "$pid" 2>/dev/null && is_our_upgrade "$pid"+}++detach_hint() {+ echo "Ctrl+C detaches the stream only. Use 'yarn upgrade: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 gracefully on SIGINT — rerun to resume" ;;+ 143) echo "stopped gracefully on SIGTERM — rerun to resume" ;;+ 137) echo "killed (SIGKILL) — partial work possible, rerun to resume" ;;+ "") echo "left no exit code — killed, or never started" ;;+ *) echo "failed (exit $code)" ;;+ esac+}++start() {+ 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 upgrade' to run it in the foreground instead." >&2+ exit 1+ }++ if is_running; then+ echo "Failed to start: already running (pid $(upgrade_pid))" >&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 upgrade "$@" &+ echo $! > "$PID_FILE"+ wait $!+ echo "EXIT=$?"+ ' upgrade-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 $(upgrade_pid)), logging to $LOG_FILE"+ detach_hint+ stream+}++logs() {+ if is_running; then+ echo "Running (pid $(upgrade_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=$(upgrade_pid)++ case "${1:-}" in+ "")+ kill -TERM "$pid"+ echo "SIGTERM sent to $pid, it finishes the step in progress then stops (exit 143)" >&2+ sleep 1+ show_tail+ echo "Follow with 'yarn upgrade: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 [args]|logs|stop [--now|--force]}" >&2; exit 1 ;;+esac