回调推送链路添加详细日志
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -236,11 +237,13 @@ func (s *CallbackService) dispatchOne(ctx context.Context, id uint) (bool, bool,
|
|||||||
|
|
||||||
resp, err := s.httpClient.Do(req)
|
resp, err := s.httpClient.Do(req)
|
||||||
if err != nil {
|
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()))
|
return false, true, s.recordCallbackFailure(delivery, 0, truncateCallbackError(err.Error()))
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||||
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
|
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{}{
|
return true, true, s.db.Model(&model.CallbackDelivery{}).Where("id = ?", delivery.ID).Updates(map[string]interface{}{
|
||||||
"status": model.CallbackDeliveryDelivered,
|
"status": model.CallbackDeliveryDelivered,
|
||||||
"attempts": delivery.Attempts + 1,
|
"attempts": delivery.Attempts + 1,
|
||||||
@@ -249,6 +252,7 @@ func (s *CallbackService) dispatchOne(ctx context.Context, id uint) (bool, bool,
|
|||||||
"delivered_at": time.Now(),
|
"delivered_at": time.Now(),
|
||||||
}).Error
|
}).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))
|
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)
|
ticker := time.NewTicker(3 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
for {
|
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 {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user