Skip to content

Commit

Permalink
Address donnie's comment
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Sep 28, 2024
1 parent ac8b6fc commit 3b78453
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 44 deletions.
68 changes: 25 additions & 43 deletions pkg/controller/handlers/uploads/onedrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/acorn-io/baaah/pkg/apply"
"github.com/acorn-io/baaah/pkg/log"
"github.com/acorn-io/baaah/pkg/router"
"github.com/acorn-io/baaah/pkg/uncached"
"github.com/gptscript-ai/otto/apiclient/types"
Expand Down Expand Up @@ -182,51 +181,11 @@ func (u *UploadHandler) RunUpload(req router.Request, _ router.Response) error {
return err
}

ctx, cancel := context.WithCancel(context.Background())

go func() {
// Don't care about the events here, but we need to pull them out
r.Wait()
cancel()
}()

go func(ctx context.Context) {

ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
file, err := u.workspaceClient.OpenFile(req.Ctx, thread.Spec.WorkspaceID, ".metadata.json")
if err != nil {
log.Errorf("failed to open metadata file: %v", err)
continue
}
defer file.Close()

var output map[string]v1.OneDriveLinksConnectorStatus
if err = json.NewDecoder(file).Decode(&output); err != nil {
log.Errorf("failed to decode metadata file: %v", err)
continue
}
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
if err := req.Get(oneDriveLinks, oneDriveLinks.Namespace, oneDriveLinks.Name); err != nil {
return err
}
oneDriveLinks.Status.Status = output["output"].Status
oneDriveLinks.Status.Error = output["output"].Error
return req.Client.Status().Update(req.Ctx, oneDriveLinks)
}); err != nil {
log.Errorf("failed to update OneDriveLinks status: %v", err)
}
}
}

}(ctx)

oneDriveLinks.Status.RunName = r.Run.Name
oneDriveLinks.Status.LastReSyncStarted = metav1.Now()
return nil
Expand All @@ -244,11 +203,34 @@ func (u *UploadHandler) HandleUploadRun(req router.Request, resp router.Response

var run v1.Run
if err := req.Get(&run, oneDriveLinks.Namespace, oneDriveLinks.Status.RunName); apierrors.IsNotFound(err) {
// Might not be in the cache yet.
return nil
} else if err != nil || !run.Status.State.IsTerminal() {
} else if err != nil {
return err
}
if !run.Status.State.IsTerminal() {
var thread v1.Thread
if err := req.Get(&thread, oneDriveLinks.Namespace, oneDriveLinks.Status.ThreadName); apierrors.IsNotFound(err) {
// Might not be in the cache yet.
return nil
} else if err != nil {
return err
}

file, err := u.workspaceClient.OpenFile(req.Ctx, thread.Spec.WorkspaceID, ".metadata.json")
if err != nil {
return err
}
defer file.Close()

var output map[string]v1.OneDriveLinksConnectorStatus
if err = json.NewDecoder(file).Decode(&output); err != nil {
return err
}
oneDriveLinks.Status.Status = output["output"].Status
oneDriveLinks.Status.Error = output["output"].Error
resp.RetryAfter(5 * time.Second)
return nil
}

var thread v1.Thread
if err := req.Get(&thread, oneDriveLinks.Namespace, oneDriveLinks.Status.ThreadName); err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/storage/apis/otto.gptscript.ai/v1/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type OnedriveLinksStatus struct {
RunName string `json:"runName,omitempty"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
AuthURL string `json:"authUrl,omitempty"`
Folders types.FolderSet `json:"folders,omitempty"`
LastReSyncStarted metav1.Time `json:"lastReSyncStarted,omitempty"`
}
Expand Down

0 comments on commit 3b78453

Please sign in to comment.