Skip to content

Commit

Permalink
Only stop the application if no app could be initialized (#29)
Browse files Browse the repository at this point in the history
* Only stop aplication if no app could be initialized

* Change metric name
  • Loading branch information
capella authored and cscatolini committed May 3, 2019
1 parent 371c375 commit 19cf20d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions extensions/datadog_statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func (s *StatsD) HandleNotificationFailure(game string, platform string, err *er
s.Client.Incr("failed", []string{fmt.Sprintf("platform:%s", platform), fmt.Sprintf("game:%s", game), fmt.Sprintf("reason:%s", err.Key)}, 1)
}

//InitializeFailure notifu error when is impossible tho initilizer an app
func (s *StatsD) InitializeFailure(game string, platform string) {
s.Client.Incr("initialize_failure", []string{fmt.Sprintf("platform:%s", platform), fmt.Sprintf("game:%s", game)}, 1)
}

//ReportGoStats reports go stats in statsd
func (s *StatsD) ReportGoStats(
numGoRoutines int,
Expand Down
1 change: 1 addition & 0 deletions interfaces/stats_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import "github.com/topfreegames/pusher/errors"

// StatsReporter interface for making stats reporters pluggable easily
type StatsReporter interface {
InitializeFailure(game string, platform string)
HandleNotificationSent(game string, platform string)
HandleNotificationSuccess(game string, platform string)
HandleNotificationFailure(game string, platform string, err *errors.PushError)
Expand Down
17 changes: 15 additions & 2 deletions pusher/apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package pusher

import (
"errors"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -89,6 +90,7 @@ func (a *APNSPusher) configure(queue interfaces.APNSPushQueue, db interfaces.DB,
a.MessageHandler = make(map[string]interfaces.MessageHandler)
a.Queue = q
l.Info("Configuring messageHandler")
success := 0
for _, k := range strings.Split(a.Config.GetString("apns.apps"), ",") {
authKeyPath := a.Config.GetString("apns.certs." + k + ".authKeyPath")
keyID := a.Config.GetString("apns.certs." + k + ".keyID")
Expand All @@ -112,10 +114,21 @@ func (a *APNSPusher) configure(queue interfaces.APNSPushQueue, db interfaces.DB,
a.feedbackReporters,
nil,
)
if err != nil {
return err
if err == nil {
success++
} else {
for _, statsReporter := range a.StatsReporters {
statsReporter.InitializeFailure(k, "apns")
}
l.WithFields(logrus.Fields{
"method": "apns",
"game": k,
}).Error(err)
}
a.MessageHandler[k] = handler
}
if success == 0 {
return errors.New("Could not initilize any app")
}
return nil
}
19 changes: 16 additions & 3 deletions pusher/gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package pusher

import (
"errors"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -87,14 +88,15 @@ func (g *GCMPusher) configure(client interfaces.GCMClient, db interfaces.DB, sta
}
g.Queue = q
g.MessageHandler = make(map[string]interfaces.MessageHandler)
success := 0
for _, k := range strings.Split(g.Config.GetString("gcm.apps"), ",") {
senderID := g.Config.GetString("gcm.certs." + k + ".senderID")
apiKey := g.Config.GetString("gcm.certs." + k + ".apiKey")
l.Infof(
"Configuring messageHandler for game %s with senderID %s and apiKey %s",
k, senderID, apiKey,
)
handler, herr := extensions.NewGCMMessageHandler(
handler, err := extensions.NewGCMMessageHandler(
senderID,
apiKey,
g.IsProduction,
Expand All @@ -105,10 +107,21 @@ func (g *GCMPusher) configure(client interfaces.GCMClient, db interfaces.DB, sta
g.feedbackReporters,
client,
)
if herr != nil {
return herr
if err == nil {
success++
} else {
for _, statsReporter := range g.StatsReporters {
statsReporter.InitializeFailure(k, "gcm")
}
l.WithFields(logrus.Fields{
"method": "gcm",
"game": k,
}).Error(err)
}
g.MessageHandler[k] = handler
}
if success == 0 {
return errors.New("Could not initilize any app")
}
return nil
}

0 comments on commit 19cf20d

Please sign in to comment.