fix: add PID lock to run-fs-scraper-mac.sh — prevent simultaneous instances

Adds /tmp/tip-fs-scraper.lock PID file to prevent launchd from running
a second instance while the previous one is still active (e.g. 2am
schedule fires, runs past 10am when launchd fires again). Without this,
concurrent instances caused rmSync(storage-fs-phase1) in one instance
to delete the Crawlee storage dir while the other was still using it,
resulting in ENOENT crashes.
This commit is contained in:
Rene Fichtmueller 2026-04-18 02:43:28 +02:00
parent 306f329d5a
commit 1c5805ab96

View File

@ -40,7 +40,22 @@ close_tunnel() {
}
# ── Main ──────────────────────────────────────────────────────────────────────
LOCK_FILE="/tmp/tip-fs-scraper.lock"
mkdir -p "$(dirname "$LOG")"
# Prevent simultaneous runs (e.g. launchd fires during a still-running instance)
if [ -f "$LOCK_FILE" ]; then
LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null || echo "")
if [ -n "$LOCK_PID" ] && kill -0 "$LOCK_PID" 2>/dev/null; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Already running (PID $LOCK_PID) — skipping" >> "$LOG"
exit 0
fi
rm -f "$LOCK_FILE"
fi
echo $$ > "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT
log "=== FS.com Mac Scraper starting ==="
# Only close tunnel if we opened it (not if one was already running)