Skip to content

Commit 9823d57

Browse files
committed
refactor(build): simplify retry logic for file uploads in build workflow
1 parent 6d34c88 commit 9823d57

File tree

3 files changed

+26
-32
lines changed

3 files changed

+26
-32
lines changed

.github/workflows/build.yml

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -293,34 +293,17 @@ jobs:
293293
apiToken: ${{ secrets.CF_R2_API_TOKEN }}
294294
wranglerVersion: "4.21.1"
295295
command: |
296-
retry_upload() {
297-
local file_path="$1"
298-
local remote_path="$2"
299-
local max_attempts=3
300-
local attempt=1
301-
302-
while [ $attempt -le $max_attempts ]; do
303-
echo "Attempt $attempt of $max_attempts: Uploading $file_path to $remote_path"
304-
if r2 object put "$remote_path" --file "$file_path" --remote; then
305-
echo "Successfully uploaded $file_path on attempt $attempt"
306-
return 0
307-
else
308-
echo "Failed to upload $file_path on attempt $attempt"
309-
if [ $attempt -lt $max_attempts ]; then
310-
echo "Waiting 5 seconds before retry..."
311-
sleep 5
312-
fi
313-
attempt=$((attempt + 1))
314-
fi
315-
done
316-
317-
echo "Failed to upload $file_path after $max_attempts attempts"
318-
return 1
319-
}
320-
321-
# Upload files with retry
322-
retry_upload ./${{ env.DIST }}.tar.gz nginx-ui-dev-build/${{ env.DIST }}.tar.gz
323-
retry_upload ./${{ env.DIST }}.tar.gz.digest nginx-ui-dev-build/${{ env.DIST }}.tar.gz.digest
296+
for i in 1 2 3; do
297+
echo "Attempt $i: Uploading ${{ env.DIST }}.tar.gz"
298+
wrangler r2 object put nginx-ui-dev-build/${{ env.DIST }}.tar.gz --file ./${{ env.DIST }}.tar.gz --remote && break
299+
[ $i -lt 3 ] && sleep 5
300+
done
301+
302+
for i in 1 2 3; do
303+
echo "Attempt $i: Uploading ${{ env.DIST }}.tar.gz.digest"
304+
wrangler r2 object put nginx-ui-dev-build/${{ env.DIST }}.tar.gz.digest --file ./${{ env.DIST }}.tar.gz.digest --remote && break
305+
[ $i -lt 3 ] && sleep 5
306+
done
324307
325308
docker-build:
326309
if: github.event_name != 'pull_request'

internal/nginx/resolve_path.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"github.com/uozi-tech/cosy/logger"
1212
)
1313

14+
var (
15+
nginxPrefix string
16+
)
17+
1418
// Returns the directory containing the nginx executable
1519
func GetNginxExeDir() string {
1620
return filepath.Dir(getNginxSbinPath())
@@ -32,14 +36,21 @@ func resolvePath(path string) string {
3236

3337
// GetPrefix returns the prefix of the nginx executable
3438
func GetPrefix() string {
39+
if nginxPrefix != "" {
40+
return nginxPrefix
41+
}
42+
3543
out := getNginxV()
3644
r, _ := regexp.Compile(`--prefix=(\S+)`)
3745
match := r.FindStringSubmatch(out)
3846
if len(match) < 1 {
3947
logger.Error("nginx.GetPrefix len(match) < 1")
40-
return "/usr/local/nginx"
48+
nginxPrefix = "/usr/local/nginx"
49+
return nginxPrefix
4150
}
42-
return resolvePath(match[1])
51+
52+
nginxPrefix = resolvePath(match[1])
53+
return nginxPrefix
4354
}
4455

4556
// GetConfPath returns the path of the nginx configuration file

internal/nginx_log/nginx_log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ import (
1717
// Regular expression for log directives - matches access_log or error_log
1818
var (
1919
logDirectiveRegex = regexp.MustCompile(`(?m)(access_log|error_log)\s+([^\s;]+)(?:\s+[^;]+)?;`)
20-
prefix = ""
2120
)
2221

2322
// Use init function to automatically register callback
2423
func init() {
25-
prefix = nginx.GetPrefix()
2624
// Register the callback directly with the global registry
2725
cache.RegisterCallback(scanForLogDirectives)
2826
}
2927

3028
// scanForLogDirectives scans and parses configuration files for log directives
3129
func scanForLogDirectives(configPath string, content []byte) error {
30+
prefix := nginx.GetPrefix()
3231
// First, remove all log paths that came from this config file
3332
// This ensures that removed log directives are properly cleaned up
3433
RemoveLogPathsFromConfig(configPath)
@@ -147,6 +146,7 @@ func isValidLogPath(logPath string) bool {
147146

148147
// IsLogPathUnderWhiteList checks if a log path is under one of the paths in LogDirWhiteList
149148
func IsLogPathUnderWhiteList(path string) bool {
149+
prefix := nginx.GetPrefix()
150150
cacheKey := fmt.Sprintf("isLogPathUnderWhiteList:%s", path)
151151
res, ok := cache.Get(cacheKey)
152152

0 commit comments

Comments
 (0)