Skip to content

Commit 86338b8

Browse files
mafredripull[bot]
authored andcommitted
fix: Fix develop script pid tracking, improve logging and interrupt (#5186)
1 parent 9ca5742 commit 86338b8

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

cli/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
169169
defer func() {
170170
cmd.Printf("Stopping built-in PostgreSQL...\n")
171171
// Gracefully shut PostgreSQL down!
172-
_ = closeFunc()
173-
cmd.Printf("Stopped built-in PostgreSQL\n")
172+
if err := closeFunc(); err != nil {
173+
cmd.Printf("Failed to stop built-in PostgreSQL: %v\n", err)
174+
} else {
175+
cmd.Printf("Stopped built-in PostgreSQL\n")
176+
}
174177
}()
175178
}
176179

scripts/develop.sh

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,53 +51,84 @@ make -j "build/coder_${GOOS}_${GOARCH}"
5151
# Use the coder dev shim so we don't overwrite the user's existing Coder config.
5252
CODER_DEV_SHIM="${PROJECT_ROOT}/scripts/coder-dev.sh"
5353

54+
# Stores the pid of the subshell that runs our main routine.
55+
ppid=0
56+
# Tracks pids of commands we've started.
5457
pids=()
5558
exit_cleanup() {
5659
set +e
5760
# Set empty interrupt handler so cleanup isn't interrupted.
58-
trap '' INT
59-
# Send interrupt to the entire process group to start shutdown procedures.
60-
kill -INT -$$
61+
trap '' INT TERM
6162
# Remove exit trap to avoid infinite loop.
6263
trap - EXIT
6364

64-
# Just in case, send interrupts to our children.
65+
# Send interrupts to the processes we started. Note that we do not
66+
# (yet) want to send a kill signal to the entire process group as
67+
# this can halt processes started by graceful shutdown.
6568
kill -INT "${pids[@]}" >/dev/null 2>&1
6669
# Use the hammer if things take too long.
67-
{ sleep 5 && kill -TERM -$$ >/dev/null 2>&1; } &
70+
{ sleep 5 && kill -TERM "${pids[@]}" >/dev/null 2>&1; } &
6871

6972
# Wait for all children to exit (this can be aborted by hammer).
7073
wait_cmds
74+
75+
# Just in case, send termination to the entire process group
76+
# in case the children left something behind.
77+
kill -TERM -"${ppid}" >/dev/null 2>&1
78+
7179
exit 1
7280
}
7381
start_cmd() {
82+
name=$1
83+
prefix=$2
84+
shift 2
85+
7486
echo "== CMD: $*" >&2
75-
"$@" 2>&1 || fatal "CMD: $*" &
87+
88+
FORCE_COLOR=1 "$@" > >(
89+
# Ignore interrupt, read will keep reading until stdin is gone.
90+
trap '' INT
91+
92+
while read -r line; do
93+
if [[ $prefix == date ]]; then
94+
echo "[$name] $(date '+%Y-%m-%d %H:%M:%S') $line"
95+
else
96+
echo "[$name] $line"
97+
fi
98+
done
99+
echo "== CMD EXIT: $*" >&2
100+
# Let parent know the command exited.
101+
kill -INT $ppid >/dev/null 2>&1
102+
) 2>&1 &
76103
pids+=("$!")
77104
}
78105
wait_cmds() {
79106
wait "${pids[@]}" >/dev/null 2>&1
80107
}
81108
fatal() {
82109
echo "== FAIL: $*" >&2
83-
exit_cleanup
110+
kill -INT $ppid >/dev/null 2>&1
84111
}
85112

86113
# This is a way to run multiple processes in parallel, and have Ctrl-C work correctly
87114
# to kill both at the same time. For more details, see:
88115
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
89116
(
117+
ppid=$BASHPID
90118
# If something goes wrong, just bail and tear everything down
91119
# rather than leaving things in an inconsistent state.
92-
trap 'exit_cleanup' INT EXIT
120+
trap 'exit_cleanup' INT TERM EXIT
93121
trap 'fatal "Script encountered an error"' ERR
94122

95123
cdroot
96-
start_cmd "${CODER_DEV_SHIM}" server --address 0.0.0.0:3000
124+
start_cmd API "" "${CODER_DEV_SHIM}" server --address 0.0.0.0:3000
97125

98126
echo '== Waiting for Coder to become ready'
127+
# Start the timeout in the background so interrupting this script
128+
# doesn't hang for 60s.
99129
timeout 60s bash -c 'until curl -s --fail http://localhost:3000/healthz > /dev/null 2>&1; do sleep 0.5; done' ||
100-
fatal 'Coder did not become ready in time'
130+
fatal 'Coder did not become ready in time' &
131+
wait $!
101132

102133
# Check if credentials are already set up to avoid setting up again.
103134
"${CODER_DEV_SHIM}" list >/dev/null 2>&1 && touch "${PROJECT_ROOT}/.coderv2/developsh-did-first-setup"
@@ -138,11 +169,7 @@ fatal() {
138169
fi
139170

140171
# Start the frontend once we have a template up and running
141-
CODER_HOST=http://127.0.0.1:3000 start_cmd yarn --cwd=./site dev --host > >(
142-
while read -r line; do
143-
echo "[SITE] $(date -Iseconds): $line"
144-
done
145-
)
172+
CODER_HOST=http://127.0.0.1:3000 start_cmd SITE date yarn --cwd=./site dev --host
146173

147174
interfaces=(localhost)
148175
if which ip >/dev/null 2>&1; then

0 commit comments

Comments
 (0)