Skip to content

Commit

Permalink
Upload debug file and delete it afterwards (#214)
Browse files Browse the repository at this point in the history
* Upload debug file and delete it afterwards

* Get folder location from task helper

* Delete the log file after it is uploaded

* Return any error from `os.Stat` that is not «file not found»
  • Loading branch information
shackra authored Aug 22, 2024
1 parent d8b900d commit 581a064
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/tasks/c1api/full_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"os"
"path/filepath"

v1 "github.com/conductorone/baton-sdk/pb/c1/connectorapi/baton/v1"
"github.com/conductorone/baton-sdk/pkg/annotations"
Expand Down Expand Up @@ -130,6 +131,11 @@ func (c *fullSyncTaskHandler) HandleTask(ctx context.Context) error {
return c.helpers.FinishTask(ctx, nil, nil, err)
}

err = uploadDebugLogs(ctx, c.helpers)
if err != nil {
return c.helpers.FinishTask(ctx, nil, nil, err)
}

return c.helpers.FinishTask(ctx, nil, nil, nil)
}

Expand All @@ -140,3 +146,39 @@ func newFullSyncTaskHandler(task *v1.Task, helpers fullSyncHelpers, skipFullSync
skipFullSync: skipFullSync,
}
}

func uploadDebugLogs(ctx context.Context, helper fullSyncHelpers) error {
l := ctxzap.Extract(ctx)

debugfilelocation := filepath.Join(helper.TempDir(), "debug.log")

_, err := os.Stat(debugfilelocation)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
l.Warn("debug log file does not exists", zap.Error(err))
return nil
}
return err
} else {
debugfile, err := os.Open(debugfilelocation)
if err != nil {
return err
}
defer debugfile.Close()

l.Info("uploading debug logs", zap.String("file", debugfilelocation))
err = helper.Upload(ctx, debugfile)

if err != nil {
return err
}
defer func() {
err := os.Remove(debugfilelocation)
if err != nil {
l.Error("failed to delete file with debug logs", zap.Error(err), zap.String("file", debugfilelocation))
}
}()

return nil
}
}

0 comments on commit 581a064

Please sign in to comment.