diff options
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | service/reload/config.go | 29 | ||||
-rw-r--r-- | service/reload/config_test.go | 6 | ||||
-rw-r--r-- | service/reload/service.go | 50 | ||||
-rw-r--r-- | service/reload/watcher.go | 7 |
5 files changed, 58 insertions, 36 deletions
@@ -10,6 +10,8 @@ require ( github.com/go-ole/go-ole v1.2.4 // indirect github.com/mattn/go-colorable v0.1.4 // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect github.com/olekukonko/tablewriter v0.0.4 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.4.1 diff --git a/service/reload/config.go b/service/reload/config.go index 930f4dff..f33b5081 100644 --- a/service/reload/config.go +++ b/service/reload/config.go @@ -9,25 +9,32 @@ import ( // Config is a Reload configuration point. type Config struct { - // Enable or disable Reload extension, default disable. - Enabled bool // Interval is a global refresh interval Interval time.Duration + // Patterns is a global file patterns to watch. It will be applied to every directory in project Patterns []string + // Services is set of services which would be reloaded in case of FS changes Services map[string]ServiceConfig } type ServiceConfig struct { + // Enabled indicates that service must be watched, doest not required when any other option specified + Enabled bool + // Recursive is options to use nested files from root folder Recursive bool + // Patterns is per-service specific files to watch Patterns []string + // Dirs is per-service specific dirs which will be combined with Patterns Dirs []string + // Ignore is set of files which would not be watched Ignore []string + // service is a link to service to restart service *roadrunner.Controllable } @@ -37,29 +44,23 @@ func (c *Config) Hydrate(cfg service.Config) error { if err := cfg.Unmarshal(c); err != nil { return err } + return nil } // InitDefaults sets missing values to their default values. func (c *Config) InitDefaults() error { - return c.Valid() + c.Interval = time.Second + c.Patterns = []string{".php"} + + return nil } // Valid validates the configuration. func (c *Config) Valid() error { - if c.Enabled == true && c.Interval < time.Second { + if c.Interval < time.Second { return errors.New("too short interval") } - if c.Enabled { - if c.Services == nil { - return errors.New("should add at least 1 service") - } - - if len(c.Services) == 0 { - return errors.New("should add initialized config") - } - } - return nil } diff --git a/service/reload/config_test.go b/service/reload/config_test.go index dd9a2797..c9c05a1e 100644 --- a/service/reload/config_test.go +++ b/service/reload/config_test.go @@ -17,7 +17,6 @@ func Test_Config_Valid(t *testing.T) { } cfg := &Config{ - Enabled: true, Interval: time.Second, Patterns: nil, Services: services, @@ -28,7 +27,6 @@ func Test_Config_Valid(t *testing.T) { func Test_Fake_ServiceConfig(t *testing.T) { services := make(map[string]ServiceConfig) cfg := &Config{ - Enabled: true, Interval: time.Second, Patterns: nil, Services: services, @@ -39,7 +37,6 @@ func Test_Fake_ServiceConfig(t *testing.T) { func Test_Interval(t *testing.T) { services := make(map[string]ServiceConfig) cfg := &Config{ - Enabled: true, Interval: time.Millisecond, Patterns: nil, Services: services, @@ -50,10 +47,9 @@ func Test_Interval(t *testing.T) { func Test_NoServiceConfig(t *testing.T) { services := make(map[string]ServiceConfig) cfg := &Config{ - Enabled: true, Interval: time.Millisecond, Patterns: nil, Services: services, } assert.Error(t, cfg.Valid()) -}
\ No newline at end of file +} diff --git a/service/reload/service.go b/service/reload/service.go index ab249c41..359b3331 100644 --- a/service/reload/service.go +++ b/service/reload/service.go @@ -3,6 +3,7 @@ package reload import ( "errors" "fmt" + "github.com/sirupsen/logrus" "github.com/spiral/roadrunner" "github.com/spiral/roadrunner/service" "os" @@ -14,17 +15,20 @@ import ( const ID = "reload" type Service struct { - reloadConfig *Config - watcher *Watcher + cfg *Config + log *logrus.Logger + watcher *Watcher } // Init controller service -func (s *Service) Init(cfg *Config, c service.Container) (bool, error) { - s.reloadConfig = cfg - if !s.reloadConfig.Enabled { +func (s *Service) Init(cfg *Config, log *logrus.Logger, c service.Container) (bool, error) { + if cfg == nil || len(cfg.Services) == 0 { return false, nil } + s.cfg = cfg + s.log = log + var configs []WatcherConfig // mount Services to designated services @@ -37,7 +41,7 @@ func (s *Service) Init(cfg *Config, c service.Container) (bool, error) { } } - for serviceName, config := range s.reloadConfig.Services { + for serviceName, config := range s.cfg.Services { if cfg.Services[serviceName].service == nil { continue } @@ -73,25 +77,22 @@ func (s *Service) Init(cfg *Config, c service.Container) (bool, error) { } func (s *Service) Serve() error { - if !s.reloadConfig.Enabled { - return nil - } - - if s.reloadConfig.Interval < time.Second { + if s.cfg.Interval < time.Second { return errors.New("reload interval is too fast") } go func() { for e := range s.watcher.Event { + println(fmt.Sprintf("[UPDATE] Service: %s, path to file: %s, filename: %s", e.service, e.path, e.info.Name())) - srv := s.reloadConfig.Services[e.service] + srv := s.cfg.Services[e.service] if srv.service != nil { - s := *srv.service - err := s.Server().Reset() + sv := *srv.service + err := sv.Server().Reset() if err != nil { - fmt.Println(err) + s.log.Error(err) } } else { s.watcher.mu.Lock() @@ -101,7 +102,24 @@ func (s *Service) Serve() error { } }() - err := s.watcher.StartPolling(s.reloadConfig.Interval) + //go func() { + // for { + // select { + // case <-update: + // updated = append(updated, update) + // case <-time.Ticker: + // updated = updated[0:0] + // err := sv.Server().Reset() + // s.log.Debugf( + // "reload %s, found file changes in %s", + // strings.Join(updated, ","), + // ) + // case <-exit: + // } + // } + //}() + + err := s.watcher.StartPolling(s.cfg.Interval) if err != nil { return err } diff --git a/service/reload/watcher.go b/service/reload/watcher.go index 612964c5..6c0ba86c 100644 --- a/service/reload/watcher.go +++ b/service/reload/watcher.go @@ -28,16 +28,22 @@ type Event struct { type WatcherConfig struct { // service name serviceName string + // recursive or just add by singe directory recursive bool + // directories used per-service directories []string + // simple hook, just CONTAINS filterHooks func(filename string, pattern []string) error + // path to file with files files map[string]os.FileInfo + // ignored directories, used map for O(1) amortized get ignored map[string]struct{} + // filePatterns to ignore filePatterns []string } @@ -194,7 +200,6 @@ outer: } filesList[pathToFile] = fileInfoList[i] - } return filesList, nil |