🔊 Add more verbose logging on handling requests

This commit is contained in:
LittleSheep 2024-11-13 22:18:14 +08:00
parent aff12bd04a
commit e6da08a987
2 changed files with 40 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package provider
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit" "git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"github.com/google/uuid"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"sync" "sync"
"time" "time"
@ -15,6 +16,14 @@ func AddProvider(in NotificationProvider) {
} }
func PushNotification(in pushkit.NotificationPushRequest) error { func PushNotification(in pushkit.NotificationPushRequest) error {
requestId := uuid.NewString()
log.Debug().
Str("tk", in.Token).
Str("provider", in.Provider).
Str("topic", in.Notification.Topic).
Str("request_id", requestId).
Msg("Pushing notification...")
prov, ok := notifyProviders[in.Provider] prov, ok := notifyProviders[in.Provider]
if !ok { if !ok {
return fmt.Errorf("provider not found") return fmt.Errorf("provider not found")
@ -26,18 +35,27 @@ func PushNotification(in pushkit.NotificationPushRequest) error {
Str("tk", in.Token). Str("tk", in.Token).
Str("provider", prov.GetName()). Str("provider", prov.GetName()).
Dur("elapsed", time.Since(start)). Dur("elapsed", time.Since(start)).
Str("request_id", requestId).
Msg("Push notification failed once") Msg("Push notification failed once")
} else { } else {
log.Debug(). log.Debug().
Str("tk", in.Token). Str("tk", in.Token).
Str("provider", prov.GetName()). Str("provider", prov.GetName()).
Dur("elapsed", time.Since(start)). Dur("elapsed", time.Since(start)).
Str("request_id", requestId).
Msg("Pushed one notification") Msg("Pushed one notification")
} }
return err return err
} }
func PushNotificationBatch(in pushkit.NotificationPushBatchRequest) { func PushNotificationBatch(in pushkit.NotificationPushBatchRequest) {
requestId := uuid.NewString()
log.Debug().
Int("count", len(in.Tokens)).
Str("topic", in.Notification.Topic).
Str("request_id", requestId).
Msg("Pushing notification batch...")
var wg sync.WaitGroup var wg sync.WaitGroup
for idx, key := range in.Providers { for idx, key := range in.Providers {
prov, ok := notifyProviders[key] prov, ok := notifyProviders[key]
@ -54,12 +72,14 @@ func PushNotificationBatch(in pushkit.NotificationPushBatchRequest) {
Str("tk", in.Tokens[idx]). Str("tk", in.Tokens[idx]).
Str("provider", prov.GetName()). Str("provider", prov.GetName()).
Dur("elapsed", time.Since(start)). Dur("elapsed", time.Since(start)).
Str("request_id", requestId).
Msg("Push notification failed once") Msg("Push notification failed once")
} else { } else {
log.Debug(). log.Debug().
Str("tk", in.Tokens[idx]). Str("tk", in.Tokens[idx]).
Str("provider", prov.GetName()). Str("provider", prov.GetName()).
Dur("elapsed", time.Since(start)). Dur("elapsed", time.Since(start)).
Str("request_id", requestId).
Msg("Pushed one notification") Msg("Pushed one notification")
} }
}() }()

View File

@ -4,13 +4,22 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit" "git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"github.com/google/uuid"
"github.com/jordan-wright/email" "github.com/jordan-wright/email"
"github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"net/smtp" "net/smtp"
"net/textproto" "net/textproto"
) )
func SendMail(target string, in pushkit.EmailData) error { func SendMail(target string, in pushkit.EmailData) error {
requestId := uuid.NewString()
log.Debug().
Str("request_id", requestId).
Str("to", target).
Str("subject", in.Subject).
Msg("Sending email...")
mail := &email.Email{ mail := &email.Email{
To: []string{target}, To: []string{target},
From: viper.GetString("mailer.name"), From: viper.GetString("mailer.name"),
@ -23,7 +32,7 @@ func SendMail(target string, in pushkit.EmailData) error {
if in.HTML != nil { if in.HTML != nil {
mail.HTML = []byte(*in.HTML) mail.HTML = []byte(*in.HTML)
} }
return mail.SendWithTLS( err := mail.SendWithTLS(
fmt.Sprintf("%s:%d", viper.GetString("mailer.smtp_host"), viper.GetInt("mailer.smtp_port")), fmt.Sprintf("%s:%d", viper.GetString("mailer.smtp_host"), viper.GetInt("mailer.smtp_port")),
smtp.PlainAuth( smtp.PlainAuth(
"", "",
@ -33,4 +42,14 @@ func SendMail(target string, in pushkit.EmailData) error {
), ),
&tls.Config{ServerName: viper.GetString("mailer.smtp_host")}, &tls.Config{ServerName: viper.GetString("mailer.smtp_host")},
) )
if err != nil {
log.Warn().
Str("request_id", requestId).
Str("to", target).
Str("subject", in.Subject).
Err(err).Msg("Failed to send email...")
}
return err
} }