forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
failure_watcher.go
35 lines (29 loc) · 952 Bytes
/
failure_watcher.go
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
package services
import (
"github.com/pkg/errors"
)
// FailureWatcher waits for service failures, and passed them to the channel.
type FailureWatcher struct {
ch chan error
}
func NewFailureWatcher() *FailureWatcher {
return &FailureWatcher{ch: make(chan error)}
}
// Returns channel for this watcher. If watcher is nil, returns nil channel.
// Errors returned on the channel include failure case and service description.
func (w *FailureWatcher) Chan() <-chan error {
if w == nil {
return nil
}
return w.ch
}
func (w *FailureWatcher) WatchService(service Service) {
service.AddListener(NewListener(nil, nil, nil, nil, func(from State, failure error) {
w.ch <- errors.Wrapf(failure, "service %v failed", service)
}))
}
func (w *FailureWatcher) WatchManager(manager *Manager) {
manager.AddListener(NewManagerListener(nil, nil, func(service Service) {
w.ch <- errors.Wrapf(service.FailureCase(), "service %v failed", service)
}))
}