diff --git a/gorush.go b/gorush.go index 3bdaf46..811e0a0 100644 --- a/gorush.go +++ b/gorush.go @@ -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() diff --git a/gorush/global.go b/gorush/global.go index 7688bc4..e7cd42c 100644 --- a/gorush/global.go +++ b/gorush/global.go @@ -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 ) diff --git a/gorush/notification.go b/gorush/notification.go index e42bce1..65c6d8b 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -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 != "" { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index d91d6b4..e6fd10c 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -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) { diff --git a/gorush/status.go b/gorush/status.go new file mode 100644 index 0000000..cdca1e9 --- /dev/null +++ b/gorush/status.go @@ -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) +} diff --git a/gorush/status_test.go b/gorush/status_test.go new file mode 100644 index 0000000..0aa8017 --- /dev/null +++ b/gorush/status_test.go @@ -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) +}