-
Notifications
You must be signed in to change notification settings - Fork 7
/
notification.go
36 lines (31 loc) · 1006 Bytes
/
notification.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
package notification
import (
"context"
"os/exec"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var notifyChange chan string
func init() {
// Creates a channel with a buffer for 600 messages
notifyChange = make(chan string, 600)
go func(notification <-chan string) {
for endpoint := range notification {
log.WithFields(log.Fields{"operation": "notification", "endpoint": endpoint}).Debug("notification endpoint")
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("notification.timeout")*time.Second)
if err := exec.CommandContext(ctx, viper.GetString("notification.script"), endpoint).Run(); err != nil {
log.WithFields(log.Fields{"operation": "notification", "endpoint": endpoint}).Error(err)
}
cancel()
}
}(notifyChange)
}
// NotifyChange will run a script to notify a system on changes to assets
func NotifyChange(asset string) {
if !viper.GetBool("notification.enabled") {
return
}
notifyChange <- asset
return
}