Skip to content

Commit 46669ae

Browse files
committed
Rely on spot termination time passed from lifecycled
1 parent a585afd commit 46669ae

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packer/linux/conf/buildkite-agent/scripts/stop-agent-gracefully

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ LIFECYCLE_TRANSITION=${1:-}
88

99
echo "Stopping buildkite-agent gracefully due to the lifecycle transition: ${LIFECYCLE_TRANSITION}"
1010

11-
if [ $LIFECYCLE_TRANSITION = "ec2:SPOT_INSTANCE_TERMINATION" ]; then
11+
if [[ $LIFECYCLE_TRANSITION = "ec2:SPOT_INSTANCE_TERMINATION" ]]; then
1212
# Send SIGTERM to the main buildkite-agent process in a non-blocking manner
1313
# to start a graceful shutdown
1414
systemctl kill --kill-who=main --signal=SIGTERM buildkite-agent
1515

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})
1921
# Subtracts 20 seconds from the published spot termination time to give the
2022
# agent a little time to forcefully quit running jobs. If the agent doesn't
2123
# 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)
2325

2426
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
2628
if ! pgrep -u buildkite-agent buildkite-agent > /dev/null; then
2729
echo "All buildkite agents have stopped gracefully"
2830
exit 0

0 commit comments

Comments
 (0)