Skip to content

Commit

Permalink
Better caching for webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed Mar 28, 2021
1 parent 225713b commit 7bf1b9a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 7 additions & 8 deletions db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7bf1b9a

Please sign in to comment.