From e76354651c161eed8f52a8b8c7eb19455ddb8ee2 Mon Sep 17 00:00:00 2001 From: itzaname Date: Thu, 6 Jul 2023 23:10:53 -0400 Subject: [PATCH] Add retry logic --- cmd/rbxcompiler/rbxcompiler.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/cmd/rbxcompiler/rbxcompiler.go b/cmd/rbxcompiler/rbxcompiler.go index faaa8f6..da5bd82 100644 --- a/cmd/rbxcompiler/rbxcompiler.go +++ b/cmd/rbxcompiler/rbxcompiler.go @@ -4,9 +4,12 @@ import ( "bytes" "flag" "fmt" + "math" + "os" + "time" + "git.itzana.me/itzaname/go-roblox" "git.itzana.me/itzaname/rbxcompiler/internal/rbxbuilder" - "os" ) var script = flag.Bool("script", false, "If the target is a script.") @@ -16,6 +19,8 @@ var input = flag.String("input", "", "Input path.") var assetId = flag.Int("asset", 0, "Upload asset ID") var groupId = flag.Int("group", 0, "Group ID") var doGenerate = flag.Bool("generate", false, "If a place file should be dumped") +var maxRetries = 5 +var baseDelay = 1 * time.Second func build() { settings := &rbxbuilder.BuildSettings{ @@ -52,10 +57,22 @@ func build() { fmt.Printf("Logged in as %s...\n", rbx.Username) fmt.Printf("Uploading file to roblox...\n") - id, err := rbx.CreateAsset(&roblox.AssetUploadOptions{ - AssetID: *assetId, - Group: *groupId, - }, buffer) + var id int + for i := 0; i < maxRetries; i++ { + id, err = rbx.CreateAsset(&roblox.AssetUploadOptions{ + AssetID: *assetId, + Group: *groupId, + }, buffer) + + if err == nil { + break + } + + backoffSec := math.Pow(2, float64(i)) + fmt.Printf("Upload failed. Will try in %f seconds\n", backoffSec) + time.Sleep(time.Duration(backoffSec) * baseDelay) + } + if err != nil { fmt.Printf("Failed to upload place: %s\n", err) os.Exit(1)