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 package goldsmith
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/fatih/color"
) )
type goldsmith struct { type goldsmith struct {
srcDir, dstDir string srcDir, dstDir string
refs map[string]bool
mtx sync.Mutex
stages []*stage stages []*stage
active int64 active int64
stalled int64 stalled int64
refs map[string]bool
refMtx sync.Mutex
tainted bool tainted bool
faultMtx sync.Mutex
} }
func (gs *goldsmith) queueFiles(target uint) { func (gs *goldsmith) queueFiles(target uint) {
@ -125,8 +130,8 @@ func (gs *goldsmith) exportFile(f *file) error {
} }
func (gs *goldsmith) referenceFile(path string) { func (gs *goldsmith) referenceFile(path string) {
gs.mtx.Lock() gs.refMtx.Lock()
defer gs.mtx.Unlock() defer gs.refMtx.Unlock()
if gs.refs == nil { if gs.refs == nil {
gs.refs = make(map[string]bool) 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) { func (gs *goldsmith) fault(s *stage, step string, f *file, err error) {
log.Printf("%s\t%s\t%s", s.name, f.path, err) 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 gs.tainted = true
} }

View File

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

View File

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