forked from jordan-wright/email
/
email_custom.go
67 lines (55 loc) · 1.08 KB
/
email_custom.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
package email
import (
"github.com/thejackrabbit/aero/conf"
"github.com/thejackrabbit/aero/ds"
"github.com/thejackrabbit/aero/panik"
"github.com/thejackrabbit/aero/que"
"time"
)
func (e *Email) Enque() {
if mails == nil { // initialization
startLoop()
}
go func() {
d, err := ds.ToBytes(e, false)
panik.On(err)
mails <- d
}()
}
func (e *Email) SendLater(after string) {
d, err := time.ParseDuration(after)
panik.On(err)
e.SendAt(time.Now().Add(d))
}
func (e *Email) SendAt(t time.Time) {
u := t.Unix()
if time.Now().Unix() < u {
panik.Do("Send time %s is earlier than current time", t)
}
e.SendAfter = &u
e.Enque()
}
var Queue que.Queue
var mails chan []byte
var size int = 100
func init() {
if conf.Exists("email.queue.default") {
Queue = que.NewQueue("email.queue.default")
}
}
func startLoop() {
if mails != nil {
return
}
mails = make(chan []byte, conf.Int(size, "email", "buffer"))
panik.If(Queue == nil, "Queue is not initialized")
go func() {
for {
select {
case mb := <-mails:
err := Queue.Push(mb)
panik.On(err)
}
}
}()
}