concurrency + waiting

This commit is contained in:
alessio 2016-08-01 01:52:11 +02:00
parent a589ad57a2
commit 2358f76e65

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"log" "log"
"strings" "strings"
"sync"
) )
func InArray(str string, list []string) bool{ func InArray(str string, list []string) bool{
@ -31,10 +32,9 @@ func (h *Config) Watch() error{
var current Watcher var current Watcher
watcher, err := fsnotify.NewWatcher() var wg sync.WaitGroup
if err != nil{
fmt.Println(err) var watcher *fsnotify.Watcher
}
walk := func(path string, info os.FileInfo, err error) error{ walk := func(path string, info os.FileInfo, err error) error{
if !Ignore(path,current.Ignore) { if !Ignore(path,current.Ignore) {
@ -51,7 +51,16 @@ func (h *Config) Watch() error{
return nil return nil
} }
watch := func(){ watch := func(val Project){
watcher, _ = fsnotify.NewWatcher()
for _, dir := range val.Watcher.Paths {
path, _ := os.Getwd()
current = val.Watcher
if err := filepath.Walk(path + dir, walk); err != nil {
fmt.Println(err)
}
}
for { for {
select { select {
case event := <-watcher.Events: case event := <-watcher.Events:
@ -63,35 +72,20 @@ func (h *Config) Watch() error{
log.Println("error:", err) log.Println("error:", err)
} }
} }
}
defer func(){ wg.Done()
watcher.Close() watcher.Close()
// kill process }
}()
// add to watcher // add to watcher
if err := h.Read(); err == nil { if err := h.Read(); err == nil {
// loop projects // loop projects
wg.Add(len(h.Projects))
for _, val := range h.Projects { for _, val := range h.Projects {
// add paths go watch(val)
for _, dir := range val.Watcher.Paths {
path, _ := os.Getwd()
current = val.Watcher
if err := filepath.Walk(path+dir, walk); err != nil{
fmt.Println(err)
} }
} wg.Wait()
}
// watch changes
watch()
// build
// install
// run
return nil return nil
}else{ }else{