chore(queue): upgrade appleboy/queue to 0.5.0 (#616)
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/core"
|
||||
@@ -58,10 +58,13 @@ type RequestPush struct {
|
||||
Notifications []PushNotification `json:"notifications" binding:"required"`
|
||||
}
|
||||
|
||||
// ResponsePush response of notification request.
|
||||
type ResponsePush struct {
|
||||
Logs []logx.LogPushEntry `json:"logs"`
|
||||
}
|
||||
|
||||
// PushNotification is single notification request
|
||||
type PushNotification struct {
|
||||
Wg *sync.WaitGroup
|
||||
Log *[]logx.LogPushEntry
|
||||
Cfg config.ConfYaml
|
||||
|
||||
// Common
|
||||
@@ -117,27 +120,6 @@ type PushNotification struct {
|
||||
Apns D `json:"apns,omitempty"`
|
||||
}
|
||||
|
||||
// WaitDone decrements the WaitGroup counter.
|
||||
func (p *PushNotification) WaitDone() {
|
||||
if p.Wg != nil {
|
||||
p.Wg.Done()
|
||||
}
|
||||
}
|
||||
|
||||
// AddWaitCount increments the WaitGroup counter.
|
||||
func (p *PushNotification) AddWaitCount() {
|
||||
if p.Wg != nil {
|
||||
p.Wg.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
// AddLog record fail log of notification
|
||||
func (p *PushNotification) AddLog(log logx.LogPushEntry) {
|
||||
if p.Log != nil {
|
||||
*p.Log = append(*p.Log, log)
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes for queue message
|
||||
func (p *PushNotification) Bytes() []byte {
|
||||
b, err := json.Marshal(p)
|
||||
@@ -253,30 +235,28 @@ func CheckPushConf(cfg config.ConfYaml) error {
|
||||
}
|
||||
|
||||
// SendNotification send notification
|
||||
func SendNotification(req queue.QueuedMessage) {
|
||||
func SendNotification(req queue.QueuedMessage) (resp *ResponsePush, err error) {
|
||||
v, ok := req.(*PushNotification)
|
||||
if !ok {
|
||||
if err := json.Unmarshal(req.Bytes(), &v); err != nil {
|
||||
if err = json.Unmarshal(req.Bytes(), &v); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
defer func() {
|
||||
v.WaitDone()
|
||||
}()
|
||||
|
||||
switch v.Platform {
|
||||
case core.PlatFormIos:
|
||||
PushToIOS(*v)
|
||||
resp, err = PushToIOS(*v)
|
||||
case core.PlatFormAndroid:
|
||||
PushToAndroid(*v)
|
||||
resp, err = PushToAndroid(*v)
|
||||
case core.PlatFormHuawei:
|
||||
PushToHuawei(*v)
|
||||
resp, err = PushToHuawei(*v)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Run send notification
|
||||
var Run = func(msg queue.QueuedMessage) error {
|
||||
SendNotification(msg)
|
||||
return nil
|
||||
var Run = func(ctx context.Context, msg queue.QueuedMessage) error {
|
||||
_, err := SendNotification(msg)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -388,7 +388,7 @@ func getApnsClient(cfg config.ConfYaml, req PushNotification) (client *apns2.Cli
|
||||
}
|
||||
|
||||
// PushToIOS provide send notification to APNs server.
|
||||
func PushToIOS(req PushNotification) {
|
||||
func PushToIOS(req PushNotification) (resp *ResponsePush, err error) {
|
||||
logx.LogAccess.Debug("Start push notification for iOS")
|
||||
|
||||
if req.Cfg.Core.Sync && !core.IsLocalQueue(core.Queue(req.Cfg.Queue.Engine)) {
|
||||
@@ -404,6 +404,8 @@ func PushToIOS(req PushNotification) {
|
||||
maxRetry = req.Retry
|
||||
}
|
||||
|
||||
resp = &ResponsePush{}
|
||||
|
||||
Retry:
|
||||
var newTokens []string
|
||||
|
||||
@@ -426,18 +428,18 @@ Retry:
|
||||
// ref: https://github.com/sideshow/apns2/blob/master/response.go#L14-L65
|
||||
err = errors.New(res.Reason)
|
||||
}
|
||||
// apns server error
|
||||
logPush(req.Cfg, core.FailedPush, token, req, err)
|
||||
|
||||
// apns server error
|
||||
errLog := logPush(req.Cfg, core.FailedPush, token, req, err)
|
||||
if req.Cfg.Core.Sync {
|
||||
req.AddLog(createLogPushEntry(req.Cfg, core.FailedPush, token, req, err))
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, createLogPushEntry(req.Cfg, core.FailedPush, token, req, err), req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
|
||||
status.StatStorage.AddIosError(1)
|
||||
@@ -468,4 +470,6 @@ Retry:
|
||||
req.Tokens = newTokens
|
||||
goto Retry
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
|
||||
}
|
||||
|
||||
// PushToAndroid provide send notification to Android server.
|
||||
func PushToAndroid(req PushNotification) {
|
||||
func PushToAndroid(req PushNotification) (resp *ResponsePush, err error) {
|
||||
logx.LogAccess.Debug("Start push notification for Android")
|
||||
|
||||
if req.Cfg.Core.Sync && !core.IsLocalQueue(core.Queue(req.Cfg.Queue.Engine)) {
|
||||
@@ -124,12 +124,14 @@ func PushToAndroid(req PushNotification) {
|
||||
}
|
||||
|
||||
// check message
|
||||
err := CheckMessage(req)
|
||||
err = CheckMessage(req)
|
||||
if err != nil {
|
||||
logx.LogError.Error("request error: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp = &ResponsePush{}
|
||||
|
||||
Retry:
|
||||
notification := GetAndroidNotification(req)
|
||||
|
||||
@@ -151,28 +153,30 @@ Retry:
|
||||
logx.LogError.Error("FCM server send message error: " + err.Error())
|
||||
|
||||
if req.IsTopic() {
|
||||
errLog := logPush(req.Cfg, core.FailedPush, req.To, req, err)
|
||||
if req.Cfg.Core.Sync {
|
||||
req.AddLog(createLogPushEntry(req.Cfg, core.FailedPush, req.To, req, err))
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, createLogPushEntry(req.Cfg, core.FailedPush, req.To, req, err), req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
status.StatStorage.AddAndroidError(1)
|
||||
} else {
|
||||
for _, token := range req.Tokens {
|
||||
errLog := logPush(req.Cfg, core.FailedPush, token, req, err)
|
||||
if req.Cfg.Core.Sync {
|
||||
req.AddLog(createLogPushEntry(req.Cfg, core.FailedPush, token, req, err))
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, createLogPushEntry(req.Cfg, core.FailedPush, token, req, err), req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
}
|
||||
status.StatStorage.AddAndroidError(int64(len(req.Tokens)))
|
||||
@@ -204,16 +208,16 @@ Retry:
|
||||
newTokens = append(newTokens, to)
|
||||
}
|
||||
|
||||
logPush(req.Cfg, core.FailedPush, to, req, result.Error)
|
||||
errLog := logPush(req.Cfg, core.FailedPush, to, req, result.Error)
|
||||
if req.Cfg.Core.Sync {
|
||||
req.AddLog(createLogPushEntry(req.Cfg, core.FailedPush, to, req, result.Error))
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
} else if req.Cfg.Core.FeedbackURL != "" {
|
||||
go func(logger *logrus.Logger, log logx.LogPushEntry, url string, timeout int64) {
|
||||
err := DispatchFeedback(log, url, timeout)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
}(logx.LogError, createLogPushEntry(req.Cfg, core.FailedPush, to, req, result.Error), req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}(logx.LogError, errLog, req.Cfg.Core.FeedbackURL, req.Cfg.Core.FeedbackTimeout)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -235,9 +239,9 @@ Retry:
|
||||
logPush(req.Cfg, core.SucceededPush, to, req, nil)
|
||||
} else {
|
||||
// failure
|
||||
logPush(req.Cfg, core.FailedPush, to, req, res.Error)
|
||||
errLog := logPush(req.Cfg, core.FailedPush, to, req, res.Error)
|
||||
if req.Cfg.Core.Sync {
|
||||
req.AddLog(createLogPushEntry(req.Cfg, core.FailedPush, to, req, res.Error))
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,9 +250,9 @@ Retry:
|
||||
if len(res.FailedRegistrationIDs) > 0 {
|
||||
newTokens = append(newTokens, res.FailedRegistrationIDs...)
|
||||
|
||||
logPush(req.Cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails"))
|
||||
errLog := logPush(req.Cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails"))
|
||||
if req.Cfg.Core.Sync {
|
||||
req.AddLog(createLogPushEntry(req.Cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails")))
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,23 +263,12 @@ Retry:
|
||||
req.Tokens = newTokens
|
||||
goto Retry
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func createLogPushEntry(cfg config.ConfYaml, status, token string, req PushNotification, err error) logx.LogPushEntry {
|
||||
return logx.GetLogPushEntry(&logx.InputLog{
|
||||
ID: req.ID,
|
||||
Status: status,
|
||||
Token: token,
|
||||
Message: req.Message,
|
||||
Platform: req.Platform,
|
||||
Error: err,
|
||||
HideToken: cfg.Log.HideToken,
|
||||
Format: cfg.Log.Format,
|
||||
})
|
||||
}
|
||||
|
||||
func logPush(cfg config.ConfYaml, status, token string, req PushNotification, err error) {
|
||||
logx.LogPush(&logx.InputLog{
|
||||
func logPush(cfg config.ConfYaml, status, token string, req PushNotification, err error) logx.LogPushEntry {
|
||||
return logx.LogPush(&logx.InputLog{
|
||||
ID: req.ID,
|
||||
Status: status,
|
||||
Token: token,
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/core"
|
||||
"github.com/appleboy/gorush/logx"
|
||||
|
||||
"github.com/appleboy/go-fcm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -105,14 +104,13 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
|
||||
Message: "Welcome",
|
||||
// overwrite android api key
|
||||
APIKey: "1234",
|
||||
|
||||
Log: &[]logx.LogPushEntry{},
|
||||
}
|
||||
|
||||
// FCM server error: 401 error: 401 Unauthorized (Wrong API Key)
|
||||
PushToAndroid(req)
|
||||
resp, err := PushToAndroid(req)
|
||||
|
||||
assert.Len(t, *req.Log, 2)
|
||||
assert.Error(t, err)
|
||||
assert.Len(t, resp.Logs, 2)
|
||||
}
|
||||
|
||||
func TestFCMMessage(t *testing.T) {
|
||||
|
||||
@@ -166,7 +166,7 @@ func GetHuaweiNotification(req PushNotification) (*model.MessageRequest, error)
|
||||
}
|
||||
|
||||
// PushToHuawei provide send notification to Android server.
|
||||
func PushToHuawei(req PushNotification) bool {
|
||||
func PushToHuawei(req PushNotification) (resp *ResponsePush, err error) {
|
||||
logx.LogAccess.Debug("Start push notification for Huawei")
|
||||
|
||||
if req.Cfg.Core.Sync && !core.IsLocalQueue(core.Queue(req.Cfg.Queue.Engine)) {
|
||||
@@ -184,30 +184,34 @@ func PushToHuawei(req PushNotification) bool {
|
||||
}
|
||||
|
||||
// check message
|
||||
err := CheckMessage(req)
|
||||
err = CheckMessage(req)
|
||||
if err != nil {
|
||||
logx.LogError.Error("request error: " + err.Error())
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
Retry:
|
||||
isError := false
|
||||
|
||||
notification, _ := GetHuaweiNotification(req)
|
||||
|
||||
client, err = InitHMSClient(req.Cfg, req.Cfg.Huawei.AppSecret, req.Cfg.Huawei.AppID)
|
||||
|
||||
if err != nil {
|
||||
// HMS server error
|
||||
logx.LogError.Error("HMS server error: " + err.Error())
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
resp = &ResponsePush{}
|
||||
|
||||
Retry:
|
||||
isError := false
|
||||
|
||||
notification, _ := GetHuaweiNotification(req)
|
||||
|
||||
res, err := client.SendMessage(context.Background(), notification)
|
||||
if err != nil {
|
||||
// Send Message error
|
||||
errLog := logPush(req.Cfg, core.FailedPush, req.To, req, err)
|
||||
resp.Logs = append(resp.Logs, errLog)
|
||||
logx.LogError.Error("HMS server send message error: " + err.Error())
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
// Huawei Push Send API does not support exact results for each token
|
||||
@@ -227,5 +231,5 @@ Retry:
|
||||
goto Retry
|
||||
}
|
||||
|
||||
return isError
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user