Skip to content

Commit 0f642e0

Browse files
committed
modify: encapsulate defer code into methods
1 parent a854b9b commit 0f642e0

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

sqle/api/controller/v2/audit_plan.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,7 @@ func UploadInstanceAuditPlanSQLs(c echo.Context) error {
534534
}
535535

536536
l := log.NewEntry()
537-
defer func() {
538-
status := model.LastCollectionNormal
539-
if err != nil {
540-
l.Error(errors.NewAuditPlanExecuteExtractErr(err, ap.InstanceID, ap.Type))
541-
status = model.LastCollectionAbnormal
542-
}
543-
updateErr := s.UpdateAuditPlanInfoByAPID(ap.ID, map[string]interface{}{"last_collection_status": status})
544-
if updateErr != nil {
545-
l.Errorf("update audit plan task info collection status status failed, error : %v", updateErr)
546-
}
547-
}()
537+
defer auditplan.ProcessAuditPlanStatusAndLogError(l, ap.ID, ap.InstanceID, ap.Type, &err)
548538
// 当scannerd执行出现错误时,将任务状态改为异常并日志打印错误信息
549539
if req.ErrorMessage != "" && len(req.SQLs) == 0 {
550540
err = fmt.Errorf("error message received: %s", req.ErrorMessage)

sqle/server/auditplan/task_wrap.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,8 @@ func (at *TaskWrapper) stopCollect() error {
228228

229229
func (at *TaskWrapper) extractSQL() {
230230
var err error
231-
defer func() {
232-
status := model.LastCollectionNormal
233-
if err != nil {
234-
at.logger.Error(sqleErr.NewAuditPlanExecuteExtractErr(err, at.ap.InstanceID, at.ap.Type))
235-
status = model.LastCollectionAbnormal
236-
}
237-
updateErr := at.persist.UpdateAuditPlanInfoByAPID(at.ap.ID, map[string]interface{}{"last_collection_status": status})
238-
if updateErr != nil {
239-
at.logger.Errorf("update audit plan task info collection status status failed, error : %v", updateErr)
240-
}
241-
}()
231+
defer ProcessAuditPlanStatusAndLogError(at.logger, at.ap.ID, at.ap.InstanceID, at.ap.Type, &err)
232+
242233
collectionTime := time.Now()
243234
sqls, err := at.collect.ExtractSQL(at.logger, at.ap, at.persist)
244235
if err != nil {
@@ -266,6 +257,19 @@ func (at *TaskWrapper) extractSQL() {
266257
}
267258
}
268259

260+
func ProcessAuditPlanStatusAndLogError(l *logrus.Entry, auditPlanId uint, instanceID, auditPlnType string, err *error) {
261+
s := model.GetStorage()
262+
status := model.LastCollectionNormal
263+
if err != nil {
264+
l.Error(sqleErr.NewAuditPlanExecuteExtractErr(*err, instanceID, auditPlnType))
265+
status = model.LastCollectionAbnormal
266+
}
267+
updateErr := s.UpdateAuditPlanInfoByAPID(auditPlanId, map[string]interface{}{"last_collection_status": status})
268+
if updateErr != nil {
269+
l.Errorf("update audit plan task info collection status status failed, error : %v", updateErr)
270+
}
271+
}
272+
269273
func (at *TaskWrapper) loop(cancel chan struct{}, interval time.Duration) {
270274
if interval == 0 {
271275
at.logger.Warnf("task(%v) loop interval can not be zero", at.ap.Name)

0 commit comments

Comments
 (0)