refactor UpdateHpcTaskStatus; streamline task synchronization logic, improve error handling, and enhance status reporting

Signed-off-by: jagger <cossjie@foxmail.com>
This commit is contained in:
jagger 2025-07-30 11:44:05 +08:00
parent 80f07495d5
commit 855d02da5c
1 changed files with 4 additions and 3 deletions

View File

@ -128,7 +128,7 @@ func reportHpcStatusMessages(svc *svc.ServiceContext, task *types.TaskModel, hpc
func UpdateHpcTaskStatus(svc *svc.ServiceContext) {
// 1. 查询需要同步的 HPC 任务
var hpcTasks []*models.TaskHpc
sqlStr := `SELECT * FROM task_hpc WHERE job_id != '' AND status NOT IN ('Failed', 'Completed', 'Cancelled') OR start_time < created_time ORDER BY created_time DESC LIMIT 10`
sqlStr := `SELECT * FROM task_hpc WHERE job_id != '' AND status NOT IN ('Failed', 'Completed', 'Cancelled') ORDER BY created_time DESC LIMIT 10`
if err := svc.DbEngin.Raw(sqlStr).Scan(&hpcTasks).Error; err != nil {
logx.Errorf("Failed to query HPC tasks for sync: %v", err)
return
@ -199,10 +199,11 @@ func UpdateHpcTaskStatus(svc *svc.ServiceContext) {
// 6. 在事务中更新数据库
err = svc.DbEngin.Transaction(func(tx *gorm.DB) error {
if err := tx.Save(task).Error; err != nil {
task.UpdatedTime = time.Now().Format(constants.Layout)
if err := tx.Table("task").Updates(task).Error; err != nil {
return fmt.Errorf("failed to update task table: %w", err)
}
if err := tx.Save(hpc).Error; err != nil {
if err := tx.Table("task_hpc").Updates(hpc).Error; err != nil {
return fmt.Errorf("failed to update hpc_task table: %w", err)
}
return nil