-
Notifications
You must be signed in to change notification settings - Fork 143
/
notifier.go
154 lines (133 loc) · 4.02 KB
/
notifier.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package notifier provides a piped component
// that sends notifications to the configured targets.
package notifier
import (
"context"
"fmt"
"time"
"go.uber.org/atomic"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
"github.com/pipe-cd/pipecd/pkg/version"
)
type Notifier struct {
config *config.PipedSpec
handlers []handler
gracePeriod time.Duration
closed atomic.Bool
logger *zap.Logger
}
type handler struct {
matcher *matcher
sender sender
}
type sender interface {
Run(ctx context.Context) error
Notify(event model.NotificationEvent)
Close(ctx context.Context)
}
func NewNotifier(cfg *config.PipedSpec, logger *zap.Logger) (*Notifier, error) {
logger = logger.Named("notifier")
receivers := make(map[string]config.NotificationReceiver, len(cfg.Notifications.Receivers))
for _, r := range cfg.Notifications.Receivers {
receivers[r.Name] = r
}
handlers := make([]handler, 0, len(cfg.Notifications.Routes))
for _, route := range cfg.Notifications.Routes {
receiver, ok := receivers[route.Receiver]
if !ok {
return nil, fmt.Errorf("missing receiver %s that is used in route %s", route.Receiver, route.Name)
}
var sd sender
switch {
case receiver.Slack != nil:
sd = newSlackSender(receiver.Name, *receiver.Slack, cfg.WebAddress, logger)
case receiver.Webhook != nil:
sd = newWebhookSender(receiver.Name, *receiver.Webhook, cfg.WebAddress, logger)
default:
continue
}
handlers = append(handlers, handler{
matcher: newMatcher(route),
sender: sd,
})
}
return &Notifier{
config: cfg,
handlers: handlers,
gracePeriod: 10 * time.Second,
logger: logger,
}, nil
}
func (n *Notifier) Run(ctx context.Context) error {
group, ctx := errgroup.WithContext(ctx)
// Start running all senders.
for i := range n.handlers {
sender := n.handlers[i].sender
group.Go(func() error {
return sender.Run(ctx)
})
}
// Send the PIPED_STARTED event.
n.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_PIPED_STARTED,
Metadata: &model.NotificationEventPipedStarted{
Id: n.config.PipedID,
Name: n.config.Name,
Version: version.Get().Version,
ProjectId: n.config.ProjectID,
},
})
n.logger.Info(fmt.Sprintf("all %d notifiers have been started", len(n.handlers)))
if err := group.Wait(); err != nil {
n.logger.Error("failed while running", zap.Error(err))
return err
}
// Send the PIPED_STOPPED event.
n.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_PIPED_STOPPED,
Metadata: &model.NotificationEventPipedStopped{
Id: n.config.PipedID,
Name: n.config.Name,
Version: version.Get().Version,
ProjectId: n.config.ProjectID,
},
})
// Mark to ignore all incoming events from this time and close all senders.
n.closed.Store(true)
ctx, cancel := context.WithTimeout(context.Background(), n.gracePeriod)
defer cancel()
for i := range n.handlers {
sender := n.handlers[i].sender
sender.Close(ctx)
}
n.logger.Info(fmt.Sprintf("all %d notifiers have been stopped", len(n.handlers)))
return nil
}
func (n *Notifier) Notify(event model.NotificationEvent) {
if n.closed.Load() {
n.logger.Warn("ignore an event because notifier is already closed", zap.String("type", event.Type.String()))
return
}
for _, h := range n.handlers {
if !h.matcher.Match(event) {
continue
}
h.sender.Notify(event)
}
}