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/service.go | |
parent | e9d42947a6922ce2f0aa9f9bcab4ead167735bc9 (diff) |
new watchers functionality
Diffstat (limited to 'service/watcher/service.go')
-rw-r--r-- | service/watcher/service.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/service/watcher/service.go b/service/watcher/service.go new file mode 100644 index 00000000..c81ff3f5 --- /dev/null +++ b/service/watcher/service.go @@ -0,0 +1,46 @@ +package watcher + +import ( + "github.com/spiral/roadrunner" + "github.com/spiral/roadrunner/service" +) + +// ID defines watcher service name. +const ID = "watch" + +// Watchable defines the ability to attach roadrunner watcher. +type Watchable interface { + // Watch attaches watcher to the service. + Watch(w roadrunner.Watcher) +} + +// Services to watch the state of roadrunner service inside other services. +type Service struct { + cfg *Config + lsns []func(event int, ctx interface{}) +} + +// Init watcher service +func (s *Service) Init(cfg *Config, c service.Container) (bool, error) { + // mount Services to designated services + for id, watcher := range cfg.Watchers(s.throw) { + svc, _ := c.Get(id) + if watchable, ok := svc.(Watchable); ok { + watchable.Watch(watcher) + } + } + + return true, nil +} + +// AddListener attaches server event watcher. +func (s *Service) AddListener(l func(event int, ctx interface{})) { + s.lsns = append(s.lsns, l) +} + +// throw handles service, server and pool events. +func (s *Service) throw(event int, ctx interface{}) { + for _, l := range s.lsns { + l(event, ctx) + } +} |