From ef524feaca48f9660ab0f71f99bddf4d853329af Mon Sep 17 00:00:00 2001 From: Danny Piper Date: Fri, 15 Mar 2024 18:06:28 +0000 Subject: [PATCH] fix: genereate image in new thread --- kube_cache/server/cache.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/kube_cache/server/cache.go b/kube_cache/server/cache.go index 02d67ca..373510d 100644 --- a/kube_cache/server/cache.go +++ b/kube_cache/server/cache.go @@ -143,23 +143,23 @@ func (s *Server) craft(c *gin.Context, id1, id2 string) (model.KubeRecipe, error return model.KubeRecipe{}, errors.New("Cannot generate kube recipe") } - image, err := s.Ai.GenerateDalleForKube(newKubeId) - if err != nil { - log.Printf("Error generating Dalle for kube: %s", err) - return model.KubeRecipe{}, errors.New("Error generating Dalle for kube") - } - - err = s.Database.SetKubeRecipe(kube1, kube2, newKubeId, image) - if err != nil { - log.Printf("Cannot save kube recipe: %s", err) - return model.KubeRecipe{}, errors.New("Cannot save kube recipe") - } - - recipe, err = s.Database.GetKubeRecipe(id1, id2) - if err != nil { - log.Printf("Cannot get kube recipe: %s", err) - return model.KubeRecipe{}, errors.New("Cannot get kube recipe") - } + log.Printf("Generated new kube: %s, generating image in new thread", newKubeId) + go func() { + image, err := s.Ai.GenerateDalleForKube(newKubeId) + if err != nil { + log.Printf("Error generating Dalle for kube: %s", err) + } + + err = s.Database.SetKubeRecipe(kube1, kube2, newKubeId, image) + if err != nil { + log.Printf("Cannot save kube recipe: %s", err) + } + + recipe, err = s.Database.GetKubeRecipe(id1, id2) + if err != nil { + log.Printf("Cannot get kube recipe: %s", err) + } + }() } else { s.Metrics.IncrementCacheHits()