File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,16 +13,23 @@ n_failures=0
1313
1414while (( n_failures < 100 )) ; do
1515 current_etag=$( aws s3api head-object --bucket " $bucket " --key " $key " --query ETag --output text 2> /dev/null) || {
16- # File doesn't exist yet, create it fresh
16+ # File doesn't exist yet, try to create it fresh
1717 echo " File does not exist in S3, creating new file."
1818 if [[ " $key " =~ \. gz$ ]]; then
1919 gzip -c " $local_file_to_concatenate " > " $local_concatenated "
2020 else
2121 cp " $local_file_to_concatenate " " $local_concatenated "
2222 fi
23- aws s3api put-object --bucket " $bucket " --key " $key " --body " $local_concatenated "
24- echo " File created and uploaded successfully."
25- exit 0
23+ # Use --if-none-match to avoid race with concurrent writers
24+ aws s3api put-object --bucket " $bucket " --key " $key " --body " $local_concatenated " --if-none-match " *" && {
25+ echo " File created and uploaded successfully."
26+ exit 0
27+ }
28+ # Another writer created the file first, retry to concatenate with it
29+ echo " File was created by another writer, retrying to concatenate."
30+ n_failures=$(( n_failures + 1 ))
31+ sleep 0.1
32+ continue
2633 }
2734 if [[ " $current_etag " == " null" ]]; then
2835 echo " Failed to retrieve ETag. Exiting."
You can’t perform that action at this time.
0 commit comments