回调推送链路添加详细日志

This commit is contained in:
yml2213
2026-07-30 20:38:40 +08:00
parent 1984bf6f7a
commit 2258e940b6
+10 -1
View File
@@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
@@ -236,11 +237,13 @@ func (s *CallbackService) dispatchOne(ctx context.Context, id uint) (bool, bool,
resp, err := s.httpClient.Do(req)
if err != nil {
log.Printf("[callback] push fail event=%s event_id=%s merchant_id=%d url=%s err=%s", delivery.Event, delivery.EventID, delivery.MerchantID, delivery.CallbackSubscription.URL, truncateCallbackError(err.Error()))
return false, true, s.recordCallbackFailure(delivery, 0, truncateCallbackError(err.Error()))
}
defer resp.Body.Close()
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
log.Printf("[callback] push ok event=%s event_id=%s merchant_id=%d url=%s status=%d", delivery.Event, delivery.EventID, delivery.MerchantID, delivery.CallbackSubscription.URL, resp.StatusCode)
return true, true, s.db.Model(&model.CallbackDelivery{}).Where("id = ?", delivery.ID).Updates(map[string]interface{}{
"status": model.CallbackDeliveryDelivered,
"attempts": delivery.Attempts + 1,
@@ -249,6 +252,7 @@ func (s *CallbackService) dispatchOne(ctx context.Context, id uint) (bool, bool,
"delivered_at": time.Now(),
}).Error
}
log.Printf("[callback] push fail event=%s event_id=%s merchant_id=%d url=%s status=%d body=%s", delivery.Event, delivery.EventID, delivery.MerchantID, delivery.CallbackSubscription.URL, resp.StatusCode, truncateCallbackError(string(body)))
return false, true, s.recordCallbackFailure(delivery, resp.StatusCode, string(body))
}
@@ -273,7 +277,12 @@ func (s *CallbackService) Run(ctx context.Context) {
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
_, _, _ = s.DispatchDue(ctx, 50)
successes, failures, err := s.DispatchDue(ctx, 50)
if err != nil {
log.Printf("[callback] dispatch error: %v", err)
} else if successes > 0 || failures > 0 {
log.Printf("[callback] dispatch done successes=%d failures=%d", successes, failures)
}
select {
case <-ctx.Done():
return