Skip to content

Commit

Permalink
refactor(fcm): add context parameter to various functions (#775)
Browse files Browse the repository at this point in the history
- Introduce `graceful.NewManager` initialization in `main.go`
- Modify `pinger` function to accept a context parameter
- Update `PushToAndroid` function to accept a context parameter
- Update `InitFCMClient` function to accept a context parameter
- Replace `context.Background()` with passed context in various functions
- Add context import in `notification_fcm_test.go`

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jun 15, 2024
1 parent 0645e5a commit 228ec1d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ func main() {
}
}

g := graceful.NewManager(
graceful.WithLogger(logx.QueueLogger()),
)

if ping {
if err := pinger(cfg); err != nil {
logx.LogError.Warnf("ping server error: %v", err)
if err := pinger(g.ShutdownContext(), cfg); err != nil {
logx.LogError.Fatal(err)
}
return
}
Expand Down Expand Up @@ -195,7 +199,7 @@ func main() {
return
}

if _, err := notify.PushToAndroid(req, cfg); err != nil {
if _, err := notify.PushToAndroid(g.ShutdownContext(), req, cfg); err != nil {
return
}

Expand Down Expand Up @@ -342,10 +346,6 @@ func main() {
queue.WithLogger(logx.QueueLogger()),
)

g := graceful.NewManager(
graceful.WithLogger(logx.QueueLogger()),
)

g.AddShutdownJob(func() error {
// logx.LogAccess.Info("close the queue system, current queue usage: ", q.Usage())
// stop queue system and wait job completed
Expand All @@ -365,7 +365,7 @@ func main() {
}

if cfg.Android.Enabled {
if _, err = notify.InitFCMClient(cfg); err != nil {
if _, err = notify.InitFCMClient(g.ShutdownContext(), cfg); err != nil {
logx.LogError.Fatal(err)
}
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func usage() {

// handles pinging the endpoint and returns an error if the
// agent is in an unhealthy state.
func pinger(cfg *config.ConfYaml) error {
func pinger(ctx context.Context, cfg *config.ConfYaml) error {
transport := &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
Expand All @@ -452,7 +452,7 @@ func pinger(cfg *config.ConfYaml) error {
Transport: transport,
}
req, _ := http.NewRequestWithContext(
context.Background(),
ctx,
http.MethodGet,
"http://localhost:"+cfg.Core.Port+cfg.API.HealthURI,
nil,
Expand Down
2 changes: 1 addition & 1 deletion notify/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func SendNotification(
case core.PlatFormIos:
resp, err = PushToIOS(v, cfg)
case core.PlatFormAndroid:
resp, err = PushToAndroid(v, cfg)
resp, err = PushToAndroid(ctx, v, cfg)
case core.PlatFormHuawei:
resp, err = PushToHuawei(v, cfg)
}
Expand Down
10 changes: 5 additions & 5 deletions notify/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func fileExists(filename string) bool {
}

// InitFCMClient use for initialize FCM Client.
func InitFCMClient(cfg *config.ConfYaml) (*fcm.Client, error) {
func InitFCMClient(ctx context.Context, cfg *config.ConfYaml) (*fcm.Client, error) {
var opts []fcm.Option

credential := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
Expand All @@ -47,7 +47,7 @@ func InitFCMClient(cfg *config.ConfYaml) (*fcm.Client, error) {
}

return fcm.NewClient(
context.Background(),
ctx,
opts...,
)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func GetAndroidNotification(req *PushNotification) []*messaging.Message {
}

// PushToAndroid provide send notification to Android server.
func PushToAndroid(req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
func PushToAndroid(ctx context.Context, req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
logx.LogAccess.Debug("Start push notification for Android")

var (
Expand All @@ -140,7 +140,7 @@ func PushToAndroid(req *PushNotification, cfg *config.ConfYaml) (resp *ResponseP
}

resp = &ResponsePush{}
client, err = InitFCMClient(cfg)
client, err = InitFCMClient(ctx, cfg)

Retry:
notification := GetAndroidNotification(req)
Expand All @@ -150,7 +150,7 @@ Retry:
return resp, err
}

res, err := client.Send(context.Background(), notification...)
res, err := client.Send(ctx, notification...)
if err != nil {
// Send Message error
logx.LogError.Error("FCM server send message error: " + err.Error())
Expand Down
9 changes: 5 additions & 4 deletions notify/notification_fcm_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package notify

import (
"context"
"os"
"testing"

Expand All @@ -27,7 +28,7 @@ func TestMissingKeyForInitFCMClient(t *testing.T) {
cfg, _ := config.LoadConf()
cfg.Android.Credential = ""
cfg.Android.KeyPath = ""
client, err := InitFCMClient(cfg)
client, err := InitFCMClient(context.Background(), cfg)

assert.Nil(t, client)
assert.Error(t, err)
Expand All @@ -47,7 +48,7 @@ func TestPushToAndroidWrongToken(t *testing.T) {
}

// Android Success count: 0, Failure count: 2
resp, err := PushToAndroid(req, cfg)
resp, err := PushToAndroid(context.Background(), req, cfg)
assert.Nil(t, err)
assert.Len(t, resp.Logs, 2)
}
Expand All @@ -68,7 +69,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
Message: "Welcome",
}

resp, err := PushToAndroid(req, cfg)
resp, err := PushToAndroid(context.Background(), req, cfg)
assert.Nil(t, err)
assert.Len(t, resp.Logs, 0)
}
Expand All @@ -87,7 +88,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
Message: "Welcome",
}

resp, err := PushToAndroid(req, cfg)
resp, err := PushToAndroid(context.Background(), req, cfg)
assert.Nil(t, err)
assert.Len(t, resp.Logs, 0)
}
Expand Down

0 comments on commit 228ec1d

Please sign in to comment.