summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2020-02-23 14:14:05 +0300
committerWolfy-J <[email protected]>2020-02-23 14:14:05 +0300
commit9ef9dfed7928e9a96b9545074f8aeb1468fda46c (patch)
treea50850ed8d74cf72964ae755e7e4790c8694a043 /service
parent6a23ccdcda44ea8d90eb174ce3aab99d6b67b495 (diff)
- need batching
Diffstat (limited to 'service')
-rw-r--r--service/reload/config.go29
-rw-r--r--service/reload/config_test.go6
-rw-r--r--service/reload/service.go50
-rw-r--r--service/reload/watcher.go7
4 files changed, 56 insertions, 36 deletions
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