diff options
-rw-r--r-- | jwg.go | 56 | ||||
-rw-r--r-- | main.go | 52 |
2 files changed, 53 insertions, 55 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} -} @@ -6,6 +6,7 @@ import ( "net" "os" "path/filepath" + "sort" "strings" "sync" "time" @@ -122,5 +123,54 @@ func main() { for _, fname := range os.Args[1:] { DoHostfile(fname) } - jwg.Watch(time.Second) + watch(time.Second) +} + +func watch(d time.Duration) { + ticker := time.NewTicker(d) + done := make(chan map[string]time.Duration) + go func() { + jobs := jwg.Wait() + ticker.Stop() + done <- jobs + }() + 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 jobs := <-done: + fmt.Fprintf(os.Stderr, "\r%-70s\n", "done") + s := newSortHelper(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} } |