Skip to content

Commit

Permalink
Clean the RAG on instance deletion (#4482)
Browse files Browse the repository at this point in the history
  • Loading branch information
nono authored Oct 29, 2024
2 parents c150b33 + f8cc145 commit f2093b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions model/instance/lifecycle/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cozy/cozy-stack/model/app"
"github.com/cozy/cozy-stack/model/instance"
job "github.com/cozy/cozy-stack/model/job"
"github.com/cozy/cozy-stack/model/rag"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
Expand Down Expand Up @@ -76,6 +77,10 @@ func Destroy(domain string) error {
return err
}

if err := rag.CleanInstance(inst); err != nil {
return err
}

// Reload the instance, it can have been updated in CouchDB if the instance
// had at least one account and was not up-to-date for its indexes/views.
inst, err = instance.Get(domain)
Expand Down
26 changes: 26 additions & 0 deletions model/rag/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,29 @@ func pushJob(inst *instance.Instance, doctype string) error {
})
return err
}

func CleanInstance(inst *instance.Instance) error {
ragServer := inst.RAGServer()
if ragServer.URL == "" {
return nil
}
u, err := url.Parse(ragServer.URL)
if err != nil {
return err
}
u.Path = fmt.Sprintf("/instances/%s", inst.Domain)
req, err := http.NewRequest(http.MethodDelete, u.String(), nil)
if err != nil {
return err
}
req.Header.Add(echo.HeaderAuthorization, "Bearer "+ragServer.APIKey)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode >= 500 {
return fmt.Errorf("DELETE status code: %d", res.StatusCode)
}
return nil
}

0 comments on commit f2093b6

Please sign in to comment.