summaryrefslogtreecommitdiff
path: root/jwg.go
blob: 2bde2090ee3121ca19f91f00eeac0eaea477b059 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main

import (
	"fmt"
	"os"
	"sort"
	"strings"
	"sync"
	"time"
)

type JobWaitGroup struct {
	lock sync.RWMutex
	jobs map[string]time.Duration
	wg   sync.WaitGroup
}

func (jwg *JobWaitGroup) Do(name string, fn func()) {
	jwg.lock.Lock()
	defer jwg.lock.Unlock()

	if jwg.jobs == nil {
		jwg.jobs = make(map[string]time.Duration)
	}
	if _, dup := jwg.jobs[name]; dup {
		panic(fmt.Sprintf("job %q already exists", name))
	}
	jwg.jobs[name] = -1
	jwg.wg.Add(1)
	start := time.Now()
	go func() {
		defer func() {
			jwg.lock.Lock()
			defer jwg.lock.Unlock()
			jwg.jobs[name] = time.Now().Sub(start)
			jwg.wg.Done()
		}()
		fn()
	}()
}

func (jwg *JobWaitGroup) Wait() {
	jwg.wg.Wait()
}

func (jwg *JobWaitGroup) Status() (int, []string) {
	jwg.lock.RLock()

	n := len(jwg.jobs)
	var jobs []string
	for job, duration := range jwg.jobs {
		if duration < 0 {
			jobs = append(jobs, job)
		}
	}
	defer jwg.lock.RUnlock()
	sort.Strings(jobs)

	return n, jobs
}

func (jwg *JobWaitGroup) Watch(d time.Duration) {
	ticker := time.NewTicker(d)
	done := make(chan struct{})
	go func() {
		jwg.Wait()
		ticker.Stop()
		done <- struct{}{}
	}()
	for {
		select {
		case <-ticker.C:
			n, jobs := jwg.Status()
			if len(jobs) == 0 {
				panic("no active jobs, but wg still waiting")
			}
			line := fmt.Sprintf("%d/%d : %v", len(jobs), n, strings.Join(jobs, ", "))
			if len(line) > 70 {
				line = line[:67] + "..."
			}
			fmt.Fprintf(os.Stderr, "\r%-70s", line)
		case <-done:
			fmt.Fprintf(os.Stderr, "\r%-70s\n", "done")
			jwg.lock.RLock()
			defer jwg.lock.RUnlock()
			s := newSortHelper(jwg.jobs)
			sort.Sort(s)
			for _, job := range s.StringSlice {
				fmt.Fprintln(os.Stderr, s.times[job], job)
			}
			return
		}
	}
}

type sortHelper struct {
	times map[string]time.Duration
	sort.StringSlice
}

func (s sortHelper) Less(i, j int) bool {
	return s.times[s.StringSlice[i]] < s.times[s.StringSlice[j]]
}

func newSortHelper(jobs map[string]time.Duration) sortHelper {
	slice := make([]string, 0, len(jobs))
	for job := range jobs {
		slice = append(slice, job)
	}
	return sortHelper{times: jobs, StringSlice: slice}
}