Skip to content

made tons of health api fixes#354

Open
LalitDeore wants to merge 1 commit intoShuffle:mainfrom
LalitDeore:health-api
Open

made tons of health api fixes#354
LalitDeore wants to merge 1 commit intoShuffle:mainfrom
LalitDeore:health-api

Conversation

@LalitDeore
Copy link
Collaborator

  1. While deleting app delete app related files and cloud function
  2. Run all app operations when app is created in health api instead of stopping it on failure.

@0x0elliot
Copy link
Member

spooky PR

// set the body
req.Body = ioutil.NopCloser(bytes.NewBuffer(reqBodyJson))
reqBody := map[string]string{"execution_id": executionData.Id, "authorization": executionData.Authorization}
reqBodyJson, _ := json.Marshal(reqBody)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't discard the error, i want us to track these.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is request body not response body. We are logging every error. Will verify again in case we are not logging erros :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oop right, missed that.

if err != nil {
log.Printf("[ERROR] Failed creating HTTP for app delete request: %s", err)
return appHealth, err
delClient := &http.Client{Timeout: 60 * time.Second}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you sure you want this to stay to 60 seconds? the rest are bumped to 120

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, delete app api triggers cloud resource deletion in background. It removes the app from the datastore and returns the response, which is fast as compared to other. We should optimize other apis in same way :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, i trust you on this then

_, err = cloudfunctions.NewProjectsLocationsFunctionsService(service).Delete(functionPath).Do()
if err != nil {
// 404 means the function was never deployed or was already deleted — not an error.
if strings.Contains(err.Error(), "404") || strings.Contains(strings.ToLower(err.Error()), "not found") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i hate the way we are doing error matching here, this works for now, but if we could do status code checking through the objects directly, things would feel better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, figure out a way to do this cleanly in this iteration itself.

a quick search presents:

var apiErr *googleapi.Error                                                                                                     
if errors.As(err, &apiErr) && apiErr.Code == 404 {      

let's clean this codebase :)

// Delete extra_specs/{app.ID} — used when app data is too large for datastore.
extraSpecsPath := fmt.Sprintf("extra_specs/%s", app.ID)
if delErr := bucket.Object(extraSpecsPath).Delete(ctx); delErr != nil {
if !strings.Contains(delErr.Error(), "doesn't exist") && !strings.Contains(delErr.Error(), "no such object") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use if !errors.Is(delErr, storage.ErrObjectNotExist) { instead? remember, we are trying to make the health code easier to look at as well.

if len(app.ReferenceUrl) > 0 && project.Environment == "cloud" {
appCopy := *app
go func() {
if err := deleteAppCloudResources(context.Background(), &appCopy); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a timeout for this? feel free to add retries + logging for resilience.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a timeout? These GCP operations run in the background. The API only triggers the function, starts the process, and deletes the app from the datastore. The Cloud Function and app bucket files continue to be deleted in the background.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wouldn't want to trust GCP. timeouts and retries make the integration more graceful for us.

// names contain the MD5 hash — handles case differences between identifier
// (mixed case, used for filenames) and functionName (always lowercase).
for _, prefix := range []string{"generated_cloudfunctions/", "generated_apps/"} {
it := bucket.Objects(ctx, &storage.Query{Prefix: prefix})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're going to end up iterating through a lot. can you not use a more specific prefix?

something like app name with dashes stripped out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. Let me check if there is any better way to do this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just see how we format app names. would make your life easier.

@@ -385,62 +390,42 @@ func RunOpsAppHealthCheck(apiKey string, orgId string) (AppHealth, error) {

executeBodyJSON, err := json.Marshal(executeBody)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this becomes very nested very soon. handle it in a clean helper function pls

@@ -1567,148 +1574,144 @@ func RunOpsAppUpload(apiKey string, orgId string) (AppHealth, error) {

executeBodyJSON, err := json.Marshal(executeBody)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a similar helper function if possible. this gets confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants