signal fixed on multiple projects

This commit is contained in:
asoseil 2017-12-04 23:39:27 +01:00
parent 311a467f06
commit 07632ecc6b
5 changed files with 34 additions and 39 deletions

View File

@ -1116,25 +1116,10 @@ func setup(c *cli.Context) (err error) {
// Start realize workflow
func start(c *cli.Context) (err error) {
r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host}
// config and start server
if c.Bool("server") || r.Server.Status {
r.Server.Status = true
if c.Bool("open") || r.Server.Open {
r.Server.Open = true
r.Server.OpenURL()
}
err = r.Server.Start()
if err != nil {
return err
}
}
// check no-config and read
if !c.Bool("no-config") {
// read a config if exist
err = r.Settings.Read(&r)
if err != nil {
return err
}
r.Settings.Read(&r)
if c.String("name") != "" {
// filter by name flag if exist
r.Schema.Projects = r.Schema.Filter("Name", c.String("name"))
@ -1147,6 +1132,18 @@ func start(c *cli.Context) (err error) {
}
}
// config and start server
if c.Bool("server") || r.Server.Status {
r.Server.Status = true
if c.Bool("open") || r.Server.Open {
r.Server.Open = true
r.Server.OpenURL()
}
err = r.Server.Start()
if err != nil {
return err
}
}
// check project list length
if len(r.Schema.Projects) <= 0 {
// create a new project based on given params

View File

@ -37,12 +37,11 @@ type (
Server Server `yaml:"server" json:"server"`
Schema `yaml:",inline" json:",inline"`
Sync chan string `yaml:"-" json:"-"`
exit chan os.Signal
Err Func `yaml:"-" json:"-"`
After Func `yaml:"-" json:"-"`
Before Func `yaml:"-" json:"-"`
Change Func `yaml:"-" json:"-"`
Reload Func `yaml:"-" json:"-"`
Err Func `yaml:"-" json:"-"`
After Func `yaml:"-" json:"-"`
Before Func `yaml:"-" json:"-"`
Change Func `yaml:"-" json:"-"`
Reload Func `yaml:"-" json:"-"`
}
// Context is used as argument for func
@ -73,22 +72,22 @@ func init() {
// Stop realize workflow
func (r *Realize) Stop() error {
if r.exit != nil {
close(r.exit)
return nil
} else {
return errors.New("exit chan undefined")
for k := range r.Schema.Projects {
if r.Schema.Projects[k].exit != nil {
close(r.Schema.Projects[k].exit)
}
}
return nil
}
// Start realize workflow
func (r *Realize) Start() error {
if len(r.Schema.Projects) > 0 {
var wg sync.WaitGroup
r.exit = make(chan os.Signal, 1)
signal.Notify(r.exit, os.Interrupt)
wg.Add(len(r.Schema.Projects))
for k := range r.Schema.Projects {
r.Schema.Projects[k].exit = make(chan os.Signal, 1)
signal.Notify(r.Schema.Projects[k].exit, os.Interrupt)
r.Schema.Projects[k].parent = r
go r.Schema.Projects[k].Watch(&wg)
}

View File

@ -11,9 +11,9 @@ import (
func TestRealize_Stop(t *testing.T) {
r := Realize{}
r.exit = make(chan os.Signal, 2)
r.Projects = append(r.Schema.Projects, Project{exit: make(chan os.Signal, 1)})
r.Stop()
_, ok := <-r.exit
_, ok := <-r.Projects[0].exit
if ok != false {
t.Error("Unexpected error", "channel should be closed")
}
@ -25,11 +25,11 @@ func TestRealize_Start(t *testing.T) {
if err == nil {
t.Error("Error expected")
}
r.Projects = append(r.Projects, Project{Name: "test"})
r.Projects = append(r.Projects, Project{Name: "test", exit: make(chan os.Signal, 1)})
go func() {
time.Sleep(100)
close(r.exit)
_, ok := <-r.exit
close(r.Projects[0].exit)
_, ok := <-r.Projects[0].exit
if ok != false {
t.Error("Unexpected error", "channel should be closed")
}

View File

@ -47,6 +47,7 @@ type Project struct {
parent *Realize
watcher FileWatcher
init bool
exit chan os.Signal
stop chan bool
files int64
folders int64
@ -332,7 +333,7 @@ L:
}
case err := <-p.watcher.Errors():
p.Err(err)
case <-p.parent.exit:
case <-p.exit:
p.After()
break L
}

View File

@ -6,7 +6,6 @@ import (
"github.com/fsnotify/fsnotify"
"log"
"os"
"os/signal"
"strings"
"sync"
"testing"
@ -142,12 +141,11 @@ func TestProject_Watch(t *testing.T) {
r := Realize{}
r.Projects = append(r.Projects, Project{
parent: &r,
exit: make(chan os.Signal, 1),
})
r.exit = make(chan os.Signal, 2)
signal.Notify(r.exit, os.Interrupt)
go func() {
time.Sleep(100)
close(r.exit)
close(r.Projects[0].exit)
}()
wg.Add(1)
// test before after and file change