blob: 0d4197160d3b6bc50f14cff5aa6df1a8e444ad48 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 rr watcher.
type Watchable interface {
// Watch attaches watcher to the service.
Watch(w roadrunner.Watcher)
}
// Services to watch the state of rr 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)
}
}
|