Skip to content

Commit

Permalink
modify: encapsulate defer code into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanghc committed Jan 17, 2025
1 parent a854b9b commit 0f642e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
12 changes: 1 addition & 11 deletions sqle/api/controller/v2/audit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,7 @@ func UploadInstanceAuditPlanSQLs(c echo.Context) error {
}

l := log.NewEntry()
defer func() {
status := model.LastCollectionNormal
if err != nil {
l.Error(errors.NewAuditPlanExecuteExtractErr(err, ap.InstanceID, ap.Type))
status = model.LastCollectionAbnormal
}
updateErr := s.UpdateAuditPlanInfoByAPID(ap.ID, map[string]interface{}{"last_collection_status": status})
if updateErr != nil {
l.Errorf("update audit plan task info collection status status failed, error : %v", updateErr)
}
}()
defer auditplan.ProcessAuditPlanStatusAndLogError(l, ap.ID, ap.InstanceID, ap.Type, &err)
// 当scannerd执行出现错误时,将任务状态改为异常并日志打印错误信息
if req.ErrorMessage != "" && len(req.SQLs) == 0 {
err = fmt.Errorf("error message received: %s", req.ErrorMessage)
Expand Down
26 changes: 15 additions & 11 deletions sqle/server/auditplan/task_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,8 @@ func (at *TaskWrapper) stopCollect() error {

func (at *TaskWrapper) extractSQL() {
var err error
defer func() {
status := model.LastCollectionNormal
if err != nil {
at.logger.Error(sqleErr.NewAuditPlanExecuteExtractErr(err, at.ap.InstanceID, at.ap.Type))
status = model.LastCollectionAbnormal
}
updateErr := at.persist.UpdateAuditPlanInfoByAPID(at.ap.ID, map[string]interface{}{"last_collection_status": status})
if updateErr != nil {
at.logger.Errorf("update audit plan task info collection status status failed, error : %v", updateErr)
}
}()
defer ProcessAuditPlanStatusAndLogError(at.logger, at.ap.ID, at.ap.InstanceID, at.ap.Type, &err)

collectionTime := time.Now()
sqls, err := at.collect.ExtractSQL(at.logger, at.ap, at.persist)
if err != nil {
Expand Down Expand Up @@ -266,6 +257,19 @@ func (at *TaskWrapper) extractSQL() {
}
}

func ProcessAuditPlanStatusAndLogError(l *logrus.Entry, auditPlanId uint, instanceID, auditPlnType string, err *error) {
s := model.GetStorage()
status := model.LastCollectionNormal
if err != nil {
l.Error(sqleErr.NewAuditPlanExecuteExtractErr(*err, instanceID, auditPlnType))
status = model.LastCollectionAbnormal
}
updateErr := s.UpdateAuditPlanInfoByAPID(auditPlanId, map[string]interface{}{"last_collection_status": status})
if updateErr != nil {
l.Errorf("update audit plan task info collection status status failed, error : %v", updateErr)
}
}

func (at *TaskWrapper) loop(cancel chan struct{}, interval time.Duration) {
if interval == 0 {
at.logger.Warnf("task(%v) loop interval can not be zero", at.ap.Name)
Expand Down

0 comments on commit 0f642e0

Please sign in to comment.