summaryrefslogtreecommitdiff
path: root/manager/manager.go
blob: 5bbd49fff3bf81d7bab203977d9de05c04733b21 (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
package manager

import (
	"fmt"
	"sync"

	"github.com/Shugyousha/stasher/filter"
	"github.com/Shugyousha/stasher/input"
	"github.com/Shugyousha/stasher/output"
	"github.com/Shugyousha/stasher/work"
)

type Manager struct {
	Input  input.Input
	Filter filter.Filter
	Output output.Output
}

func (m *Manager) Run() {
	var wg sync.WaitGroup

	ic := m.Input.Start()
	for w := range ic {
		if w.Err != nil {
			fmt.Printf("Got an error when getting Work input: %q\n", w.Err)
			continue
		}
		wg.Add(1)

		go func(w *work.Work) {
			nw := m.Filter.Filter(w)
			err := nw.Error()
			if err != nil {
				fmt.Printf("Got an error when filtering Work: %q\n", err)
			}
			err = m.Output.Output(nw)
			if err != nil {
				fmt.Printf("Got an error when outputting Work: %q\n", err)
			}

			wg.Done()
		}(w)
	}

	wg.Wait()
}