diff --git a/hcl/gapi.go b/hcl/gapi.go index 161b07b3..50e4646b 100644 --- a/hcl/gapi.go +++ b/hcl/gapi.go @@ -24,7 +24,6 @@ import ( "github.com/purpleidea/mgmt/gapi" "github.com/purpleidea/mgmt/pgraph" - "github.com/purpleidea/mgmt/recwatch" "github.com/purpleidea/mgmt/resources" errwrap "github.com/pkg/errors" @@ -46,11 +45,10 @@ func init() { type GAPI struct { InputURI string - initialized bool - data gapi.Data - wg sync.WaitGroup - closeChan chan struct{} - configWatcher *recwatch.ConfigWatcher + initialized bool + data gapi.Data + wg sync.WaitGroup + closeChan chan struct{} } // Cli takes a cli.Context, and returns our GAPI if activated. All arguments @@ -103,8 +101,6 @@ func (obj *GAPI) Init(d gapi.Data) error { obj.data = d obj.closeChan = make(chan struct{}) obj.initialized = true - obj.configWatcher = recwatch.NewConfigWatcher() - return nil } @@ -195,7 +191,6 @@ func (obj *GAPI) Close() error { return fmt.Errorf("%s: GAPI is not initialized", Name) } - obj.configWatcher.Close() close(obj.closeChan) obj.wg.Wait() obj.initialized = false diff --git a/lang/gapi.go b/lang/gapi.go index 3487957a..e450755d 100644 --- a/lang/gapi.go +++ b/lang/gapi.go @@ -25,7 +25,6 @@ import ( "github.com/purpleidea/mgmt/gapi" "github.com/purpleidea/mgmt/pgraph" - "github.com/purpleidea/mgmt/recwatch" "github.com/purpleidea/mgmt/resources" multierr "github.com/hashicorp/go-multierror" @@ -50,11 +49,10 @@ type GAPI struct { lang *Lang // lang struct - data gapi.Data - initialized bool - closeChan chan struct{} - wg *sync.WaitGroup // sync group for tunnel go routines - configWatcher *recwatch.ConfigWatcher + data gapi.Data + initialized bool + closeChan chan struct{} + wg *sync.WaitGroup // sync group for tunnel go routines } // Cli takes a cli.Context, and returns our GAPI if activated. All arguments @@ -112,7 +110,6 @@ func (obj *GAPI) Init(data gapi.Data) error { obj.closeChan = make(chan struct{}) obj.wg = &sync.WaitGroup{} obj.initialized = true - obj.configWatcher = recwatch.NewConfigWatcher() return nil } @@ -262,7 +259,6 @@ func (obj *GAPI) Close() error { if !obj.initialized { return fmt.Errorf("%s: GAPI is not initialized", Name) } - obj.configWatcher.Close() obj.LangClose() // close lang, esp. if blocked in Stream() wait close(obj.closeChan) obj.wg.Wait() diff --git a/recwatch/configwatch.go b/recwatch/configwatch.go deleted file mode 100644 index 1089d978..00000000 --- a/recwatch/configwatch.go +++ /dev/null @@ -1,148 +0,0 @@ -// Mgmt -// Copyright (C) 2013-2018+ James Shubin and the project contributors -// Written by James Shubin and the project contributors -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -package recwatch - -import ( - "log" - "sync" -) - -// ConfigWatcher returns events on a channel anytime one of its files events. -type ConfigWatcher struct { - Flags Flags - - ch chan string - wg sync.WaitGroup - closechan chan struct{} - errorchan chan error -} - -// NewConfigWatcher creates a new ConfigWatcher struct. -func NewConfigWatcher() *ConfigWatcher { - return &ConfigWatcher{ - ch: make(chan string), - closechan: make(chan struct{}), - errorchan: make(chan error), - } -} - -// Add new file paths to watch for events on. -func (obj *ConfigWatcher) Add(file ...string) { - if len(file) == 0 { - return - } - if len(file) > 1 { - for _, f := range file { // add all the files... - obj.Add(f) // recurse - } - return - } - // otherwise, add the one file passed in... - obj.wg.Add(1) - go func() { - defer obj.wg.Done() - ch := obj.ConfigWatch(file[0]) - for { - select { - case e, ok := <-ch: - if !ok { // channel closed - return - } - if e != nil { - obj.errorchan <- e - return - } - select { - case obj.ch <- file[0]: // send on channel - case <-obj.closechan: - return // never mind, close early! - } - continue - // not needed, closes via ConfigWatch() chan close - //case <-obj.closechan: - // return - } - } - }() -} - -// Error returns a channel of errors that notifies us of permanent issues. -func (obj *ConfigWatcher) Error() <-chan error { - return obj.errorchan -} - -// Events returns a channel to listen on for file events. It closes when it is -// emptied after the Close() method is called. You can test for closure with the -// f, more := <-obj.Events() pattern. -func (obj *ConfigWatcher) Events() chan string { - return obj.ch -} - -// Close shuts down the ConfigWatcher object. It closes the Events channel after -// all the currently pending events have been emptied. -func (obj *ConfigWatcher) Close() { - if obj.ch == nil { - return - } - close(obj.closechan) - obj.wg.Wait() // wait until everyone is done sending on obj.ch - //obj.ch <- "" // send finished message - close(obj.ch) - obj.ch = nil - close(obj.errorchan) -} - -// ConfigWatch writes on the channel every time an event is seen for the path. -func (obj *ConfigWatcher) ConfigWatch(file string) chan error { - ch := make(chan error) - go func() { - recWatcher, err := NewRecWatcher(file, false) - if err != nil { - ch <- err - close(ch) - return - } - recWatcher.Flags = obj.Flags - defer recWatcher.Close() - for { - if obj.Flags.Debug { - log.Printf("Watching: %v", file) - } - select { - case event, ok := <-recWatcher.Events(): - if !ok { // channel is closed - close(ch) - return - } - if err := event.Error; err != nil { - ch <- err - close(ch) - return - } - select { - case ch <- nil: // send event! - case <-obj.closechan: - close(ch) - return - } - } - } - //close(ch) - }() - return ch -} diff --git a/yamlgraph/gapi.go b/yamlgraph/gapi.go index df0a4bce..11339bed 100644 --- a/yamlgraph/gapi.go +++ b/yamlgraph/gapi.go @@ -24,7 +24,6 @@ import ( "github.com/purpleidea/mgmt/gapi" "github.com/purpleidea/mgmt/pgraph" - "github.com/purpleidea/mgmt/recwatch" "github.com/purpleidea/mgmt/resources" errwrap "github.com/pkg/errors" @@ -46,11 +45,10 @@ func init() { type GAPI struct { InputURI string // input URI of file system containing yaml graph to use - data gapi.Data - initialized bool - closeChan chan struct{} - wg sync.WaitGroup // sync group for tunnel go routines - configWatcher *recwatch.ConfigWatcher + data gapi.Data + initialized bool + closeChan chan struct{} + wg sync.WaitGroup // sync group for tunnel go routines } // Cli takes a cli.Context, and returns our GAPI if activated. All arguments @@ -103,7 +101,6 @@ func (obj *GAPI) Init(data gapi.Data) error { obj.data = data // store for later obj.closeChan = make(chan struct{}) obj.initialized = true - obj.configWatcher = recwatch.NewConfigWatcher() return nil } @@ -197,7 +194,6 @@ func (obj *GAPI) Close() error { if !obj.initialized { return fmt.Errorf("%s: GAPI is not initialized", Name) } - obj.configWatcher.Close() close(obj.closeChan) obj.wg.Wait() obj.initialized = false // closed = true diff --git a/yamlgraph2/gapi.go b/yamlgraph2/gapi.go index 29743961..56480a66 100644 --- a/yamlgraph2/gapi.go +++ b/yamlgraph2/gapi.go @@ -24,7 +24,6 @@ import ( "github.com/purpleidea/mgmt/gapi" "github.com/purpleidea/mgmt/pgraph" - "github.com/purpleidea/mgmt/recwatch" "github.com/purpleidea/mgmt/resources" errwrap "github.com/pkg/errors" @@ -46,11 +45,10 @@ func init() { type GAPI struct { InputURI string // input URI of file system containing yaml graph to use - data gapi.Data - initialized bool - closeChan chan struct{} - wg sync.WaitGroup // sync group for tunnel go routines - configWatcher *recwatch.ConfigWatcher + data gapi.Data + initialized bool + closeChan chan struct{} + wg sync.WaitGroup // sync group for tunnel go routines } // Cli takes a cli.Context, and returns our GAPI if activated. All arguments @@ -103,7 +101,6 @@ func (obj *GAPI) Init(data gapi.Data) error { obj.data = data // store for later obj.closeChan = make(chan struct{}) obj.initialized = true - obj.configWatcher = recwatch.NewConfigWatcher() return nil } @@ -197,7 +194,6 @@ func (obj *GAPI) Close() error { if !obj.initialized { return fmt.Errorf("%s: GAPI is not initialized", Name) } - obj.configWatcher.Close() close(obj.closeChan) obj.wg.Wait() obj.initialized = false // closed = true