-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (68 loc) · 1.36 KB
/
main.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
package main
import (
"log"
"main/email"
"main/env"
"main/model"
"main/queue"
"os"
"os/signal"
"syscall"
)
const QueueEmail = "email-q"
func main() {
err := env.Load("env/.env")
if err != nil {
log.Fatalf("failed to load env: %v", err)
}
emailQueue, err := queue.NewQueue(QueueEmail)
if err != nil {
log.Fatalf("failed to get %s queue url: %v", QueueEmail, err)
}
recommendChannel := make(chan model.Result, 1)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
for {
select {
case <-c:
return
default:
recommend, deleteM, err := emailQueue.Receive()
if err != nil {
log.Printf("failed to receive from queue: %v", err)
continue
}
recommendChannel <- model.Result{
Recommend: recommend,
Delete: deleteM,
}
}
}
}()
go func() {
for {
select {
case <-c:
break
case recommend := <-recommendChannel:
log.Printf("recommend: %v", recommend.Recommend)
err = email.SendEmail(recommend.Recommend)
if err != nil {
log.Printf("failed to send email: %v", err)
continue
}
err = recommend.Delete()
if err != nil {
log.Printf("failed to delete message: %v", err)
}
break
default:
break
}
}
}()
log.Printf("waiting for messages from %s\n", QueueEmail)
<-c
log.Println("exiting...")
}