summaryrefslogtreecommitdiff
path: root/service/reload/service.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-02-20 14:14:11 +0300
committerValery Piashchynski <[email protected]>2020-02-20 14:14:11 +0300
commit2efcfeb89861ba981f980bb4503c31ca6c7a92e0 (patch)
tree22269a5cecc888cd26d6b1bd8477f30ea604ba21 /service/reload/service.go
parentec7975355a8acea632e5c9b7e912b3e9ad6907ca (diff)
Declare general interfaces, Controllable and Attacher instead of private
First dirty working example of reload
Diffstat (limited to 'service/reload/service.go')
-rw-r--r--service/reload/service.go122
1 files changed, 78 insertions, 44 deletions
diff --git a/service/reload/service.go b/service/reload/service.go
index 5a400159..9b3ac2f0 100644
--- a/service/reload/service.go
+++ b/service/reload/service.go
@@ -1,6 +1,8 @@
package reload
import (
+ "fmt"
+ "github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service"
"os"
"strings"
@@ -12,79 +14,111 @@ const ID = "reload"
type Service struct {
reloadConfig *Config
- container service.Container
watcher *Watcher
+
}
// Init controller service
func (s *Service) Init(cfg *Config, c service.Container) (bool, error) {
- s.container = c
s.reloadConfig = cfg
- return true, nil
-}
+ var err error
+ var configs []WatcherConfig
+
+ // mount Services to designated services
+ for serviceName, _ := range cfg.Services {
+ svc, _ := c.Get(serviceName)
+ if ctrl, ok := svc.(roadrunner.Controllable); ok {
+ tmp := cfg.Services[serviceName]
+ tmp.service = &ctrl
+ cfg.Services[serviceName] = tmp
+ }
+ }
-func (s *Service) Serve() error {
- if !s.reloadConfig.Enabled {
- return nil
+
+
+ for serviceName, config := range s.reloadConfig.Services {
+ if cfg.Services[serviceName].service == nil {
+ continue
+ }
+ configs = append(configs, WatcherConfig{
+ serviceName: serviceName,
+ recursive: config.Recursive,
+ directories: config.Dirs,
+ filterHooks: func(filename, pattern string) error {
+ if strings.Contains(filename, pattern) {
+ return ErrorSkip
+ }
+ return nil
+ },
+ files: make(map[string]os.FileInfo),
+ //ignored:
+ })
}
- var err error
- s.watcher, err = NewWatcher([]WatcherConfig{WatcherConfig{
- serviceName: "test_service_name",
- recursive: false,
- directories: []string{"/service"},
- filterHooks: func(filename, pattern string) error {
- if strings.Contains(filename, pattern) {
- return ErrorSkip
- }
- return nil
- },
- files: make(map[string]os.FileInfo),
- //ignored: []string{".php"},
- }})
+ s.watcher, err = NewWatcher(configs)
if err != nil {
- return err
+ return false, err
}
+ for serviceName, config := range s.reloadConfig.Services {
+ svc, _ := c.Get(serviceName)
+ if ctrl, ok := svc.(*roadrunner.Controllable); ok {
+ (*ctrl).Server().Reset()
+ }
+ configs = append(configs, WatcherConfig{
+ serviceName: serviceName,
+ recursive: config.Recursive,
+ directories: config.Dirs,
+ filterHooks: func(filename, pattern string) error {
+ if strings.Contains(filename, pattern) {
+ return ErrorSkip
+ }
+ return nil
+ },
+ files: make(map[string]os.FileInfo),
+ //ignored:
+
+ })
+ }
- s.watcher.AddSingle("test_service_name", "/service")
+ return true, nil
+}
+func (s *Service) Serve() error {
+ if !s.reloadConfig.Enabled {
+ return nil
+ }
go func() {
for {
select {
case e := <-s.watcher.Event:
- println(e.Type)
+ println(fmt.Sprintf("type is:%s, oldPath:%s, path:%s, name:%s", e.Type, e.OldPath, e.Path, e.FileInfo.Name()))
+
+ srv := s.reloadConfig.Services[e.Type]
+
+ if srv.service != nil {
+ s := *srv.service
+ err := s.Server().Reset()
+ if err != nil {
+ fmt.Println(err)
+ }
+ } else {
+ s.watcher.mu.Lock()
+ delete(s.watcher.watcherConfigs, e.Type)
+ s.watcher.mu.Unlock()
+ }
}
}
- //for e = range w.Event {
- //
- // println("event")
- // // todo use status
- // //svc, _ := s.container.Get("http")
- // //if svc != nil {
- // // if srv, ok := svc.(service.Service); ok {
- // // srv.Stop()
- // // err = srv.Serve()
- // // if err != nil {
- // // return err
- // // }
- // // }
- // //}
- //
- // //println("event skipped due to service is nil")
- //}
}()
- err = s.watcher.StartPolling(time.Second)
+ err := s.watcher.StartPolling(time.Second * 2)
if err != nil {
return err
}
- // read events and restart corresponding services
-
return nil
}