Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload debug file and delete it afterwards #214

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading