diff options
author | Valery Piashchynski <[email protected]> | 2020-02-23 14:53:16 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-02-23 14:53:16 +0300 |
commit | d4d83ac0043cb15d19ae4fa49095434b1c594451 (patch) | |
tree | 54cf81eb136ff3c6c0b4fc766698e6c3891ddd36 /service | |
parent | 9ef9dfed7928e9a96b9545074f8aeb1468fda46c (diff) |
Fix CR issues
Diffstat (limited to 'service')
-rw-r--r-- | service/reload/service.go | 83 | ||||
-rw-r--r-- | service/reload/watcher.go | 2 |
2 files changed, 56 insertions, 29 deletions
diff --git a/service/reload/service.go b/service/reload/service.go index 359b3331..832ac185 100644 --- a/service/reload/service.go +++ b/service/reload/service.go @@ -2,11 +2,11 @@ package reload import ( "errors" - "fmt" "github.com/sirupsen/logrus" "github.com/spiral/roadrunner" "github.com/spiral/roadrunner/service" "os" + "runtime" "strings" "time" ) @@ -18,6 +18,7 @@ type Service struct { cfg *Config log *logrus.Logger watcher *Watcher + stopc chan struct{} } // Init controller service @@ -28,6 +29,7 @@ func (s *Service) Init(cfg *Config, log *logrus.Logger, c service.Container) (bo s.cfg = cfg s.log = log + s.stopc = make(chan struct{}) var configs []WatcherConfig @@ -81,44 +83,67 @@ func (s *Service) Serve() error { return errors.New("reload interval is too fast") } + // make a map with unique services + // so, if we would have a 100 events from http service + // in map we would see only 1 key and it's config + treshholdc := make(chan struct { + serviceConfig ServiceConfig + service string + }, 100) + + // drain channel in case of leaved messages + defer func() { + go func() { + for range treshholdc { + + } + }() + }() + go func() { for e := range s.watcher.Event { + s.log.Debugf("[UPDATE] Service: %s, path to file: %s, filename: %s", e.service, e.path, e.info.Name()) + + treshholdc <- struct { + serviceConfig ServiceConfig + service string + }{serviceConfig: s.cfg.Services[e.service], service: e.service} - println(fmt.Sprintf("[UPDATE] Service: %s, path to file: %s, filename: %s", e.service, e.path, e.info.Name())) + } + }() + + // use the same interval + ticker := time.NewTicker(s.cfg.Interval) - srv := s.cfg.Services[e.service] + // map with configs by services + updated := make(map[string]ServiceConfig, 100) - if srv.service != nil { - sv := *srv.service - err := sv.Server().Reset() - if err != nil { - s.log.Error(err) + go func() { + for { + select { + case config := <-treshholdc: + // replace previous value in map by more recent without adding new one + updated[config.service] = config.serviceConfig + case <-ticker.C: + if len(updated) > 0 { + for k, v := range updated { + sv := *v.service + err := sv.Server().Reset() + if err != nil { + s.log.Error(err) + } + s.log.Debugf("found file changes in %s service, reloading", k) + } + // zero map + updated = make(map[string]ServiceConfig, 100) } - } else { - s.watcher.mu.Lock() - delete(s.watcher.watcherConfigs, e.service) - s.watcher.mu.Unlock() + case <-s.stopc: + ticker.Stop() + runtime.Goexit() } } }() - //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 6c0ba86c..3596ae73 100644 --- a/service/reload/watcher.go +++ b/service/reload/watcher.go @@ -382,6 +382,7 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { //Send all the remaining create and remove events. for pth, info := range creates { + w.watcherConfigs[serviceName].files[pth] = info w.Event <- Event{ path: pth, info: info, @@ -389,6 +390,7 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { } } for pth, info := range removes { + delete(w.watcherConfigs[serviceName].files, pth) w.Event <- Event{ path: pth, info: info, |