diff --git a/go.mod b/go.mod index 7a82b9c..f057ebe 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/appleboy/gin-status-api v1.1.0 github.com/appleboy/go-fcm v0.1.5 github.com/appleboy/gofight/v2 v2.1.2 - github.com/appleboy/queue v0.0.4-0.20210725000413-1085ecaca645 + github.com/appleboy/queue v0.0.5 github.com/asdine/storm/v3 v3.2.1 github.com/buger/jsonparser v1.1.1 github.com/dgraph-io/badger/v3 v3.2103.1 @@ -15,7 +15,7 @@ require ( github.com/gin-gonic/gin v1.7.2 github.com/go-redis/redis/v7 v7.4.0 github.com/golang/glog v0.0.0-20210429001901-424d2337a529 // indirect - github.com/golang/protobuf v1.5.1 + github.com/golang/protobuf v1.5.2 github.com/google/flatbuffers v2.0.0+incompatible // indirect github.com/json-iterator/go v1.1.10 github.com/mattn/go-isatty v0.0.12 @@ -30,10 +30,9 @@ require ( github.com/syndtr/goleveldb v1.0.0 github.com/thoas/stats v0.0.0-20190407194641-965cb2de1678 github.com/tidwall/buntdb v1.2.0 - golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 + golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c - golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect google.golang.org/grpc v1.36.1 - google.golang.org/protobuf v1.26.0 + google.golang.org/protobuf v1.27.1 ) diff --git a/go.sum b/go.sum index 44e6d78..c035b4a 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,10 @@ github.com/appleboy/go-fcm v0.1.5 h1:fKbcZf/7vwGsvDkcop8a+kCHnK+tt4wXX0X7uEzwI6E github.com/appleboy/go-fcm v0.1.5/go.mod h1:MSxZ4LqGRsnywOjnlXJXMqbjZrG4vf+0oHitfC9HRH0= github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= -github.com/appleboy/queue v0.0.4-0.20210725000413-1085ecaca645 h1:RpjX4HMSLsrQsxsCQN2pAbPDZWOtpX3dqLnoK0+UzLY= -github.com/appleboy/queue v0.0.4-0.20210725000413-1085ecaca645/go.mod h1:6Mn0z4hURZW/26huvRXG0SJ4o7pBdo6hOryRiegy/4Q= +github.com/appleboy/queue v0.0.4 h1:WuRPuiYs4aj7cZfPbzgJBxvDyrJkOy0evNpkNvLS+JM= +github.com/appleboy/queue v0.0.4/go.mod h1:cEQW2y7dduAUqUGnGJEK9oM5bZLlc0+3HI9bTUL0+Ek= +github.com/appleboy/queue v0.0.5 h1:4wrS83ktdxg3U0YGx1EC9mU8vXD4BCAswAliqCEaxHw= +github.com/appleboy/queue v0.0.5/go.mod h1:cEQW2y7dduAUqUGnGJEK9oM5bZLlc0+3HI9bTUL0+Ek= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= @@ -179,8 +181,8 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.1 h1:jAbXjIeW2ZSW2AwFxlGTDoc2CjI2XujLkV3ArsZFCvc= -github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= @@ -265,6 +267,7 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8 github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.11.12/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU= github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -294,6 +297,7 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -316,10 +320,16 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg= github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU= +github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q= +github.com/nats-io/jwt/v2 v2.0.2/go.mod h1:VRP+deawSXyhNjXmxPCHskrR6Mq50BqpEI5SEcNiGlY= github.com/nats-io/nats-server/v2 v2.1.2/go.mod h1:Afk+wRZqkMQs/p45uXdrVLuab3gwv3Z8C4HTBu8GD/k= +github.com/nats-io/nats-server/v2 v2.3.2/go.mod h1:dUf7Cm5z5LbciFVwWx54owyCKm8x4/hL6p7rrljhLFY= github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w= +github.com/nats-io/nats.go v1.11.1-0.20210623165838-4b75fc59ae30/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= +github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s= +github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/nsqio/go-nsq v1.0.8 h1:3L2F8tNLlwXXlp2slDUrUWSBn2O3nMh8R1/KEDFTHPk= github.com/nsqio/go-nsq v1.0.8/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY= @@ -514,9 +524,11 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= -golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -586,6 +598,7 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -612,8 +625,8 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -624,6 +637,7 @@ golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -704,8 +718,9 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/logx/log.go b/logx/log.go index 4b57c4d..ccda24d 100644 --- a/logx/log.go +++ b/logx/log.go @@ -200,7 +200,7 @@ type InputLog struct { } // LogPush record user push request and server response. -func LogPush(input *InputLog) { +func LogPush(input *InputLog) LogPushEntry { var platColor, resetColor, output string if isTerm { @@ -249,4 +249,6 @@ func LogPush(input *InputLog) { case core.FailedPush: LogError.Error(output) } + + return log } diff --git a/notify/notification.go b/notify/notification.go index 82b8de6..f47f057 100644 --- a/notify/notification.go +++ b/notify/notification.go @@ -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 } diff --git a/notify/notification_apns.go b/notify/notification_apns.go index 4361ea0..e59ca86 100644 --- a/notify/notification_apns.go +++ b/notify/notification_apns.go @@ -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 } diff --git a/notify/notification_fcm.go b/notify/notification_fcm.go index 868bc78..762ca51 100644 --- a/notify/notification_fcm.go +++ b/notify/notification_fcm.go @@ -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, diff --git a/notify/notification_fcm_test.go b/notify/notification_fcm_test.go index f1f027b..1e57872 100644 --- a/notify/notification_fcm_test.go +++ b/notify/notification_fcm_test.go @@ -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) { diff --git a/notify/notification_hms.go b/notify/notification_hms.go index 6f5ecda..1099a15 100644 --- a/notify/notification_hms.go +++ b/notify/notification_hms.go @@ -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 } diff --git a/router/server.go b/router/server.go index 000fc0c..f2eeb5f 100644 --- a/router/server.go +++ b/router/server.go @@ -228,10 +228,11 @@ func routerEngine(cfg config.ConfYaml, q *queue.Queue) *gin.Engine { } // markFailedNotification adds failure logs for all tokens in push notification -func markFailedNotification(cfg config.ConfYaml, notification *notify.PushNotification, reason string) { +func markFailedNotification(cfg config.ConfYaml, notification *notify.PushNotification, reason string) []logx.LogPushEntry { logx.LogError.Error(reason) + logs := make([]logx.LogPushEntry, 0) for _, token := range notification.Tokens { - notification.AddLog(logx.GetLogPushEntry(&logx.InputLog{ + logs = append(logs, logx.GetLogPushEntry(&logx.InputLog{ ID: notification.ID, Status: core.FailedPush, Token: token, @@ -242,7 +243,8 @@ func markFailedNotification(cfg config.ConfYaml, notification *notify.PushNotifi Format: cfg.Log.Format, })) } - notification.WaitDone() + + return logs } // HandleNotification add notification to queue list. @@ -275,16 +277,36 @@ func handleNotification(ctx context.Context, cfg config.ConfYaml, req notify.Req newNotification = append(newNotification, notification) } - log := make([]logx.LogPushEntry, 0, count) + logs := make([]logx.LogPushEntry, 0, count) for _, notification := range newNotification { if cfg.Core.Sync { - notification.Wg = &wg - notification.Log = &log - notification.AddWaitCount() + wg.Add(1) } - if err := q.Queue(notification); err != nil { - markFailedNotification(cfg, notification, "max capacity reached") + if core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) && cfg.Core.Sync { + func(msg *notify.PushNotification) { + q.QueueTask(func(ctx context.Context) error { + defer wg.Done() + resp, err := notify.SendNotification(msg) + if err != nil { + return err + } + + // add log + for _, v := range resp.Logs { + logs = append(logs, v) + } + + return nil + }) + }(notification) + } else if err := q.Queue(notification); err != nil { + resp := markFailedNotification(cfg, notification, "max capacity reached") + // add log + for _, v := range resp { + logs = append(logs, v) + } + wg.Done() } count += len(notification.Tokens) @@ -300,5 +322,5 @@ func handleNotification(ctx context.Context, cfg config.ConfYaml, req notify.Req status.StatStorage.AddTotalCount(int64(count)) - return count, log + return count, logs } diff --git a/router/server_test.go b/router/server_test.go index baf76f5..d782c4d 100644 --- a/router/server_test.go +++ b/router/server_test.go @@ -37,7 +37,7 @@ func TestMain(m *testing.M) { } w = simple.NewWorker( - simple.WithRunFunc(func(msg queue.QueuedMessage) error { + simple.WithRunFunc(func(ctx context.Context, msg queue.QueuedMessage) error { notify.SendNotification(msg) return nil }),