Skip to content

Commit

Permalink
Fix: Add proper onedrive sync status to api
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Sep 27, 2024
1 parent 7ebfaf8 commit ac8b6fc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apiclient/types/onedrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type OneDriveLinks struct {
SharedLinks []string `json:"sharedLinks,omitempty"`
ThreadID string `json:"threadID,omitempty"`
RunID string `json:"runID,omitempty"`
Status string `json:"output,omitempty"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
Folders FolderSet `json:"folders,omitempty"`
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/controller/handlers/uploads/onedrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"errors"
"fmt"
"path/filepath"
"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 @@ -180,11 +182,51 @@ 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 Down
5 changes: 3 additions & 2 deletions pkg/storage/apis/otto.gptscript.ai/v1/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ type OnedriveLinksStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
ThreadName string `json:"threadName,omitempty"`
RunName string `json:"runName,omitempty"`
Status string `json:"output,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"`
}

type OneDriveLinksConnectorStatus struct {
Status string `json:"output,omitempty"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
Files map[string]types.FileDetails `json:"files,omitempty"`
Folders types.FolderSet `json:"folders,omitempty"`
Expand Down
12 changes: 9 additions & 3 deletions pkg/storage/openapi/generated/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac8b6fc

Please sign in to comment.