diff --git a/client/internal/engine.go b/client/internal/engine.go index 54cfa7892..d5f38e1b4 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -1060,7 +1060,9 @@ func (e *Engine) handleBundle(params *mgmProto.BundleParameters) (*mgmProto.JobR LogFileCount: uint32(params.LogFileCount), } - uploadKey, err := e.jobExecutor.BundleJob(e.ctx, bundleDeps, bundleJobParams, e.config.ProfileConfig.ManagementURL.String()) + waitFor := time.Duration(params.BundleForTime) * time.Minute + + uploadKey, err := e.jobExecutor.BundleJob(e.ctx, bundleDeps, bundleJobParams, waitFor, e.config.ProfileConfig.ManagementURL.String()) if err != nil { return nil, err } diff --git a/client/jobexec/executor.go b/client/jobexec/executor.go index 3269bde61..a1e0a82a0 100644 --- a/client/jobexec/executor.go +++ b/client/jobexec/executor.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "time" log "github.com/sirupsen/logrus" @@ -11,6 +12,10 @@ import ( "github.com/netbirdio/netbird/upload-server/types" ) +const ( + MaxBundleWaitTime = 60 * time.Minute // maximum wait time for bundle generation (1 hour) +) + var ( ErrJobNotImplemented = errors.New("job not implemented") ) @@ -22,8 +27,18 @@ func NewExecutor() *Executor { return &Executor{} } -func (e *Executor) BundleJob(ctx context.Context, debugBundleDependencies debug.GeneratorDependencies, params debug.BundleConfig, mgmURL string) (string, error) { +func (e *Executor) BundleJob(ctx context.Context, debugBundleDependencies debug.GeneratorDependencies, params debug.BundleConfig, waitForDuration time.Duration, mgmURL string) (string, error) { + if waitForDuration > MaxBundleWaitTime { + log.Warnf("bundle wait time %v exceeds maximum %v, capping to maximum", waitFor, MaxBundleWaitTime) + waitForDuration = MaxBundleWaitTime + } + + if waitForDuration > 0 { + waitFor(ctx, waitForDuration) + } + log.Infof("execute debug bundle generation") + bundleGenerator := debug.NewBundleGenerator(debugBundleDependencies, params) path, err := bundleGenerator.Generate() @@ -40,3 +55,12 @@ func (e *Executor) BundleJob(ctx context.Context, debugBundleDependencies debug. log.Infof("debug bundle has been generated well") return key, nil } + +func waitFor(ctx context.Context, duration time.Duration) { + log.Infof("wait for %v minutes before executing debug bundle", duration.Minutes()) + select { + case <-time.After(duration): + case <-ctx.Done(): + log.Infof("wait cancelled: %v", ctx.Err()) + } +} diff --git a/shared/management/client/grpc.go b/shared/management/client/grpc.go index 1ab880d1b..af8f3cd74 100644 --- a/shared/management/client/grpc.go +++ b/shared/management/client/grpc.go @@ -204,7 +204,7 @@ func (c *GrpcClient) handleJobStream( continue } - log.WithContext(ctx).Infof("Received a new job from the management server (ID: %s)", jobReq.ID) + log.WithContext(ctx).Infof("received a new job from the management server (ID: %s)", jobReq.ID) jobResp := c.processJobRequest(ctx, jobReq, msgHandler) if err := c.sendJobResponse(ctx, stream, serverPubKey, jobResp); err != nil { return err