-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify_server.go
149 lines (139 loc) · 3.93 KB
/
notify_server.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
package mpapi
import (
"encoding/json"
"sync"
"time"
"github.com/mapleque/kelp/logger"
"github.com/mapleque/kelp/mysql"
)
const (
MP_NOTIFY_STATUS_NEW = 0
MP_NOTIFY_STATUS_DOING = 1
MP_NOTIFY_STATUS_SUCCESS = 2
MP_NOTIFY_STATUS_FAILD = 3
)
type NotifyServer struct {
conn mysql.Connector
log logger.Loggerer
}
func NewNotifyServer(conn mysql.Connector) *NotifyServer {
server := &NotifyServer{
conn: conn,
log: logger.Get("http"),
}
return server
}
func (this *NotifyServer) StartNotify(maxThread int) {
if maxThread < 1 {
return
}
nextSig := make(chan bool, 1)
nextSig <- true
for {
select {
case <-nextSig:
if this.doNotifyBatch(maxThread) > 0 {
nextSig <- true
} else {
time.Sleep(10 * time.Second)
nextSig <- true
}
}
}
}
type NotifyTask struct {
// query from mp_notify
Id int64 `json:"-" column:"id"`
UserId int64 `json:"-" column:"user_id"`
AppId string `json:"-" column:"appid"`
TemplateId string `json:"template_id"`
Page string `json:"page"`
FormId string `json:"form_id"`
Datastr string `json:"-" column:"data"`
EmphasisKeyword string `json:"emphasis_keyword"`
Status int `json:"-" column:"status"`
Additional string `json:"-" column:"additional"`
ActiveAt string `json:"-" column:"active_at"`
CreateAt string `json:"-" column:"create_at"`
// join from user_open
OpenId string `json:"touser" column:"openid"`
// build from Datastr
Data map[string]interface{} `json:"data" column:"-"`
}
func (this *NotifyServer) doNotifyBatch(maxTaskNum int) int {
taskList := []*NotifyTask{}
if err := this.conn.Query(
&taskList,
"SELECT mp_notify.id AS id, mp_notify.user_id AS user_id, mp_notify.appid AS appid, "+
"template_id, page, form_id, data, emphasis_keyword, status, additional, active_at, "+
"mp_notify.create_at AS create_at, openid "+
"FROM mp_notify INNER JOIN user "+
"ON mp_notify.user_id = user.id "+
"WHERE mp_notify.status = ? AND mp_notify.active_at <= NOW() "+
"ORDER BY mp_notify.id LIMIT ?",
MP_NOTIFY_STATUS_NEW,
maxTaskNum,
); err != nil {
return 0
}
this.log.Info("wx notify start", len(taskList))
var wg sync.WaitGroup
for _, task := range taskList {
wg.Add(1)
go func(task *NotifyTask) {
defer wg.Done()
if eff, err := this.conn.Execute(
"UPDATE mp_notify SET status = ? WHERE id = ? AND status = ? LIMIT 1",
MP_NOTIFY_STATUS_DOING,
task.Id,
MP_NOTIFY_STATUS_NEW,
); err != nil || eff != 1 {
return
}
if wxapp, err := NewWXApp(task.AppId, this.conn); err != nil {
this.log.Error("can not create wx app", err)
this.conn.Execute(
"UPDATE mp_notify SET status = ?, additional = ? WHERE id = ? AND status = ? LIMIT 1",
MP_NOTIFY_STATUS_FAILD,
"invalid appid",
task.Id,
MP_NOTIFY_STATUS_DOING,
)
} else {
task.Data = map[string]interface{}{}
if err := json.Unmarshal([]byte(task.Datastr), &task.Data); err != nil {
this.log.Error("message data encode error", task.Datastr)
this.conn.Execute(
"UPDATE mp_notify SET status = ?, additional = ? WHERE id = ? AND status = ? LIMIT 1",
MP_NOTIFY_STATUS_FAILD,
"message data decode error",
task.Id,
MP_NOTIFY_STATUS_DOING,
)
return
}
body, _ := json.Marshal(task)
if err := wxapp.SendTemplateMessage(body); err != nil {
this.log.Error("send message error", err)
this.conn.Execute(
"UPDATE mp_notify SET status = ?, additional = ? WHERE id = ? AND status = ? LIMIT 1",
MP_NOTIFY_STATUS_FAILD,
err.Error(),
task.Id,
MP_NOTIFY_STATUS_DOING,
)
} else {
this.conn.Execute(
"UPDATE mp_notify SET status = ? WHERE id = ? AND status = ? LIMIT 1",
MP_NOTIFY_STATUS_SUCCESS,
task.Id,
MP_NOTIFY_STATUS_DOING,
)
}
}
}(task)
}
wg.Wait()
this.log.Info("wx notify end", len(taskList))
return len(taskList)
}