-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
165 lines (150 loc) · 3.97 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
155
156
157
158
159
160
161
162
163
164
165
package notificationqueue
import (
"time"
"github.com/herb-go/notification"
"github.com/herb-go/notification/notificationdelivery"
)
//Notifier notifier struct
type Notifier struct {
//DeliveryCenter
notificationdelivery.DeliveryCenter
//Workers push workers num
Workers int
queue Queue
c chan int
//IDGenerator id generator
IDGenerator func() (string, error)
//OnNotification notification handler
OnNotification func(*notification.Notification)
//OnReceipt receipt handler
OnReceipt func(*Receipt)
//OnExecution execution handler
OnExecution func(*Execution)
//Recover recover handler
Recover func()
}
//SetQueue set queue to notifier.
//SetQueue should be called before start
func (notifier *Notifier) SetQueue(q Queue) {
notifier.queue = q
}
//HandleDeliverTimeout deliver timeout handler
func (notifier *Notifier) HandleDeliverTimeout(e *Execution) {
notifier.newReceipt(e, notificationdelivery.DeliveryStatusTimeout, "")
}
//HandleRetryTooMany retry toomany handler
func (notifier *Notifier) HandleRetryTooMany(e *Execution) {
notifier.newReceipt(e, notificationdelivery.DeliveryStatusRetryTooMany, "")
}
//Queue return notifier queue.
func (notifier *Notifier) Queue() Queue {
return notifier.queue
}
func (notifier *Notifier) handleReceipt(r *Receipt) {
defer notifier.Recover()
notifier.OnReceipt(r)
}
func (notifier *Notifier) newReceipt(e *Execution, status notificationdelivery.DeliveryStatus, msg string) {
eid := e.ExecutionID
r := NewReceipt()
r.Notification = e.Notification
r.ExecutionID = eid
r.Status = status
r.Message = msg
go notifier.handleReceipt(r)
}
func (notifier *Notifier) deliver(e *Execution) {
defer notifier.Recover()
status, msg, err := notifier.deliveryNotification(e.Notification)
if err != nil {
status = notificationdelivery.DeliveryStatusFail
msg = err.Error()
go func() {
defer notifier.Recover()
panic(err)
}()
}
notifier.newReceipt(e, status, msg)
if notificationdelivery.IsStatusRetryable(status) {
return
}
err = notifier.queue.Remove(e.Notification.ID)
if err != nil {
panic(err)
}
}
func (notifier *Notifier) listen(c <-chan *Execution) {
for {
select {
case e := <-c:
notifier.deliver(e)
case <-notifier.c:
return
}
}
}
func (notifier *Notifier) onNotification(n *notification.Notification) {
defer notifier.Recover()
notifier.OnNotification(n)
}
func (notifier *Notifier) InitNotification(n *notification.Notification) error {
id, err := notifier.IDGenerator()
if err != nil {
return err
}
n.ID = id
n.CreatedTime = time.Now().Unix()
return nil
}
//Notify delivery notification
//Notification id will be returned
func (notifier *Notifier) Notify(n *notification.Notification) error {
err := notifier.queue.Push(n)
if err == nil {
go notifier.onNotification(n)
}
return err
}
//Start start notifier
func (notifier *Notifier) Start() error {
notifier.c = make(chan int)
c, err := notifier.queue.PopChan()
if err != nil {
return err
}
workers := notifier.Workers
if workers < 1 {
workers = 1
}
for i := 0; i < workers; i++ {
go notifier.listen(c)
}
return notifier.queue.Start()
}
//Stop stop notifier
func (notifier *Notifier) Stop() error {
close(notifier.c)
notifier.c = nil
return notifier.queue.Stop()
}
func (notifier *Notifier) deliveryNotification(n *notification.Notification) (status notificationdelivery.DeliveryStatus, receipt string, err error) {
return notificationdelivery.DeliverNotification(notifier.DeliveryCenter, n)
}
//Reset reset notifier handlers
func (notifier *Notifier) Reset() {
notifier.OnNotification = NopNotificationHandler
notifier.OnReceipt = NopReceiptHanlder
// notifier.OnDeliverTimeout = NopExecutionHandler
// notifier.OnRetryTooMany = NopExecutionHandler
notifier.OnExecution = NopExecutionHandler
notifier.IDGenerator = NopIDGenerator
}
//NewNotifier create new notifier
func NewNotifier() *Notifier {
n := &Notifier{
queue: &NopQueue{},
DeliveryCenter: notificationdelivery.NewAtomicDeliveryCenter(),
}
n.Reset()
return n
}