File: simple_task.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (67 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package tasklog

import (
	"fmt"
	"sync"
	"time"
)

// SimpleTask is in an implementation of tasklog.Task which prints out messages
// verbatim.
type SimpleTask struct {
	// ch is used to transmit task updates.
	ch chan *Update

	// wg is used to wait between closing the channel, and acknowledging
	// that the close-related operations have been completed by the
	// tasklog.Logger.
	wg *sync.WaitGroup
}

// NewSimpleTask returns a new *SimpleTask instance.
func NewSimpleTask() *SimpleTask {
	return &SimpleTask{
		ch: make(chan *Update),
		wg: new(sync.WaitGroup),
	}
}

// Log logs a string with no formatting verbs.
func (s *SimpleTask) Log(str string) {
	s.Logf(str)
}

// Logf logs some formatted string, which is interpreted according to the rules
// defined in package "fmt".
func (s *SimpleTask) Logf(str string, vals ...interface{}) {
	s.ch <- &Update{
		S:  fmt.Sprintf(str, vals...),
		At: time.Now(),
	}
}

// Complete notes that the task is completed by closing the Updates channel and
// yields the logger to the next Task. Complete blocks until the *tasklog.Logger
// has acknowledged completion of this task.
func (s *SimpleTask) Complete() {
	s.wg.Add(1)
	close(s.ch)
	s.wg.Wait()
}

// OnComplete implements an interface which receives a call to this method when
// the *tasklog.Logger has finished processing this task, but before it has
// accepted new tasks.
func (s *SimpleTask) OnComplete() {
	s.wg.Done()
}

// Updates implements Task.Updates and returns a channel of updates which is
// closed when Complete() is called.
func (s *SimpleTask) Updates() <-chan *Update {
	return s.ch
}

// Throttled implements Task.Throttled and returns false, indicating that this
// task is not throttled.
func (s *SimpleTask) Throttled() bool { return false }