2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 18:27:13 +08:00
Files
panel/internal/queuejob/process_task.go
2024-10-16 04:09:59 +08:00

53 lines
987 B
Go

package queuejob
import (
"errors"
"github.com/TheTNB/panel/internal/biz"
"github.com/TheTNB/panel/pkg/shell"
)
// ProcessTask 处理面板任务
type ProcessTask struct {
taskRepo biz.TaskRepo
taskID uint
}
// NewProcessTask 实例化 ProcessTask
func NewProcessTask(taskRepo biz.TaskRepo) *ProcessTask {
return &ProcessTask{
taskRepo: taskRepo,
}
}
func (r *ProcessTask) Handle(args ...any) error {
taskID, ok := args[0].(uint)
if !ok {
return errors.New("参数错误")
}
r.taskID = taskID
task, err := r.taskRepo.Get(taskID)
if err != nil {
return err
}
if err = r.taskRepo.UpdateStatus(taskID, biz.TaskStatusRunning); err != nil {
return err
}
if _, err = shell.Execf(task.Shell); err != nil { // nolint: govet
return err
}
if err = r.taskRepo.UpdateStatus(taskID, biz.TaskStatusSuccess); err != nil {
return err
}
return nil
}
func (r *ProcessTask) ErrHandle(err error) {
_ = r.taskRepo.UpdateStatus(r.taskID, biz.TaskStatusFailed)
}