Skip to content

Commit 23ff7fc

Browse files
authored
feat: push layers in batches (#393)
* feat: push layers in batches * feat: Check latest inside region loop to prevent early return on our backfill processes
1 parent 60ab01c commit 23ff7fc

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

scripts/publish_layers.sh

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ AVAILABLE_LAYERS=("Datadog-Python37" "Datadog-Python38" "Datadog-Python38-ARM" "
1919
ARCHS=("amd64" "amd64" "amd64""amd64" "amd64" "arm64" "amd64" "arm64" "amd64" "arm64")
2020
AVAILABLE_REGIONS=$(aws ec2 describe-regions | jq -r '.[] | .[] | .RegionName')
2121

22+
BATCH_SIZE=60
23+
PIDS=()
24+
25+
# Makes sure any subprocesses will be terminated with this process
26+
trap "pkill -P $$; exit 1;" INT
27+
2228
# Check that the layer files exist
2329
for layer_file in "${LAYER_PATHS[@]}"
2430
do
@@ -102,6 +108,35 @@ publish_layer() {
102108
echo $version_nbr
103109
}
104110

111+
wait_for_processes() {
112+
for pid in "${PIDS[@]}"; do
113+
wait $pid
114+
done
115+
PIDS=()
116+
}
117+
118+
backfill_layers() {
119+
latest_version=$1
120+
region=$2
121+
layer_name=$3
122+
aws_version_key=$4
123+
layer_path=$5
124+
125+
while [ $latest_version -lt $VERSION ]; do
126+
latest_version=$(publish_layer $region $layer_name $aws_version_key $layer_path)
127+
echo "Published version $latest_version for layer $layer_name in region $region"
128+
129+
# This shouldn't happen unless someone manually deleted the latest version, say 28, and
130+
# then tries to republish 28 again. The published version would actually be 29, because
131+
# Lambda layers are immutable and AWS will skip deleted version and use the next number.
132+
if [ $latest_version -gt $VERSION ]; then
133+
echo "ERROR: Published version $latest_version is greater than the desired version $VERSION!"
134+
echo "Exiting"
135+
exit 1
136+
fi
137+
done
138+
}
139+
105140
for region in $REGIONS
106141
do
107142
echo "Starting publishing layer for region $region..."
@@ -112,31 +147,20 @@ do
112147
echo "Layer $layer_name version $VERSION already exists in region $region, skipping..."
113148
continue
114149
elif [ $latest_version -lt $((VERSION-1)) ]; then
115-
read -p "WARNING: The latest version of layer $layer_name in region $region is $latest_version, publish all the missing versions including $VERSION or EXIT the script (y/n)?" CONT
116-
if [ "$CONT" != "y" ]; then
117-
echo "Exiting"
118-
exit 1
119-
fi
150+
echo "WARNING: The latest version of layer $layer_name in region $region is $latest_version, this will publish all the missing versions including $VERSION"
120151
fi
121152

122153
index=$(index_of_layer $layer_name)
123154
aws_version_key="${PYTHON_VERSIONS_FOR_AWS_CLI[$index]}"
124155
layer_path="${LAYER_PATHS[$index]}"
125156

126-
while [ $latest_version -lt $VERSION ]; do
127-
latest_version=$(publish_layer $region $layer_name $aws_version_key $layer_path)
128-
echo "Published version $latest_version for layer $layer_name in region $region"
129-
130-
# This shouldn't happen unless someone manually deleted the latest version, say 28
131-
# and then try to republish it again. The published version is actually be 29, because
132-
# Lambda layers are immutable and AWS will skip deleted version and use the next number.
133-
if [ $latest_version -gt $VERSION ]; then
134-
echo "ERROR: Published version $latest_version is greater than the desired version $VERSION!"
135-
echo "Exiting"
136-
exit 1
137-
fi
138-
done
157+
backfill_layers $latest_version $region $layer_name $aws_version_key $layer_path &
158+
PIDS+=($!)
159+
if [ ${#PIDS[@]} -ge $BATCH_SIZE ]; then
160+
wait_for_processes
161+
fi
139162
done
140163
done
164+
wait_for_processes
141165

142166
echo "Done !"

0 commit comments

Comments
 (0)