From e1d20e3ec9d89cc5ee2b1b09e54597ece5a6f930 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 25 Jun 2025 16:29:15 +0800 Subject: [PATCH] fix: Optimize snapshot and snapshot recovery mechanism (#9279) --- agent/app/service/snapshot_create.go | 5 ++-- agent/app/service/snapshot_recover.go | 8 +++++- agent/app/task/task.go | 36 ++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/agent/app/service/snapshot_create.go b/agent/app/service/snapshot_create.go index 5c5969c60..2a597e2e1 100644 --- a/agent/app/service/snapshot_create.go +++ b/agent/app/service/snapshot_create.go @@ -106,13 +106,14 @@ func (u *SnapshotService) SnapshotReCreate(id uint) error { return err } req.TaskID = taskModel.ID - taskItem, err := task.NewTaskWithOps(req.Name, task.TaskCreate, task.TaskScopeSnapshot, req.TaskID, req.ID) + taskItem, err := task.ReNewTaskWithOps(req.Name, task.TaskCreate, task.TaskScopeSnapshot, req.TaskID, req.ID) if err != nil { global.LOG.Errorf("new task for create snapshot failed, err: %v", err) return err } + _ = snapshotRepo.Update(req.ID, map[string]interface{}{"status": constant.StatusWaiting, "message": ""}) go func() { - _ = handleSnapshot(req, taskItem, 0, 0, 0) + _ = handleSnapshot(req, taskItem, 0, 3, 0) }() return nil diff --git a/agent/app/service/snapshot_recover.go b/agent/app/service/snapshot_recover.go index 24090bb07..4a1468386 100644 --- a/agent/app/service/snapshot_recover.go +++ b/agent/app/service/snapshot_recover.go @@ -67,7 +67,13 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error { } else { _ = snapshotRepo.Update(snap.ID, map[string]interface{}{"task_recover_id": req.TaskID}) } - taskItem, err := task.NewTaskWithOps(snap.Name, task.TaskRecover, task.TaskScopeSnapshot, req.TaskID, snap.ID) + + var taskItem *task.Task + if req.IsNew { + taskItem, err = task.NewTaskWithOps(snap.Name, task.TaskRecover, task.TaskScopeSnapshot, req.TaskID, snap.ID) + } else { + taskItem, err = task.ReNewTaskWithOps(snap.Name, task.TaskRecover, task.TaskScopeSnapshot, req.TaskID, snap.ID) + } if err != nil { global.LOG.Errorf("new task for create snapshot failed, err: %v", err) return err diff --git a/agent/app/task/task.go b/agent/app/task/task.go index 7e5ba3a33..044740043 100644 --- a/agent/app/task/task.go +++ b/agent/app/task/task.go @@ -142,6 +142,40 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e return task, nil } +func ReNewTaskWithOps(resourceName, operate, scope, taskID string, resourceID uint) (*Task, error) { + return ReNewTask(GetTaskName(resourceName, operate, scope), operate, scope, taskID, resourceID) +} +func ReNewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, error) { + if taskID == "" { + return NewTask(name, operate, taskScope, taskID, resourceID) + } + logDir := path.Join(global.Dir.TaskDir, taskScope) + if _, err := os.Stat(logDir); err != nil { + if err = os.MkdirAll(logDir, constant.DirPerm); err != nil { + return nil, fmt.Errorf("failed to create log directory: %w", err) + } + } + logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log") + file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm) + if err != nil { + return nil, fmt.Errorf("failed to open log file: %w", err) + } + writer := bufio.NewWriter(file) + logger := log.New(file, "", log.LstdFlags) + taskModel := &model.Task{ + ID: taskID, + Name: name, + Type: taskScope, + LogFile: logPath, + Status: constant.StatusExecuting, + ResourceID: resourceID, + Operate: operate, + } + taskRepo := repo.NewITaskRepo() + task := &Task{Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: taskModel, Writer: writer} + return task, nil +} + func (t *Task) AddSubTask(name string, action ActionFunc, rollback RollbackFunc) { subTask := &SubTask{RootTask: t, Name: name, Retry: 0, Timeout: 30 * time.Minute, Action: action, Rollback: rollback} t.SubTasks = append(t.SubTasks, subTask) @@ -158,7 +192,7 @@ func (t *Task) AddSubTaskWithOps(name string, action ActionFunc, rollback Rollba } func (t *Task) AddSubTaskWithAliasAndOps(key string, action ActionFunc, rollback RollbackFunc, retry int, timeout time.Duration) { - subTask := &SubTask{RootTask: t, Name: i18n.GetMsgByKey(key), Retry: retry, Timeout: timeout, Action: action, Rollback: rollback} + subTask := &SubTask{RootTask: t, Name: i18n.GetMsgByKey(key), StepAlias: key, Retry: retry, Timeout: timeout, Action: action, Rollback: rollback} t.SubTasks = append(t.SubTasks, subTask) }