diff options
Diffstat (limited to 'jwg.go')
-rw-r--r-- | jwg.go | 56 |
1 files changed, 2 insertions, 54 deletions
@@ -2,9 +2,7 @@ package main import ( "fmt" - "os" "sort" - "strings" "sync" "time" ) @@ -39,8 +37,9 @@ func (jwg *JobWaitGroup) Do(name string, fn func()) { }() } -func (jwg *JobWaitGroup) Wait() { +func (jwg *JobWaitGroup) Wait() map[string]time.Duration { jwg.wg.Wait() + return jwg.jobs } func (jwg *JobWaitGroup) Status() (int, []string) { @@ -58,54 +57,3 @@ func (jwg *JobWaitGroup) Status() (int, []string) { 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} -} |