@@ -8,21 +8,23 @@ LIFECYCLE_TRANSITION=${1:-}
8
8
9
9
echo " Stopping buildkite-agent gracefully due to the lifecycle transition: ${LIFECYCLE_TRANSITION} "
10
10
11
- if [ $LIFECYCLE_TRANSITION = " ec2:SPOT_INSTANCE_TERMINATION" ]; then
11
+ if [[ $LIFECYCLE_TRANSITION = " ec2:SPOT_INSTANCE_TERMINATION" ] ]; then
12
12
# Send SIGTERM to the main buildkite-agent process in a non-blocking manner
13
13
# to start a graceful shutdown
14
14
systemctl kill --kill-who=main --signal=SIGTERM buildkite-agent
15
15
16
- SPOT_TERMINATION_TIME=$( curl http://169.254.169.254/latest/meta-data/spot/termination-time 2> /dev/null)
17
- # Convert the RFC3339 spot termination time to a unix timestamp
18
- SPOT_TERMINATION_UNIX=$( date ' +%s' -d ${SPOT_TERMINATION_TIME} )
16
+
17
+ # Spot termination time is provided as the third argument in RFC3339 format,
18
+ # i.e. 2020-10-09T01:33:10Z
19
+ SPOT_TERMINATION_TIME=$3
20
+ SPOT_TERMINATION_TIME_UNIX=$( date ' +%s' -d ${SPOT_TERMINATION_TIME} )
19
21
# Subtracts 20 seconds from the published spot termination time to give the
20
22
# agent a little time to forcefully quit running jobs. If the agent doesn't
21
23
# gracefully quit before this time we will send SIGQUIT.
22
- STOP_BY_UNIX =$( expr ${SPOT_TERMINATION_UNIX } - 20)
24
+ STOP_BY_TIME_UNIX =$( expr ${SPOT_TERMINATION_TIME_UNIX } - 20)
23
25
24
26
echo " Waiting for agents to quit gracefully until 20 seconds before the spot termination time: ${SPOT_TERMINATION_TIME} "
25
- while [ $( date ' +%s' ) -lt $STOP_BY_UNIX ]; do
27
+ while [[ $( date ' +%s' ) -lt $STOP_BY_TIME_UNIX ] ]; do
26
28
if ! pgrep -u buildkite-agent buildkite-agent > /dev/null; then
27
29
echo " All buildkite agents have stopped gracefully"
28
30
exit 0
0 commit comments