From c0f115d97d5c3aca77f081f2997a17dfd42e5d7c Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Wed, 6 Mar 2024 17:25:00 +0800 Subject: [PATCH] fix: manual sync alarm failed while abnormal NEs --- features/fm/alarm.go | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/features/fm/alarm.go b/features/fm/alarm.go index c8d537e3..6b054551 100644 --- a/features/fm/alarm.go +++ b/features/fm/alarm.go @@ -115,6 +115,27 @@ var ( CustomUriAlarmsFmt = config.UriPrefix + "/faultManagement/v1/elementType/%s/objectType/alarms" ) +var client = resty.New() + +func init() { + /* + client. + SetTimeout(10 * time.Second). + SetRetryCount(1). + SetRetryWaitTime(1 * time.Second). + SetRetryMaxWaitTime(2 * time.Second). + SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) { + return 0, errors.New("quota exceeded") + }) + */ + client. + SetTimeout(time.Duration(500 * time.Millisecond)) + // SetRetryCount(1). + // SetRetryWaitTime(time.Duration(1 * time.Second)). + // SetRetryMaxWaitTime(time.Duration(2 * time.Second)) + //client.SetTimeout(2 * time.Second) +} + var xEngine *xorm.Engine type DatabaseClient struct { @@ -439,8 +460,8 @@ func PostAlarmFromNF(w http.ResponseWriter, r *http.Request) { if err = AlarmEmailForward(&alarmData); err != nil { log.Error("Failed to AlarmEmailForward:", err) } - if err = AlarmForwardBySMS(&alarmData); err != nil { - log.Error("Failed to AlarmForwardBySMS:", err) + if err = AlarmForwardBySMSC(&alarmData); err != nil { + log.Error("Failed to AlarmForwardBySMSC:", err) } } } @@ -482,7 +503,7 @@ func GetAlarmFromNF(w http.ResponseWriter, r *http.Request) { apiUri := fmt.Sprintf(UriAlarmsFmt, strings.ToLower(ne.NeType)) requestURI2NF := fmt.Sprintf("%s%s", hostUri, apiUri) log.Debug("requestURI2NF: Get ", requestURI2NF) - client := resty.New() + // client := resty.New() response, err := client.R(). EnableTrace(). SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}). @@ -498,7 +519,12 @@ func GetAlarmFromNF(w http.ResponseWriter, r *http.Request) { switch response.StatusCode() { case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted: body := response.Body() - log.Debug("Request body:", string(body)) + if len(body) == 0 || body == nil { + log.Infof("Empty alarm body from neType=%s, neId=%s", ne.NeType, ne.NeId) + //services.ResponseInternalServerError500ProcessError(w, err) + continue + } + //log.Debug("Request body:", string(body)) err = json.Unmarshal(body, &alarmArray) if err != nil { @@ -518,7 +544,7 @@ func GetAlarmFromNF(w http.ResponseWriter, r *http.Request) { continue } if alarmArray == nil { - log.Info("Not found sync alarms, neType=%s, neId=%s", ne.NeType, ne.NeId) + log.Infof("Not found sync alarms, neType=%s, neId=%s", ne.NeType, ne.NeId) //services.ResponseInternalServerError500ProcessError(w, err) continue } @@ -536,7 +562,7 @@ func GetAlarmFromNF(w http.ResponseWriter, r *http.Request) { Where("ne_type=? and ne_id=? and alarm_id=? and alarm_status=1", alarmData.NeType, alarmData.NeId, alarmData.AlarmId). Exist() if err == nil || !exist { - log.Info("Not found active alarm: ne_id=%s, alarm_id=%s", alarmData.NeId, alarmData.AlarmId) + log.Infof("Not found active alarm: ne_id=%s, alarm_id=%s", alarmData.NeId, alarmData.AlarmId) continue } alarmData.ClearType = ClearTypeAutoClear @@ -708,8 +734,8 @@ func GetAlarmFromNF(w http.ResponseWriter, r *http.Request) { if err = AlarmEmailForward(&alarmData); err != nil { log.Error("Failed to AlarmEmailForward:", err) } - if err = AlarmForwardBySMS(&alarmData); err != nil { - log.Error("Failed to AlarmForwardBySMS:", err) + if err = AlarmForwardBySMSC(&alarmData); err != nil { + log.Error("Failed to AlarmForwardBySMSC:", err) } } } @@ -717,5 +743,5 @@ func GetAlarmFromNF(w http.ResponseWriter, r *http.Request) { } } } - services.ResponseStatusOK200Null(w) + services.ResponseStatusOK204NoContent(w) }