@@ -51,53 +51,84 @@ make -j "build/coder_${GOOS}_${GOARCH}"
51
51
# Use the coder dev shim so we don't overwrite the user's existing Coder config.
52
52
CODER_DEV_SHIM=" ${PROJECT_ROOT} /scripts/coder-dev.sh"
53
53
54
+ # Stores the pid of the subshell that runs our main routine.
55
+ ppid=0
56
+ # Tracks pids of commands we've started.
54
57
pids=()
55
58
exit_cleanup () {
56
59
set +e
57
60
# 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
61
62
# Remove exit trap to avoid infinite loop.
62
63
trap - EXIT
63
64
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.
65
68
kill -INT " ${pids[@]} " > /dev/null 2>&1
66
69
# 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 ; } &
68
71
69
72
# Wait for all children to exit (this can be aborted by hammer).
70
73
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
+
71
79
exit 1
72
80
}
73
81
start_cmd () {
82
+ name=$1
83
+ prefix=$2
84
+ shift 2
85
+
74
86
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 &
76
103
pids+=(" $! " )
77
104
}
78
105
wait_cmds () {
79
106
wait " ${pids[@]} " > /dev/null 2>&1
80
107
}
81
108
fatal () {
82
109
echo " == FAIL: $* " >&2
83
- exit_cleanup
110
+ kill -INT $ppid > /dev/null 2>&1
84
111
}
85
112
86
113
# This is a way to run multiple processes in parallel, and have Ctrl-C work correctly
87
114
# to kill both at the same time. For more details, see:
88
115
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
89
116
(
117
+ ppid=$BASHPID
90
118
# If something goes wrong, just bail and tear everything down
91
119
# rather than leaving things in an inconsistent state.
92
- trap ' exit_cleanup' INT EXIT
120
+ trap ' exit_cleanup' INT TERM EXIT
93
121
trap ' fatal "Script encountered an error"' ERR
94
122
95
123
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
97
125
98
126
echo ' == Waiting for Coder to become ready'
127
+ # Start the timeout in the background so interrupting this script
128
+ # doesn't hang for 60s.
99
129
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 $!
101
132
102
133
# Check if credentials are already set up to avoid setting up again.
103
134
" ${CODER_DEV_SHIM} " list > /dev/null 2>&1 && touch " ${PROJECT_ROOT} /.coderv2/developsh-did-first-setup"
@@ -138,11 +169,7 @@ fatal() {
138
169
fi
139
170
140
171
# 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
146
173
147
174
interfaces=(localhost)
148
175
if which ip > /dev/null 2>&1 ; then
0 commit comments