forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.go
59 lines (50 loc) · 1.34 KB
/
send.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
package notify
import (
"context"
"fmt"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// send calls the underlying notification services to send the given subject and message to their respective endpoints.
func (n *Notify) send(ctx context.Context, subject, message string) error {
if n.Disabled {
return nil
}
if ctx == nil {
ctx = context.Background()
}
var eg errgroup.Group
for _, service := range n.notifiers {
if service == nil {
continue
}
service := service
eg.Go(func() error {
var err error
defer func() {
if r := recover(); r != nil {
if recErr, ok := r.(error); ok {
err = recErr
} else {
err = fmt.Errorf("service panic: %v", r)
}
}
}()
err = service.Send(ctx, subject, message)
return err
})
}
err := eg.Wait()
if err != nil {
err = errors.Wrap(ErrSendNotification, err.Error())
}
return err
}
// Send calls the underlying notification services to send the given subject and message to their respective endpoints.
func (n *Notify) Send(ctx context.Context, subject, message string) error {
return n.send(ctx, subject, message)
}
// Send calls the underlying notification services to send the given subject and message to their respective endpoints.
func Send(ctx context.Context, subject, message string) error {
return std.Send(ctx, subject, message)
}