From 9066051c97cec72a5129ab96cd6348f4ccca5a24 Mon Sep 17 00:00:00 2001 From: Tibz-Dankan Date: Fri, 13 Sep 2024 18:52:12 +0300 Subject: [PATCH 1/2] feat: add external api requests for apps that have been inactive --- internal/routes/request/makeRequest.go | 93 ++++++++++++++++++++++---- internal/services/httpRequest.go | 40 ++++++----- 2 files changed, 103 insertions(+), 30 deletions(-) diff --git a/internal/routes/request/makeRequest.go b/internal/routes/request/makeRequest.go index a044985..4acaac8 100644 --- a/internal/routes/request/makeRequest.go +++ b/internal/routes/request/makeRequest.go @@ -13,24 +13,34 @@ import ( // Makes request for the app func MakeAppRequest(app models.App) { - ok, err := validateApp(app) + isValid, requiresExternalRequest, err := validateApp(app) if err != nil { log.Println("Error validating the app: ", err) } - if !ok { + if !isValid { log.Println("Couldn't make request: ", app.Name) return } - + response := services.Response{} appRequestProgress := services.AppRequestProgress{App: app, InProgress: true} services.UserAppMem.Add(app.UserID, appRequestProgress) events.EB.Publish("appRequestProgress", appRequestProgress) - response, err := services.MakeHTTPRequest(app.URL) - if err != nil { - log.Println("Request error:", err) - return + if !requiresExternalRequest { + response, err = services.MakeHTTPRequest(app.URL) + if err != nil { + log.Println("Request error:", err) + return + } + } + + if requiresExternalRequest { + response, err = services.MakeExternalHTTPRequest(app.URL) + if err != nil { + log.Println("Request error:", err) + return + } } request := models.Request{ @@ -54,9 +64,15 @@ func MakeAppRequest(app models.App) { } // Validates the app's eligibility for making requests -func validateApp(app models.App) (bool, error) { +// It returns isValid, requiresExternalRequest and error +func validateApp(app models.App) (bool, bool, error) { if app.IsDisabled { - return false, nil + return false, false, nil + } + + requiresExternal, err := appRequiresExternalRequest(app) + if err != nil { + log.Println("Error getting the external determination", err) } hasLastRequest := len(app.Request) > 0 @@ -64,7 +80,7 @@ func validateApp(app models.App) (bool, error) { // Check and validate requestInterval if len(app.RequestTime) == 0 { if !hasLastRequest { - return true, nil + return true, requiresExternal, nil } log.Println("App doesn't have requestTime") currentTime := time.Now() @@ -80,9 +96,9 @@ func validateApp(app models.App) (bool, error) { } if int(timeDiff) >= requestInterval { - return true, nil + return true, requiresExternal, nil } - return false, nil + return false, requiresExternal, nil } for _, rt := range app.RequestTime { @@ -112,7 +128,7 @@ func validateApp(app models.App) (bool, error) { // Check and validate requestInterval log.Println("App time frame is correct") if !hasLastRequest { - return true, nil + return true, requiresExternal, nil } lastRequestStartedAt, _ := appDateStart.ISOTime() timeDiff := currentTimeStart.Sub(lastRequestStartedAt).Minutes() @@ -122,10 +138,59 @@ func validateApp(app models.App) (bool, error) { } if int(timeDiff) >= requestInterval { - return true, nil + return true, requiresExternal, nil } } } + return false, requiresExternal, nil +} + +func appRequiresExternalRequest(app models.App) (bool, error) { + hasLastRequest := len(app.Request) > 0 + hasRequestTime := len(app.RequestTime) > 0 + + if !hasLastRequest { + return true, nil + } + + if !hasRequestTime { + currentTime := time.Now() + location := currentTime.Location().String() + appDate := services.Date{TimeZone: location, ISOStringDate: app.Request[0].StartedAt.String()} + + currentAppTime, _ := appDate.CurrentTime() + lastRequestStartedAt, _ := appDate.ISOTime() + timeDiff := currentAppTime.Sub(lastRequestStartedAt).Minutes() + + if int(timeDiff) > 15 { + return true, nil + } + + if app.Request[0].StatusCode == 503 && app.Request[0].Duration < 15*1000 { + return true, nil + } + } + + for _, rt := range app.RequestTime { + lastReqStartedAtStr := time.Now().String() + if hasLastRequest { + lastReqStartedAtStr = app.Request[0].StartedAt.String() + } + + appDate := services.Date{TimeZone: rt.TimeZone, ISOStringDate: lastReqStartedAtStr, HourMinSec: rt.Start} + currentAppTime, _ := appDate.CurrentTime() + lastRequestStartedAt, _ := appDate.ISOTime() + timeDiff := currentAppTime.Sub(lastRequestStartedAt).Minutes() + + if int(timeDiff) > 15 { + return true, nil + } + + if app.Request[0].StatusCode == 503 && app.Request[0].Duration < 15*1000 { + return true, nil + } + } + return false, nil } diff --git a/internal/services/httpRequest.go b/internal/services/httpRequest.go index 71de577..c604908 100644 --- a/internal/services/httpRequest.go +++ b/internal/services/httpRequest.go @@ -2,7 +2,6 @@ package services import ( "fmt" - "io" "log" "net" "net/http" @@ -49,12 +48,6 @@ func MakeHTTPRequest(URL string) (Response, error) { if err != nil { log.Printf("Error making request: %v", err) if isTimeoutError(err) { - retryBaseURL := os.Getenv("APPCRONS_RETRY_URL") - retryURL := fmt.Sprintf("%s?url=%s", retryBaseURL, URL) - - if err := MakeHTTPRetryRequest(retryURL); err != nil { - return response, err - } response.StatusCode = 503 response.RequestTimeMS = requestTimeMS response.StartedAt = startTime @@ -71,11 +64,16 @@ func MakeHTTPRequest(URL string) (Response, error) { return response, nil } -func MakeHTTPRetryRequest(URL string) error { - req, err := http.NewRequest(http.MethodGet, URL, nil) +func MakeExternalHTTPRequest(URL string) (Response, error) { + response := Response{} + startTime := time.Now() + + externalURL := fmt.Sprintf("%s?url=%s", os.Getenv("APPCRONS_EXTERNAL_URL"), URL) + + req, err := http.NewRequest(http.MethodGet, externalURL, nil) if err != nil { log.Printf("Error creating request: %v", err) - return err + return response, err } req.Header.Set("Content-Type", "application/json") @@ -89,20 +87,30 @@ func MakeHTTPRetryRequest(URL string) error { client := &http.Client{ Transport: transport, - Timeout: time.Second * 35, + Timeout: time.Minute * 4, } res, err := client.Do(req) + duration := time.Since(startTime) + requestTimeMS := int(duration.Milliseconds()) if err != nil { - return err + log.Printf("Error making request: %v", err) + if isTimeoutError(err) { + response.StatusCode = 503 + response.RequestTimeMS = requestTimeMS + response.StartedAt = startTime + response.Message = "Request timeout" + return response, nil + } + return response, err } - defer res.Body.Close() - body, _ := io.ReadAll(res.Body) - log.Printf("Response Body: %s", string(body)) + response.StatusCode = res.StatusCode + response.RequestTimeMS = requestTimeMS + response.StartedAt = startTime - return nil + return response, nil } func isTimeoutError(err error) bool { From 3218702586fb802138981a7bdc1db89ba3168489 Mon Sep 17 00:00:00 2001 From: Tibz-Dankan Date: Fri, 13 Sep 2024 18:52:44 +0300 Subject: [PATCH 2/2] clean: remove nolonger needed logs --- internal/routes/request/getLiveRequests.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/routes/request/getLiveRequests.go b/internal/routes/request/getLiveRequests.go index 5f7be8b..fd4d979 100644 --- a/internal/routes/request/getLiveRequests.go +++ b/internal/routes/request/getLiveRequests.go @@ -18,7 +18,6 @@ type AppRequestProgress = services.AppRequestProgress func sendMessage(message, userId string, clientManager *services.ClientManager) error { w, ok := clientManager.GetClient(userId) if !ok { - log.Println("Client not found") return nil } @@ -45,7 +44,6 @@ func sendMessage(message, userId string, clientManager *services.ClientManager) func sendAppToClient(appRequestProgress AppRequestProgress, clientManager *services.ClientManager) error { client, ok := clientManager.GetClient(appRequestProgress.UserID) if !ok { - log.Println("Client not found") return nil }