Skip to content

Commit

Permalink
Merge pull request #30 from metrik-tech/feature/ENG-176
Browse files Browse the repository at this point in the history
feature/eng 176: Address all to-do comments in the codebase
  • Loading branch information
4x8Matrix authored Jul 2, 2024
2 parents 9d63712 + 4f4fa02 commit 8290529
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Src/API/ActionBuilder.luau
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Action.Prototype = {}
ActionBuilder.Public = {}
ActionBuilder.Prototype = {}

function Action.Prototype:CanRun(...: unknown) -- todo: figure out how the 'middleware' impl is going to look.
function Action.Prototype:CanRun(...: unknown)
return true
end

Expand Down
50 changes: 33 additions & 17 deletions Src/Services/ApiService.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local ApiPaths = require(script.Parent.Parent.Data.ApiPaths)
local ServerType = require(script.Parent.Parent.Enums.ServerType)

local HEARTBEAT_UPDATE_SECONDS = 60 * 10 -- send server heartbeat every 10 minutes.
local MAX_ATTEMPTS_AT_REQUESTS = 5
local DELAY_BETWEEN_ATTEMPTS = 1.5

local ApiService = {}

Expand All @@ -30,8 +32,6 @@ ApiService.OnAuthenticated = Signal.new()

ApiService.Authenticated = State.new(false)

-- todo: re-queue HTTP requests if they fail!

function ApiService._QueryTraceAsync(self: ApiService)
return self:RawRequestAsync({
Url = `https://{ApiPaths.TraceUrl}`,
Expand Down Expand Up @@ -103,24 +103,40 @@ end

function ApiService.RawRequestAsync(self: ApiService, data: { [any]: any })
return Promise.new(function(resolve, reject)
local success, response = pcall(HttpService.RequestAsync, HttpService, data)
local attempts = 0
local rejectionResponse

if not success or not response.Success then
local responseIsTable = typeof(response) == "table"

return reject({
Success = responseIsTable and response.Success or success,
StatusCode = responseIsTable and response.StatusCode or 0,
StatusMessage = responseIsTable and response.StatusMessage or response,
Headers = responseIsTable and response.Headers or {},
Body = responseIsTable and response.Body or HttpService:JSONEncode({
code = 0,
message = response
})
})
while attempts < MAX_ATTEMPTS_AT_REQUESTS do
local success, response = pcall(HttpService.RequestAsync, HttpService, data)

if not success or not response.Success then
local responseIsTable = typeof(response) == "table"

rejectionResponse = {
Success = responseIsTable and response.Success or success,
StatusCode = responseIsTable and response.StatusCode or 0,
StatusMessage = responseIsTable and response.StatusMessage or response,
Headers = responseIsTable and response.Headers or {},
Body = responseIsTable and response.Body or HttpService:JSONEncode({
code = 0,
message = response
})
}

self.Reporter:Warn(`Attempt #{attempts} to make a request to '{data.Url}' failed;`)
self.Reporter:Warn(rejectionResponse)

task.wait(DELAY_BETWEEN_ATTEMPTS)

attempts += 1

continue
end

return resolve(response)
end

return resolve(response)
return reject(rejectionResponse)
end)
end

Expand Down

0 comments on commit 8290529

Please sign in to comment.