(name, operate, taskScope, taskID string, resourceID uint)
| 178 | return ReNewTask(GetTaskName(resourceName, operate, scope), operate, scope, taskID, resourceID) |
| 179 | } |
| 180 | func ReNewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, error) { |
| 181 | taskRepo := repo.NewITaskRepo() |
| 182 | taskItem, _ := taskRepo.GetFirst(taskRepo.WithByID(taskID)) |
| 183 | if taskItem.ID == "" { |
| 184 | return NewTask(name, operate, taskScope, taskID, resourceID) |
| 185 | } |
| 186 | logDir := path.Join(global.Dir.TaskDir, taskScope) |
| 187 | if _, err := os.Stat(logDir); err != nil { |
| 188 | if err = os.MkdirAll(logDir, constant.DirPerm); err != nil { |
| 189 | return nil, fmt.Errorf("failed to create log directory: %w", err) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log") |
| 194 | logger := logrus.New() |
| 195 | logger.SetFormatter(&SimpleFormatter{}) |
| 196 | logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm) |
| 197 | if err != nil { |
| 198 | return nil, fmt.Errorf("failed to open log file: %w", err) |
| 199 | } |
| 200 | logger.SetOutput(logFile) |
| 201 | logger.Print("\n --------------------------------------------------- \n") |
| 202 | taskItem.Status = constant.StatusExecuting |
| 203 | task := &Task{Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: &taskItem} |
| 204 | task.updateTask(&taskItem) |
| 205 | return task, nil |
| 206 | } |
| 207 | |
| 208 | func (t *Task) AddSubTask(name string, action ActionFunc, rollback RollbackFunc) { |
| 209 | subTask := &SubTask{RootTask: t, Name: name, Retry: 0, Timeout: 30 * time.Minute, Action: action, Rollback: rollback} |
no test coverage detected