Add app status.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-15 09:41:10 +08:00
parent b877f1cc9e
commit e6f2bf5152
6 changed files with 118 additions and 4 deletions

View File

@ -77,6 +77,7 @@ func main() {
Message: *message,
}
gorush.InitAppStatus()
gorush.PushToAndroid(req)
return
@ -103,6 +104,7 @@ func main() {
Message: *message,
}
gorush.InitAppStatus()
gorush.InitAPNSClient()
gorush.PushToIOS(req)
@ -113,6 +115,7 @@ func main() {
gorush.LogError.Fatal(err)
}
gorush.InitAppStatus()
gorush.InitAPNSClient()
gorush.InitWorkers(gorush.PushConf.Core.WorkerNum, gorush.PushConf.Core.QueueNum)
gorush.RunHTTPServer()

View File

@ -18,5 +18,6 @@ var (
// LogAccess is log server request log
LogAccess *logrus.Logger
// LogError is log server error log
LogError *logrus.Logger
LogError *logrus.Logger
RushStatus StatusApp
)

View File

@ -158,9 +158,11 @@ func queueNotification(req RequestPush) int {
}
QueueNotification <- notification
count++
count += len(notification.Tokens)
}
addTotalCount(int64(count))
return count
}
@ -276,6 +278,7 @@ func PushToIOS(req PushNotification) bool {
// apns server error
LogPush(FailedPush, token, req, err)
isError = true
addIosError(1)
continue
}
@ -283,11 +286,13 @@ func PushToIOS(req PushNotification) bool {
// error message:
// ref: https://github.com/sideshow/apns2/blob/master/response.go#L14-L65
LogPush(FailedPush, token, req, errors.New(res.Reason))
addIosError(1)
continue
}
if res.Sent() {
LogPush(SucceededPush, token, req, nil)
addIosSuccess(1)
}
}
@ -380,6 +385,8 @@ func PushToAndroid(req PushNotification) bool {
}
LogAccess.Debug(fmt.Sprintf("Android Success count: %d, Failure count: %d", res.Success, res.Failure))
addAndroidSuccess(int64(res.Success))
addAndroidError(int64(res.Failure))
for k, result := range res.Results {
if result.Error != "" {

View File

@ -355,7 +355,7 @@ func TestSenMultipleNotifications(t *testing.T) {
}
count := queueNotification(req)
assert.Equal(t, 2, count)
assert.Equal(t, 3, count)
}
func TestDisabledAndroidNotifications(t *testing.T) {
@ -421,7 +421,7 @@ func TestDisabledIosNotifications(t *testing.T) {
}
count := queueNotification(req)
assert.Equal(t, 1, count)
assert.Equal(t, 2, count)
}
func TestMissingIosCertificate(t *testing.T) {

51
gorush/status.go Normal file
View File

@ -0,0 +1,51 @@
package gorush
import (
"sync/atomic"
)
type StatusApp struct {
QueueMax int `json:"queue_max"`
QueueUsage int `json:"queue_usage"`
TotalCount int64 `json:"total_count"`
Ios IosStatus `json:"ios"`
Android AndroidStatus `json:"android"`
}
type AndroidStatus struct {
PushSuccess int64 `json:"push_success"`
PushError int64 `json:"push_error"`
}
type IosStatus struct {
PushSuccess int64 `json:"push_success"`
PushError int64 `json:"push_error"`
}
func InitAppStatus() {
RushStatus.TotalCount = 0
RushStatus.Ios.PushSuccess = 0
RushStatus.Ios.PushError = 0
RushStatus.Android.PushSuccess = 0
RushStatus.Android.PushError = 0
}
func addTotalCount(count int64) {
atomic.AddInt64(&RushStatus.TotalCount, count)
}
func addIosSuccess(count int64) {
atomic.AddInt64(&RushStatus.Ios.PushSuccess, count)
}
func addIosError(count int64) {
atomic.AddInt64(&RushStatus.Ios.PushError, count)
}
func addAndroidSuccess(count int64) {
atomic.AddInt64(&RushStatus.Android.PushSuccess, count)
}
func addAndroidError(count int64) {
atomic.AddInt64(&RushStatus.Android.PushError, count)
}

52
gorush/status_test.go Normal file
View File

@ -0,0 +1,52 @@
package gorush
import (
"github.com/stretchr/testify/assert"
"sync/atomic"
"testing"
)
func TestAddTotalCount(t *testing.T) {
InitAppStatus()
addTotalCount(1000)
val := atomic.LoadInt64(&RushStatus.TotalCount)
assert.Equal(t, int64(1000), val)
}
func TestAddIosSuccess(t *testing.T) {
InitAppStatus()
addIosSuccess(1000)
val := atomic.LoadInt64(&RushStatus.Ios.PushSuccess)
assert.Equal(t, int64(1000), val)
}
func TestAddIosError(t *testing.T) {
InitAppStatus()
addIosError(1000)
val := atomic.LoadInt64(&RushStatus.Ios.PushError)
assert.Equal(t, int64(1000), val)
}
func TestAndroidSuccess(t *testing.T) {
InitAppStatus()
addAndroidSuccess(1000)
val := atomic.LoadInt64(&RushStatus.Android.PushSuccess)
assert.Equal(t, int64(1000), val)
}
func TestAddAndroidError(t *testing.T) {
InitAppStatus()
addAndroidError(1000)
val := atomic.LoadInt64(&RushStatus.Android.PushError)
assert.Equal(t, int64(1000), val)
}