github.com/git-lfs/git-lfs@v2.5.2+incompatible/tasklog/simple_task.go (about)

     1  package tasklog
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"time"
     7  )
     8  
     9  // SimpleTask is in an implementation of tasklog.Task which prints out messages
    10  // verbatim.
    11  type SimpleTask struct {
    12  	// ch is used to transmit task updates.
    13  	ch chan *Update
    14  
    15  	// wg is used to wait between closing the channel, and acknowledging
    16  	// that the close-related operations have been completed by the
    17  	// tasklog.Logger.
    18  	wg *sync.WaitGroup
    19  }
    20  
    21  // NewSimpleTask returns a new *SimpleTask instance.
    22  func NewSimpleTask() *SimpleTask {
    23  	return &SimpleTask{
    24  		ch: make(chan *Update),
    25  		wg: new(sync.WaitGroup),
    26  	}
    27  }
    28  
    29  // Log logs a string with no formatting verbs.
    30  func (s *SimpleTask) Log(str string) {
    31  	s.Logf(str)
    32  }
    33  
    34  // Logf logs some formatted string, which is interpreted according to the rules
    35  // defined in package "fmt".
    36  func (s *SimpleTask) Logf(str string, vals ...interface{}) {
    37  	s.ch <- &Update{
    38  		S:  fmt.Sprintf(str, vals...),
    39  		At: time.Now(),
    40  	}
    41  }
    42  
    43  // Complete notes that the task is completed by closing the Updates channel and
    44  // yields the logger to the next Task. Complete blocks until the *tasklog.Logger
    45  // has acknowledged completion of this task.
    46  func (s *SimpleTask) Complete() {
    47  	s.wg.Add(1)
    48  	close(s.ch)
    49  	s.wg.Wait()
    50  }
    51  
    52  // OnComplete implements an interface which receives a call to this method when
    53  // the *tasklog.Logger has finished processing this task, but before it has
    54  // accepted new tasks.
    55  func (s *SimpleTask) OnComplete() {
    56  	s.wg.Done()
    57  }
    58  
    59  // Updates implements Task.Updates and returns a channel of updates which is
    60  // closed when Complete() is called.
    61  func (s *SimpleTask) Updates() <-chan *Update {
    62  	return s.ch
    63  }
    64  
    65  // Throttled implements Task.Throttled and returns false, indicating that this
    66  // task is not throttled.
    67  func (s *SimpleTask) Throttled() bool { return false }