feat: add lockfile
Some checks failed
perso/opencode/pipeline/head Something is wrong with the build of this commit

This commit is contained in:
Julien Cabillot
2026-03-30 17:01:40 -04:00
parent 193115e519
commit 9a9eced188
2 changed files with 43 additions and 3 deletions

View File

@@ -1,7 +1,48 @@
#!/bin/sh
set -eu
LOCKFILE="${OPENCODE_ATTACH_LOCK:-/tmp/opencode-attach.lock}"
: "${OPENCODE_SERVER_PASSWORD:?OPENCODE_SERVER_PASSWORD is required}"
: "${OPENCODE_API_URL:?OPENCODE_API_URL is required}"
cleanup_lock() {
if [ -f "$LOCKFILE" ]; then
existing_pid=$(cat "$LOCKFILE" 2>/dev/null || true)
[ "$existing_pid" = "$$" ] && rm -f "$LOCKFILE"
fi
}
kill_existing_instance() {
if [ ! -f "$LOCKFILE" ]; then
return
fi
existing_pid=$(cat "$LOCKFILE" 2>/dev/null || true)
if [ -z "$existing_pid" ] || [ "$existing_pid" = "$$" ]; then
rm -f "$LOCKFILE"
return
fi
if kill -0 "$existing_pid" 2>/dev/null; then
kill "$existing_pid" 2>/dev/null
for _ in 1 2 3 4 5; do
if ! kill -0 "$existing_pid" 2>/dev/null; then
break
fi
sleep 1
done
if kill -0 "$existing_pid" 2>/dev/null; then
kill -9 "$existing_pid" 2>/dev/null || true
fi
fi
rm -f "$LOCKFILE"
}
trap cleanup_lock INT TERM EXIT
kill_existing_instance
printf '%s\n' "$$" > "$LOCKFILE"
exec opencode attach -p "${OPENCODE_SERVER_PASSWORD}" "${OPENCODE_API_URL}" "$@"