diff options
author | Wolfy-J <[email protected]> | 2019-05-03 15:44:51 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-05-03 15:44:51 +0300 |
commit | 28c787d66c2b74dd2300c792abd1e4f987c3d6c9 (patch) | |
tree | b9c5ef036eda3ffa16b5e87a06ce99fcd8a4d7b4 /service/watcher/config.go | |
parent | e9d42947a6922ce2f0aa9f9bcab4ead167735bc9 (diff) |
new watchers functionality
Diffstat (limited to 'service/watcher/config.go')
-rw-r--r-- | service/watcher/config.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/service/watcher/config.go b/service/watcher/config.go new file mode 100644 index 00000000..dcd31777 --- /dev/null +++ b/service/watcher/config.go @@ -0,0 +1,55 @@ +package watcher + +import ( + "fmt" + "github.com/spiral/roadrunner" + "github.com/spiral/roadrunner/service" + "time" +) + +// Configures set of Services. +type Config struct { + // Interval defines the update duration for underlying watchers, default 1s. + Interval time.Duration + + // Services declares list of services to be watched. + Services map[string]*watcherConfig +} + +// Hydrate must populate Config values using given Config source. Must return error if Config is not valid. +func (c *Config) Hydrate(cfg service.Config) error { + if err := cfg.Unmarshal(c); err != nil { + return err + } + + // Always use second based definition for time durations + if c.Interval < time.Microsecond { + c.Interval = time.Second * time.Duration(c.Interval.Nanoseconds()) + } + + for name, cfg := range c.Services { + if err := cfg.Normalize(); err != nil { + return fmt.Errorf("invalid watcher `%s`: %s", name, err.Error()) + } + } + + return nil +} + +// InitDefaults sets missing values to their default values. +func (c *Config) InitDefaults() error { + c.Interval = time.Second + + return nil +} + +// Watchers returns list of defined Services +func (c *Config) Watchers(l listener) (watchers map[string]roadrunner.Watcher) { + watchers = make(map[string]roadrunner.Watcher) + + for name, cfg := range c.Services { + watchers[name] = &watcher{lsn: l, interval: c.Interval, cfg: cfg} + } + + return watchers +} |