-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathworktree-gc-daemon
More file actions
executable file
·60 lines (48 loc) · 1.44 KB
/
worktree-gc-daemon
File metadata and controls
executable file
·60 lines (48 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
# Worktree Garbage Collection Daemon
# Runs periodically to clean up abandoned worktrees
set -euo pipefail
SUPERPOWERS_LIB="${HOME}/.config/superpowers/lib"
WORKTREE_MANAGER="${SUPERPOWERS_LIB}/worktree-manager"
DAEMON_PID_FILE="/tmp/superpowers-worktree-gc.pid"
DAEMON_LOCK_FILE="/tmp/superpowers-worktree-gc.lock"
LOG_FILE="${HOME}/.config/superpowers/worktree-gc.log"
# Ensure log directory exists
mkdir -p "$(dirname "$LOG_FILE")"
# Handle shutdown signals
RUNNING=1
shutdown() {
log "Daemon shutting down"
RUNNING=0
}
trap 'shutdown' SIGTERM SIGINT
# Atomic lock acquisition using flock
exec 200>"$DAEMON_LOCK_FILE"
if ! flock -n 200; then
echo "Daemon already running (lock held)" >&2
exit 0
fi
# Cleanup on exit
trap 'flock -u 200; rm -f "$DAEMON_PID_FILE"' EXIT
# Write PID while holding lock
echo $$ > "$DAEMON_PID_FILE"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}
log "Worktree GC daemon started (PID: $$)"
# Main loop
while [ "$RUNNING" = "1" ]; do
# Detect status changes
if [ -x "$WORKTREE_MANAGER" ]; then
"$WORKTREE_MANAGER" detect >> "$LOG_FILE" 2>&1 || true
# Run garbage collection
"$WORKTREE_MANAGER" gc >> "$LOG_FILE" 2>&1 || true
fi
# Sleep for 1 hour (in short intervals to allow graceful shutdown)
for i in $(seq 1 360); do
if [ "$RUNNING" = "0" ]; then
break
fi
sleep 10
done
done