refactor(doorbell-touch): extract lockfile logic to shared script
This commit is contained in:
@@ -28,17 +28,8 @@ arduino-cli compile --fqbn "$FQBN" --libraries ./libraries $LIBS --build-propert
|
||||
description = "Upload (uses BOARD env var)"
|
||||
run = """
|
||||
source ./boards/$BOARD/board-config.sh
|
||||
|
||||
LOCKFILE="/tmp/doorbell-$BOARD.lock"
|
||||
if [ -f "$LOCKFILE" ]; then
|
||||
echo "Error: Another instance is running (lockfile exists: $LOCKFILE)"
|
||||
echo "Remove with: rm $LOCKFILE"
|
||||
exit 1
|
||||
fi
|
||||
trap 'rm -f "$LOCKFILE"' EXIT
|
||||
touch "$LOCKFILE"
|
||||
|
||||
echo "Acquired lock: $LOCKFILE"
|
||||
source ./scripts/lockfile.sh
|
||||
FORCE=1 TASK_NAME=upload acquire_lock || exit 1
|
||||
|
||||
PORT="${PORT:-$PORT}"
|
||||
arduino-cli compile --fqbn "$FQBN" --libraries ./libraries $LIBS --build-property "compiler.cpp.extra_flags=$OPTS" --warnings default ./boards/$BOARD && \
|
||||
@@ -49,17 +40,8 @@ arduino-cli upload --fqbn "$FQBN" --port "$PORT" ./boards/$BOARD
|
||||
description = "Monitor (uses BOARD env var)"
|
||||
run = """
|
||||
source ./boards/$BOARD/board-config.sh
|
||||
|
||||
LOCKFILE="/tmp/doorbell-$BOARD.lock"
|
||||
if [ -f "$LOCKFILE" ]; then
|
||||
echo "Error: Another instance is running (lockfile exists: $LOCKFILE)"
|
||||
echo "Remove with: rm $LOCKFILE"
|
||||
exit 1
|
||||
fi
|
||||
trap 'rm -f "$LOCKFILE"' EXIT
|
||||
touch "$LOCKFILE"
|
||||
|
||||
echo "Acquired lock: $LOCKFILE (Ctrl+C to release)"
|
||||
source ./scripts/lockfile.sh
|
||||
acquire_lock || exit 1
|
||||
|
||||
PORT="${PORT:-$PORT}"
|
||||
TARGET="$(readlink -f "$PORT" 2>/dev/null || echo "$PORT")"
|
||||
@@ -70,27 +52,19 @@ arduino-cli monitor -p "$TARGET" --config baudrate=115200
|
||||
description = "Show tio command to run in separate terminal"
|
||||
run = """
|
||||
source ./boards/$BOARD/board-config.sh
|
||||
|
||||
LOCKFILE="/tmp/doorbell-$BOARD.lock"
|
||||
if [ -f "$LOCKFILE" ]; then
|
||||
echo "Error: Another instance is running (lockfile exists: $LOCKFILE)"
|
||||
echo "Remove with: rm $LOCKFILE"
|
||||
exit 1
|
||||
fi
|
||||
trap 'rm -f "$LOCKFILE"' EXIT
|
||||
touch "$LOCKFILE"
|
||||
|
||||
echo "Acquired lock: $LOCKFILE (Ctrl+C to release)"
|
||||
source ./scripts/lockfile.sh
|
||||
acquire_lock || exit 1
|
||||
|
||||
PORT="${PORT:-$PORT}"
|
||||
TARGET="$(readlink -f "$PORT" 2>/dev/null || echo "$PORT")"
|
||||
tio --map INLCRNL "$TARGET" -e
|
||||
"""
|
||||
|
||||
[tasks.monitor-screen-kill]
|
||||
description = "Kill tio (Ctrl+C in the terminal)"
|
||||
[tasks.kill]
|
||||
description = "Kill running monitor/upload for BOARD"
|
||||
run = """
|
||||
echo "Press Ctrl+C in the tio terminal to exit"
|
||||
source ./scripts/lockfile.sh
|
||||
kill_locked
|
||||
"""
|
||||
|
||||
[tasks.install-libs-shared]
|
||||
|
||||
65
sketches/doorbell-touch/scripts/lockfile.sh
Executable file
65
sketches/doorbell-touch/scripts/lockfile.sh
Executable file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env bash
|
||||
# Lockfile helper for doorbell build harness
|
||||
# Source this from mise tasks
|
||||
|
||||
LOCKFILE="/tmp/doorbell-${BOARD:-esp32-32e-4}.lock"
|
||||
|
||||
acquire_lock() {
|
||||
if [ -f "$LOCKFILE" ]; then
|
||||
OLD_PID=$(cat "$LOCKFILE" 2>/dev/null)
|
||||
if [ -n "$OLD_PID" ] && kill -0 "$OLD_PID" 2>/dev/null; then
|
||||
if [ "${FORCE:-}" = "1" ]; then
|
||||
echo "Force: Killing existing process (PID: $OLD_PID)..."
|
||||
kill "$OLD_PID" 2>/dev/null
|
||||
sleep 1
|
||||
kill -9 "$OLD_PID" 2>/dev/null
|
||||
echo "Killed PID $OLD_PID"
|
||||
else
|
||||
echo "Error: Another instance is running (PID: $OLD_PID)"
|
||||
echo "Kill with: mise run kill BOARD=$BOARD"
|
||||
echo "Or force with: FORCE=1 mise run $TASK_NAME BOARD=$BOARD"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "Stale lockfile found, removing..."
|
||||
rm -f "$LOCKFILE"
|
||||
fi
|
||||
fi
|
||||
trap 'rm -f "$LOCKFILE"' EXIT
|
||||
echo "$$" > "$LOCKFILE"
|
||||
echo "Acquired lock: $LOCKFILE (PID: $$)"
|
||||
return 0
|
||||
}
|
||||
|
||||
release_lock() {
|
||||
rm -f "$LOCKFILE"
|
||||
}
|
||||
|
||||
kill_locked() {
|
||||
if [ ! -f "$LOCKFILE" ]; then
|
||||
echo "No lockfile found: $LOCKFILE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
OLD_PID=$(cat "$LOCKFILE" 2>/dev/null)
|
||||
if [ -z "$OLD_PID" ]; then
|
||||
echo "Lockfile is empty, removing..."
|
||||
rm -f "$LOCKFILE"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if kill -0 "$OLD_PID" 2>/dev/null; then
|
||||
echo "Killing PID $OLD_PID..."
|
||||
kill "$OLD_PID" 2>/dev/null
|
||||
sleep 1
|
||||
if kill -0 "$OLD_PID" 2>/dev/null; then
|
||||
echo "Force killing PID $OLD_PID..."
|
||||
kill -9 "$OLD_PID" 2>/dev/null
|
||||
fi
|
||||
echo "Killed PID $OLD_PID"
|
||||
else
|
||||
echo "Process $OLD_PID not running (stale lockfile)"
|
||||
fi
|
||||
rm -f "$LOCKFILE"
|
||||
echo "Lockfile removed"
|
||||
}
|
||||
Reference in New Issue
Block a user