mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 07:57:21 +08:00
feat: 添加计划任务
This commit is contained in:
52
internal/queuejob/process_task.go
Normal file
52
internal/queuejob/process_task.go
Normal file
@@ -0,0 +1,52 @@
|
||||
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 (receiver *ProcessTask) Handle(args ...any) error {
|
||||
taskID, ok := args[0].(uint)
|
||||
if !ok {
|
||||
return errors.New("参数错误")
|
||||
}
|
||||
receiver.taskID = taskID
|
||||
|
||||
task, err := receiver.taskRepo.Get(taskID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = receiver.taskRepo.UpdateStatus(taskID, biz.TaskStatusRunning); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = shell.Execf(task.Shell); err != nil { // nolint: govet
|
||||
return err
|
||||
}
|
||||
|
||||
if err = receiver.taskRepo.UpdateStatus(taskID, biz.TaskStatusSuccess); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (receiver *ProcessTask) ErrHandle(err error) {
|
||||
_ = receiver.taskRepo.UpdateStatus(receiver.taskID, biz.TaskStatusFailed)
|
||||
}
|
||||
Reference in New Issue
Block a user