From cfb6ce894e8a44fcf133c794bf84bf77483525d7 Mon Sep 17 00:00:00 2001 From: Jorge Javier Araya Navarro Date: Tue, 20 Aug 2024 14:46:18 -0600 Subject: [PATCH 1/4] Upload debug file and delete it afterwards --- pkg/tasks/c1api/full_sync.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/tasks/c1api/full_sync.go b/pkg/tasks/c1api/full_sync.go index e5dd447e..289736eb 100644 --- a/pkg/tasks/c1api/full_sync.go +++ b/pkg/tasks/c1api/full_sync.go @@ -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" @@ -130,6 +131,12 @@ func (c *fullSyncTaskHandler) HandleTask(ctx context.Context) error { return c.helpers.FinishTask(ctx, nil, nil, err) } + debugfilepath := filepath.Join(c1zPath, "debug.log") + err = uploadDebugLogs(ctx, debugfilepath, c.helpers) + if err != nil { + return c.helpers.FinishTask(ctx, nil, nil, err) + } + return c.helpers.FinishTask(ctx, nil, nil, nil) } @@ -140,3 +147,31 @@ func newFullSyncTaskHandler(task *v1.Task, helpers fullSyncHelpers, skipFullSync skipFullSync: skipFullSync, } } + +func uploadDebugLogs(ctx context.Context, p string, helper fullSyncHelpers) error { + l := ctxzap.Extract(ctx) + _, err := os.Stat(p) + if err == nil { + debugfile, err := os.Open(p) + if err != nil { + return err + } + defer debugfile.Close() + defer func() { + err := os.Remove(p) + if err != nil { + l.Error("failed to remove debug logs after upload", zap.Error(err)) + } + }() + + err = helper.Upload(ctx, debugfile) + + if err != nil { + return err + } + } else if errors.Is(err, os.ErrNotExist) { + l.Warn("debug log file does not exists", zap.Error(err)) + } + + return nil +} From 49e553227c16f29c584486fca1a776f0d27ac6d2 Mon Sep 17 00:00:00 2001 From: Jorge Javier Araya Navarro Date: Wed, 21 Aug 2024 16:08:10 -0600 Subject: [PATCH 2/4] Get folder location from task helper --- pkg/tasks/c1api/full_sync.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/tasks/c1api/full_sync.go b/pkg/tasks/c1api/full_sync.go index 289736eb..50f15cde 100644 --- a/pkg/tasks/c1api/full_sync.go +++ b/pkg/tasks/c1api/full_sync.go @@ -131,8 +131,7 @@ func (c *fullSyncTaskHandler) HandleTask(ctx context.Context) error { return c.helpers.FinishTask(ctx, nil, nil, err) } - debugfilepath := filepath.Join(c1zPath, "debug.log") - err = uploadDebugLogs(ctx, debugfilepath, c.helpers) + err = uploadDebugLogs(ctx, c.helpers) if err != nil { return c.helpers.FinishTask(ctx, nil, nil, err) } @@ -148,22 +147,20 @@ func newFullSyncTaskHandler(task *v1.Task, helpers fullSyncHelpers, skipFullSync } } -func uploadDebugLogs(ctx context.Context, p string, helper fullSyncHelpers) error { +func uploadDebugLogs(ctx context.Context, helper fullSyncHelpers) error { l := ctxzap.Extract(ctx) - _, err := os.Stat(p) + + debugfilelocation := filepath.Join(helper.TempDir(), "debug.log") + + _, err := os.Stat(debugfilelocation) if err == nil { - debugfile, err := os.Open(p) + debugfile, err := os.Open(debugfilelocation) if err != nil { return err } defer debugfile.Close() - defer func() { - err := os.Remove(p) - if err != nil { - l.Error("failed to remove debug logs after upload", zap.Error(err)) - } - }() + l.Info("uploading debug logs", zap.String("file", debugfilelocation)) err = helper.Upload(ctx, debugfile) if err != nil { From 6dbb42a5251381fd1494daf607574d96afcdc286 Mon Sep 17 00:00:00 2001 From: Jorge Javier Araya Navarro Date: Wed, 21 Aug 2024 16:10:49 -0600 Subject: [PATCH 3/4] Delete the log file after it is uploaded --- pkg/tasks/c1api/full_sync.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/tasks/c1api/full_sync.go b/pkg/tasks/c1api/full_sync.go index 50f15cde..05e9d66d 100644 --- a/pkg/tasks/c1api/full_sync.go +++ b/pkg/tasks/c1api/full_sync.go @@ -166,6 +166,12 @@ func uploadDebugLogs(ctx context.Context, helper fullSyncHelpers) error { 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)) + } + }() } else if errors.Is(err, os.ErrNotExist) { l.Warn("debug log file does not exists", zap.Error(err)) } From 1fc1be21c8d93f137f9344483d3253792e3595ff Mon Sep 17 00:00:00 2001 From: Jorge Javier Araya Navarro Date: Thu, 22 Aug 2024 10:47:54 -0600 Subject: [PATCH 4/4] =?UTF-8?q?Return=20any=20error=20from=20`os.Stat`=20t?= =?UTF-8?q?hat=20is=20not=20=C2=ABfile=20not=20found=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/tasks/c1api/full_sync.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/tasks/c1api/full_sync.go b/pkg/tasks/c1api/full_sync.go index 05e9d66d..e214b769 100644 --- a/pkg/tasks/c1api/full_sync.go +++ b/pkg/tasks/c1api/full_sync.go @@ -153,7 +153,13 @@ func uploadDebugLogs(ctx context.Context, helper fullSyncHelpers) error { debugfilelocation := filepath.Join(helper.TempDir(), "debug.log") _, err := os.Stat(debugfilelocation) - if err == nil { + 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 @@ -172,9 +178,7 @@ func uploadDebugLogs(ctx context.Context, helper fullSyncHelpers) error { l.Error("failed to delete file with debug logs", zap.Error(err), zap.String("file", debugfilelocation)) } }() - } else if errors.Is(err, os.ErrNotExist) { - l.Warn("debug log file does not exists", zap.Error(err)) - } - return nil + return nil + } }