gitlab.com/picnic-app/backend/role-api@v0.0.0-20230614140944-06a76ff3696d/internal/util/background/context.go (about)

     1  package background
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"go.uber.org/zap"
     8  
     9  	"gitlab.com/picnic-app/backend/libs/golang/logger"
    10  )
    11  
    12  // Context copies all values from the context. Such a context can
    13  // be used to run background tasks, as it does not contain deadlines and cannot
    14  // be canceled by the parent context.
    15  func Context(ctx context.Context) context.Context {
    16  	log := logger.FromContext(ctx).With(zap.Bool("background", true))
    17  	ctx = logger.ToContext(ctx, log)
    18  	return &copyValuesCtx{ctx: ctx}
    19  }
    20  
    21  type copyValuesCtx struct{ ctx context.Context }
    22  
    23  func (*copyValuesCtx) Err() error                       { return nil }
    24  func (*copyValuesCtx) Done() <-chan struct{}            { return nil }
    25  func (*copyValuesCtx) Deadline() (v time.Time, ok bool) { return v, ok }
    26  func (c *copyValuesCtx) Value(key any) any              { return c.ctx.Value(key) }