File size: 5,369 Bytes
b152fd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
#
# Kill all local Paperclip dev server processes (across all worktrees).
#
# Usage:
#   scripts/kill-dev.sh        # kill all paperclip dev processes
#   scripts/kill-dev.sh --dry  # preview what would be killed
#

set -euo pipefail
shopt -s nullglob

DRY_RUN=false
if [[ "${1:-}" == "--dry" || "${1:-}" == "--dry-run" || "${1:-}" == "-n" ]]; then
  DRY_RUN=true
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
REPO_PARENT="$(dirname "$REPO_ROOT")"

node_pids=()
node_lines=()
pg_pids=()
pg_pidfiles=()
pg_data_dirs=()

is_pid_running() {
  local pid="$1"
  kill -0 "$pid" 2>/dev/null
}

read_pidfile_pid() {
  local pidfile="$1"
  local first_line
  first_line="$(head -n 1 "$pidfile" 2>/dev/null | tr -d '[:space:]' || true)"
  if [[ "$first_line" =~ ^[0-9]+$ ]] && (( first_line > 0 )); then
    printf '%s\n' "$first_line"
    return 0
  fi
  return 1
}

command_for_pid() {
  local pid="$1"
  ps -o command= -p "$pid" 2>/dev/null || true
}

append_postgres_from_pidfile() {
  local pidfile="$1"
  local pid cmd data_dir
  pid="$(read_pidfile_pid "$pidfile" || true)"
  [[ -n "$pid" ]] || return 0
  is_pid_running "$pid" || return 0
  cmd="$(command_for_pid "$pid")"
  [[ "$cmd" == *postgres* ]] || return 0

  for existing_pid in "${pg_pids[@]:-}"; do
    [[ "$existing_pid" == "$pid" ]] && return 0
  done

  data_dir="$(dirname "$pidfile")"
  pg_pids+=("$pid")
  pg_pidfiles+=("$pidfile")
  pg_data_dirs+=("$data_dir")
}

wait_for_pid_exit() {
  local pid="$1"
  local timeout_sec="$2"
  local waited=0
  while is_pid_running "$pid"; do
    if (( waited >= timeout_sec * 10 )); then
      return 1
    fi
    sleep 0.1
    ((waited += 1))
  done
  return 0
}

while IFS= read -r line; do
  [[ -z "$line" ]] && continue
  [[ "$line" == *postgres* ]] && continue
  pid=$(echo "$line" | awk '{print $2}')
  node_pids+=("$pid")
  node_lines+=("$line")
done < <(ps aux | grep -E '/paperclip(-[^/]+)?/' | grep node | grep -v grep || true)

candidate_pidfiles=()
candidate_pidfiles+=(
  "$HOME"/.paperclip/instances/*/db/postmaster.pid
  "$REPO_ROOT"/.paperclip/instances/*/db/postmaster.pid
  "$REPO_ROOT"/.paperclip/runtime-services/instances/*/db/postmaster.pid
)

for sibling_root in "$REPO_PARENT"/paperclip*; do
  [[ -d "$sibling_root" ]] || continue
  candidate_pidfiles+=(
    "$sibling_root"/.paperclip/instances/*/db/postmaster.pid
    "$sibling_root"/.paperclip/runtime-services/instances/*/db/postmaster.pid
  )
done

for pidfile in "${candidate_pidfiles[@]:-}"; do
  [[ -f "$pidfile" ]] || continue
  append_postgres_from_pidfile "$pidfile"
done

if [[ ${#node_pids[@]} -eq 0 && ${#pg_pids[@]} -eq 0 ]]; then
  echo "No Paperclip dev processes found."
  exit 0
fi

if [[ ${#node_pids[@]} -gt 0 ]]; then
  echo "Found ${#node_pids[@]} Paperclip dev node process(es):"
  echo ""

  for i in "${!node_pids[@]:-}"; do
    line="${node_lines[$i]}"
    pid=$(echo "$line" | awk '{print $2}')
    start=$(echo "$line" | awk '{print $9}')
    cmd=$(echo "$line" | awk '{for(i=11;i<=NF;i++) printf "%s ", $i; print ""}')
    cmd=$(echo "$cmd" | sed "s|$HOME/||g")
    printf "  PID %-7s  started %-10s  %s\n" "$pid" "$start" "$cmd"
  done

  echo ""
fi

if [[ ${#pg_pids[@]} -gt 0 ]]; then
  echo "Found ${#pg_pids[@]} embedded PostgreSQL master process(es):"
  echo ""

  for i in "${!pg_pids[@]:-}"; do
    pid="${pg_pids[$i]}"
    data_dir="${pg_data_dirs[$i]}"
    pidfile="${pg_pidfiles[$i]}"
    short_data_dir="${data_dir/#$HOME\//}"
    short_pidfile="${pidfile/#$HOME\//}"
    printf "  PID %-7s  data %-55s  pidfile %s\n" "$pid" "$short_data_dir" "$short_pidfile"
  done

  echo ""
fi

if [[ "$DRY_RUN" == true ]]; then
  echo "Dry run — re-run without --dry to kill these processes."
  exit 0
fi

if [[ ${#node_pids[@]} -gt 0 ]]; then
  echo "Sending SIGTERM to Paperclip node processes..."
  for pid in "${node_pids[@]}"; do
    kill -TERM "$pid" 2>/dev/null && echo "  signaled $pid" || echo "  $pid already gone"
  done
  echo "Waiting briefly for node processes to exit..."
  sleep 2
fi

leftover_pg_pids=()
leftover_pg_data_dirs=()
for i in "${!pg_pids[@]:-}"; do
  pid="${pg_pids[$i]}"
  if is_pid_running "$pid"; then
    leftover_pg_pids+=("$pid")
    leftover_pg_data_dirs+=("${pg_data_dirs[$i]}")
  fi
done

if [[ ${#leftover_pg_pids[@]} -gt 0 ]]; then
  echo "Sending SIGTERM to leftover embedded PostgreSQL processes..."
  for i in "${!leftover_pg_pids[@]:-}"; do
    pid="${leftover_pg_pids[$i]}"
    data_dir="${leftover_pg_data_dirs[$i]}"
    kill -TERM "$pid" 2>/dev/null \
      && echo "  signaled $pid ($data_dir)" \
      || echo "  $pid already gone"
  done
  echo "Waiting up to 15s for PostgreSQL to shut down cleanly..."
  for pid in "${leftover_pg_pids[@]:-}"; do
    if wait_for_pid_exit "$pid" 15; then
      echo "  postgres $pid exited cleanly"
    fi
  done
fi

if [[ ${#node_pids[@]} -gt 0 ]]; then
  for pid in "${node_pids[@]:-}"; do
    if kill -0 "$pid" 2>/dev/null; then
      echo "  node $pid still alive, sending SIGKILL..."
      kill -KILL "$pid" 2>/dev/null || true
    fi
  done
fi

if [[ ${#pg_pids[@]} -gt 0 ]]; then
  for pid in "${pg_pids[@]:-}"; do
    if kill -0 "$pid" 2>/dev/null; then
      echo "  postgres $pid still alive, sending SIGKILL..."
      kill -KILL "$pid" 2>/dev/null || true
    fi
  done
fi

echo "Done."