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 // Start realize workflow
func start(c *cli.Context) (err error) { func start(c *cli.Context) (err error) {
r.Server = realize.Server{Parent: &r, Status: false, Open: false, Port: realize.Port, Host: realize.Host} 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 // check no-config and read
if !c.Bool("no-config") { if !c.Bool("no-config") {
// read a config if exist // read a config if exist
err = r.Settings.Read(&r) r.Settings.Read(&r)
if err != nil {
return err
}
if c.String("name") != "" { if c.String("name") != "" {
// filter by name flag if exist // filter by name flag if exist
r.Schema.Projects = r.Schema.Filter("Name", c.String("name")) 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 // check project list length
if len(r.Schema.Projects) <= 0 { if len(r.Schema.Projects) <= 0 {
// create a new project based on given params // create a new project based on given params

View File

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

View File

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

View File

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

View File

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