From 07632ecc6b814d98c16ca42bf48c04321c1836d5 Mon Sep 17 00:00:00 2001 From: asoseil Date: Mon, 4 Dec 2017 23:39:27 +0100 Subject: [PATCH] signal fixed on multiple projects --- realize.go | 29 +++++++++++++---------------- realize/cli.go | 25 ++++++++++++------------- realize/cli_test.go | 10 +++++----- realize/projects.go | 3 ++- realize/projects_test.go | 6 ++---- 5 files changed, 34 insertions(+), 39 deletions(-) diff --git a/realize.go b/realize.go index 50fe112..333dde0 100644 --- a/realize.go +++ b/realize.go @@ -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 diff --git a/realize/cli.go b/realize/cli.go index 45dc53a..d8d29a6 100644 --- a/realize/cli.go +++ b/realize/cli.go @@ -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) } diff --git a/realize/cli_test.go b/realize/cli_test.go index 406826c..cd19236 100644 --- a/realize/cli_test.go +++ b/realize/cli_test.go @@ -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") } diff --git a/realize/projects.go b/realize/projects.go index a2c2064..60a8684 100644 --- a/realize/projects.go +++ b/realize/projects.go @@ -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 } diff --git a/realize/projects_test.go b/realize/projects_test.go index 49ab4a5..b4a3e22 100644 --- a/realize/projects_test.go +++ b/realize/projects_test.go @@ -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