Handle race condition in get on a mutli self-hosted runners setup

This commit is contained in:
Shivam Mathur 2024-11-11 12:53:09 +05:30
parent ce2f681d22
commit 449afbcaec
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A

View File

@ -84,12 +84,19 @@ get() {
if [ "$mode" = "-s" ]; then if [ "$mode" = "-s" ]; then
sudo curl "${curl_opts[@]}" "${links[0]}" sudo curl "${curl_opts[@]}" "${links[0]}"
else else
lock_path="$file_path.lock"
until sudo mkdir "$lock_path" 2>/dev/null; do
echo "Another process is downloading a file at $file_path, waiting"
sleep 1
done
trap 'sudo rm -rf "$lock_path"' EXIT SIGINT SIGTERM
for link in "${links[@]}"; do for link in "${links[@]}"; do
status_code=$(sudo curl -w "%{http_code}" -o "$file_path" "${curl_opts[@]}" "$link") status_code=$(sudo curl -w "%{http_code}" -o "$file_path" "${curl_opts[@]}" "$link")
[ "$status_code" = "200" ] && break [ "$status_code" = "200" ] && break
done done
[ "$execute" = "-e" ] && sudo chmod a+x "$file_path" [ "$execute" = "-e" ] && sudo chmod a+x "$file_path"
[ "$mode" = "-v" ] && echo "$status_code" [ "$mode" = "-v" ] && echo "$status_code"
sudo rm -rf "$lock_path" >/dev/null 2>&1 || true
fi fi
} }