forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_alerts_notify.go
132 lines (106 loc) · 3.88 KB
/
config_alerts_notify.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
package evergreen
import (
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
DefaultEventProcessingLimit = 1000
DefaultBufferIntervalSeconds = 60
DefaultBufferTargetPerInterval = 20
)
// NotifyConfig hold logging and email settings for the notify package.
type NotifyConfig struct {
BufferTargetPerInterval int `bson:"buffer_target_per_interval" json:"buffer_target_per_interval" yaml:"buffer_target_per_interval"`
BufferIntervalSeconds int `bson:"buffer_interval_seconds" json:"buffer_interval_seconds" yaml:"buffer_interval_seconds"`
EventProcessingLimit int `bson:"event_processing_limit" json:"event_processing_limit" yaml:"event_processing_limit"`
SMTP SMTPConfig `bson:"smtp" json:"smtp" yaml:"smtp"`
}
func (c *NotifyConfig) SectionId() string { return "notify" }
func (c *NotifyConfig) Get(env Environment) error {
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
res := coll.FindOne(ctx, byId(c.SectionId()))
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
*c = NotifyConfig{}
return nil
}
return errors.Wrapf(err, "error retrieving section %s", c.SectionId())
}
if err := res.Decode(c); err != nil {
return errors.Wrap(err, "problem decoding result")
}
return nil
}
func (c *NotifyConfig) Set() error {
env := GetEnvironment()
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
_, err := coll.ReplaceOne(ctx, byId(c.SectionId()), c, options.Replace().SetUpsert(true))
return errors.Wrapf(err, "error updating section %s", c.SectionId())
}
func (c *NotifyConfig) ValidateAndDefault() error {
if c.BufferIntervalSeconds <= 0 {
c.BufferIntervalSeconds = DefaultBufferIntervalSeconds
}
if c.BufferTargetPerInterval <= 0 {
c.BufferTargetPerInterval = DefaultBufferTargetPerInterval
}
// cap to 100 jobs/sec per server
jobsPerSecond := c.BufferIntervalSeconds / c.BufferTargetPerInterval
if jobsPerSecond > maxNotificationsPerSecond {
return errors.Errorf("maximum notification jobs per second is %d", maxNotificationsPerSecond)
}
if c.EventProcessingLimit <= 0 {
c.EventProcessingLimit = DefaultEventProcessingLimit
}
return nil
}
type AlertsConfig struct {
SMTP SMTPConfig `bson:"smtp" json:"smtp" yaml:"smtp"`
}
func (c *AlertsConfig) SectionId() string { return "alerts" }
func (c *AlertsConfig) Get(env Environment) error {
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
res := coll.FindOne(ctx, byId(c.SectionId()))
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
*c = AlertsConfig{}
return nil
}
return errors.Wrapf(err, "error retrieving section %s", c.SectionId())
}
if err := res.Decode(c); err != nil {
return errors.Wrap(err, "problem decoding result")
}
return nil
}
func (c *AlertsConfig) Set() error {
env := GetEnvironment()
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
_, err := coll.UpdateOne(ctx, byId(c.SectionId()), bson.M{
"$set": bson.M{
"smtp": c.SMTP,
},
}, options.Update().SetUpsert(true))
return errors.Wrapf(err, "error updating section %s", c.SectionId())
}
func (c *AlertsConfig) ValidateAndDefault() error { return nil }
// SMTPConfig holds SMTP email settings.
type SMTPConfig struct {
Server string `bson:"server" json:"server" yaml:"server"`
Port int `bson:"port" json:"port" yaml:"port"`
UseSSL bool `bson:"use_ssl" json:"use_ssl" yaml:"use_ssl"`
Username string `bson:"username" json:"username" yaml:"username"`
Password string `bson:"password" json:"password" yaml:"password"`
From string `bson:"from" json:"from" yaml:"from"`
AdminEmail []string `bson:"admin_email" json:"admin_email" yaml:"admin_email"`
}