From 7bf1b9a8229c19447bf39fd33fc544c74b927098 Mon Sep 17 00:00:00 2001 From: frikky Date: Sun, 28 Mar 2021 16:40:08 +0200 Subject: [PATCH] Better caching for webhooks --- db-connector.go | 15 +++++++-------- shared.go | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/db-connector.go b/db-connector.go index ed0eed0..b103719 100644 --- a/db-connector.go +++ b/db-connector.go @@ -1329,24 +1329,23 @@ func GetHook(ctx context.Context, hookId string) (*Hook, error) { cacheData := []byte(cache.([]uint8)) //log.Printf("CACHEDATA: %#v", cacheData) err = json.Unmarshal(cacheData, &hook) - if err == nil { + if err == nil && len(hook.Id) > 0 { return hook, nil + } else { + return hook, errors.New(fmt.Sprintf("Bad cache for %s", hookId)) } } else { log.Printf("[INFO] Failed getting cache for hook: %s", err) } } - key := datastore.NameKey(nameKey, strings.ToLower(hookId), nil) - if err := project.Dbclient.Get(ctx, key, hook); err != nil { - return &Hook{}, err - } - + key := datastore.NameKey(nameKey, hookId, nil) + dbErr := project.Dbclient.Get(ctx, key, hook) if project.CacheDb { hookData, err := json.Marshal(hook) if err != nil { log.Printf("[WARNING] Failed marshalling in gethook: %s", err) - return hook, nil + return hook, dbErr } err = SetCache(ctx, cacheKey, hookData) @@ -1355,7 +1354,7 @@ func GetHook(ctx context.Context, hookId string) (*Hook, error) { } } - return hook, nil + return hook, dbErr } func SetHook(ctx context.Context, hook Hook) error { diff --git a/shared.go b/shared.go index 3eb4b46..6b25f85 100644 --- a/shared.go +++ b/shared.go @@ -1235,7 +1235,7 @@ func GetWorkflowExecutions(resp http.ResponseWriter, request *http.Request) { ctx := getContext(request) workflow, err := GetWorkflow(ctx, fileId) if err != nil { - log.Printf("Failed getting the workflow %s locally (get executions): %s", fileId, err) + log.Printf("[WARNING] Failed getting the workflow %s locally (get executions): %s", fileId, err) resp.WriteHeader(401) resp.Write([]byte(`{"success": false}`)) return @@ -2164,7 +2164,7 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) { if tmpworkflow.OrgId == user.ActiveOrg.Id && user.Role == "admin" { log.Printf("[INFO] User %s is accessing %s executions as admin", user.Username, tmpworkflow.ID) } else if tmpworkflow.Public { - log.Printf("\n\nSHOULD CREATE A NEW WORKFLOW FOR THE USER :O\n\n") + //log.Printf("\n\nSHOULD CREATE A NEW WORKFLOW FOR THE USER :O\n\n") workflow = *tmpworkflow workflow.ID = uuid.NewV4().String()