package tdsync

import (
	

	
	

	
)

// LogGroup is simple wrapper around CancellableGroup to log task state.
// Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.
type LogGroup struct {
	group CancellableGroup

	log   *zap.Logger
	clock clock.Clock
}

// NewLogGroup creates new LogGroup.
func ( context.Context,  *zap.Logger) *LogGroup {
	return &LogGroup{
		group: *NewCancellableGroup(),
		log:   ,
		clock: clock.System,
	}
}

// SetClock sets Clock to use.
func ( *LogGroup) ( clock.Clock) {
	.clock = 
}

// Go calls the given function in a new goroutine.
//
// The first call to return a non-nil error cancels the group; its error will be
// returned by Wait.
func ( *LogGroup) ( string,  func( context.Context) error) {
	.group.Go(func( context.Context) error {
		 := .clock.Now()
		 := .log.With(zap.String("task", )).WithOptions(zap.AddCallerSkip(1))
		.Debug("Task started")

		if  := ();  != nil {
			 := .clock.Now().Sub()
			.Debug("Task stopped", zap.Error(), zap.Duration("elapsed", ))
			return errors.Wrapf(, "task %s", )
		}

		 := .clock.Now().Sub()
		.Debug("Task complete", zap.Duration("elapsed", ))
		return nil
	})
}

// Cancel cancels all goroutines in group.
//
// Note: context cancellation error will be returned by Wait().
func ( *LogGroup) () {
	.group.Cancel()
}

// Wait blocks until all function calls from the Go method have returned, then
// returns the first non-nil error (if any) from them.
func ( *LogGroup) () error {
	return .group.Wait()
}