-
Notifications
You must be signed in to change notification settings - Fork 0
/
passthroughqueue.go
83 lines (69 loc) · 1.69 KB
/
passthroughqueue.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
package passthroughqueue
import (
"time"
"github.com/herb-go/notification"
"github.com/herb-go/notification/notificationdelivery/notificationqueue"
)
var DirectiveName = "passthroughqueue"
type Passthroughqueue struct {
c chan *notificationqueue.Execution
IDGenerator func() (string, error)
}
//PopChan return execution chan
func (q *Passthroughqueue) PopChan() (<-chan *notificationqueue.Execution, error) {
return q.c, nil
}
//Push push notification to queue
func (q *Passthroughqueue) Push(n *notification.Notification) error {
var err error
e := notificationqueue.NewExecution()
e.ExecutionID, err = q.IDGenerator()
if err != nil {
return err
}
e.StartTime = time.Now().Unix()
e.Notification = n
go func() {
q.c <- e
}()
return nil
}
//Remove remove notification by given id
func (q *Passthroughqueue) Remove(nid string) error {
return nil
}
//Start start queue
func (q *Passthroughqueue) Start() error {
return nil
}
//Stop stop queue
func (q *Passthroughqueue) Stop() error {
close(q.c)
return nil
}
//AttachTo attach queue to notifier
func (q *Passthroughqueue) AttachTo(n *notificationqueue.Notifier) error {
q.IDGenerator = func() (string, error) {
return n.IDGenerator()
}
return nil
}
//Detach detach queue.
func (pq *Passthroughqueue) Detach() error {
return nil
}
func (q *Passthroughqueue) AppylToPublisher(p *notificationqueue.Publisher) error {
p.SetQueue(q)
return nil
}
func New() *Passthroughqueue {
return &Passthroughqueue{
c: make(chan *notificationqueue.Execution),
}
}
func Factory(loader func(v interface{}) error) (notificationqueue.Directive, error) {
return New(), nil
}
func init() {
notificationqueue.Register(DirectiveName, Factory)
}