Simplification

This commit is contained in:
Alex Yatskov 2015-12-18 20:30:14 +09:00
parent 570c65db08
commit 12e62912c8
3 changed files with 29 additions and 19 deletions

View File

@ -23,22 +23,27 @@
package goldsmith
import (
"log"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/fatih/color"
)
type goldsmith struct {
srcDir, dstDir string
refs map[string]bool
mtx sync.Mutex
stages []*stage
active int64
stalled int64
refs map[string]bool
refMtx sync.Mutex
tainted bool
faultMtx sync.Mutex
}
func (gs *goldsmith) queueFiles(target uint) {
@ -125,8 +130,8 @@ func (gs *goldsmith) exportFile(f *file) error {
}
func (gs *goldsmith) referenceFile(path string) {
gs.mtx.Lock()
defer gs.mtx.Unlock()
gs.refMtx.Lock()
defer gs.refMtx.Unlock()
if gs.refs == nil {
gs.refs = make(map[string]bool)
@ -144,8 +149,16 @@ func (gs *goldsmith) referenceFile(path string) {
}
}
func (gs *goldsmith) fault(s *stage, f *file, err error) {
log.Printf("%s\t%s\t%s", s.name, f.path, err)
func (gs *goldsmith) fault(s *stage, step string, f *file, err error) {
gs.faultMtx.Lock()
defer gs.faultMtx.Unlock()
color.Red("Fault Detected\n")
color.Yellow("\tPlugin:\t%s\n", color.WhiteString(s.name))
color.Yellow("\tStep:\t%s\n", color.WhiteString(step))
color.Yellow("\tFile:\t%s\n", color.WhiteString(f.path))
color.Yellow("\tError:\t%s\n\n", color.WhiteString(err.Error()))
gs.tainted = true
}

View File

@ -45,6 +45,7 @@ func newStage(gs *goldsmith) *stage {
func (s *stage) chain(p Plugin) {
defer close(s.output)
s.name = p.Name()
init, _ := p.(Initializer)
accept, _ := p.(Accepter)
@ -71,7 +72,7 @@ func (s *stage) chain(p Plugin) {
if init != nil {
if err := init.Initialize(s); err != nil {
s.gs.fault(s, nil, err)
s.gs.fault(s, "Initialization", nil, err)
return
}
}
@ -88,7 +89,7 @@ func (s *stage) chain(p Plugin) {
f.rewind()
if err := proc.Process(s, f); err != nil {
s.gs.fault(s, f, err)
s.gs.fault(s, "Processing", f, err)
}
dispatch(f)
@ -100,7 +101,7 @@ func (s *stage) chain(p Plugin) {
if fin != nil {
if err := fin.Finalize(s, batch); err != nil {
s.gs.fault(s, nil, err)
s.gs.fault(s, "Finalization", nil, err)
}
for _, f := range batch {

View File

@ -34,17 +34,13 @@ type Goldsmith interface {
}
func New(srcDir, dstDir string) Goldsmith {
return NewThrottled(srcDir, dstDir, uint(runtime.NumCPU()))
}
func NewThrottled(srcDir, dstDir string, targetFileCount uint) Goldsmith {
gs := &goldsmith{
srcDir: srcDir,
dstDir: dstDir,
refs: make(map[string]bool),
}
gs.queueFiles(targetFileCount)
gs.queueFiles(uint(runtime.NumCPU()))
return gs
}