optimal task job get

This commit is contained in:
liuzhihang1
2025-02-13 11:24:28 +08:00
parent 1852f7ab4c
commit b2a1ef94e9
2 changed files with 26 additions and 25 deletions

View File

@@ -13,11 +13,10 @@ import (
) )
func (t *taskLogic) RunTaskById(id int) error { func (t *taskLogic) RunTaskById(id int) error {
v, ok := t.taskJobMap.Load(id) task, err := t.getTaskJob(id)
if !ok { if err != nil {
return errors.New("don't exist task id") return errors.New("id不存在")
} }
task := v.(*model.TaskJob)
if task.Running { if task.Running {
return errors.New("task is running") return errors.New("task is running")
} }
@@ -68,9 +67,8 @@ func (t *taskLogic) run(ctx context.Context, data *model.TaskJob) {
log.Logger.Infow("任务执行成功", "target", data.Task.OperationTarget) log.Logger.Infow("任务执行成功", "target", data.Task.OperationTarget)
if data.Task.NextId != nil { if data.Task.NextId != nil {
v, ok := t.taskJobMap.Load(*data.Task.NextId) nextTask, err := t.getTaskJob(*data.Task.NextId)
nextTask := v.(*model.TaskJob) if err != nil {
if !ok {
log.Logger.Errorw("无法获取到下一个节点,结束任务", "nextId", data.Task.NextId) log.Logger.Errorw("无法获取到下一个节点,结束任务", "nextId", data.Task.NextId)
return return
} }

View File

@@ -21,6 +21,14 @@ type taskLogic struct {
var TaskLogic taskLogic var TaskLogic taskLogic
func (t *taskLogic) getTaskJob(id int) (*model.TaskJob, error) {
c, ok := t.taskJobMap.Load(id)
if !ok {
return nil, errors.New("don't exist this task id")
}
return c.(*model.TaskJob), nil
}
func (t *taskLogic) InitTaskJob() { func (t *taskLogic) InitTaskJob() {
for _, v := range repository.TaskRepository.GetAllTask() { for _, v := range repository.TaskRepository.GetAllTask() {
tj := &model.TaskJob{ tj := &model.TaskJob{
@@ -61,11 +69,10 @@ func (t *taskLogic) cronHandle(data *model.TaskJob) func() {
} }
func (t *taskLogic) StopTaskJob(id int) error { func (t *taskLogic) StopTaskJob(id int) error {
c, ok := t.taskJobMap.Load(id) taskJob, err := t.getTaskJob(id)
if !ok { if err != nil {
return errors.New("id不存在") return errors.New("don't exist this task id")
} }
taskJob := c.(*model.TaskJob)
if taskJob.Running { if taskJob.Running {
taskJob.Cancel() taskJob.Cancel()
} }
@@ -73,23 +80,21 @@ func (t *taskLogic) StopTaskJob(id int) error {
} }
func (t *taskLogic) StartTaskJob(id int) error { func (t *taskLogic) StartTaskJob(id int) error {
c, ok := t.taskJobMap.Load(id) taskJob, err := t.getTaskJob(id)
if !ok { if err != nil {
return errors.New("id不存在") return errors.New("don't exist this task id")
} }
TaskJob := c.(*model.TaskJob) taskJob.Cron.Run()
TaskJob.Cron.Run()
return nil return nil
} }
func (t *taskLogic) GetAllTaskJob() []model.TaskVo { func (t *taskLogic) GetAllTaskJob() []model.TaskVo {
result := repository.TaskRepository.GetAllTaskWithProcessName() result := repository.TaskRepository.GetAllTaskWithProcessName()
for i, v := range result { for i, v := range result {
item, ok := t.taskJobMap.Load(v.Id) task, err := t.getTaskJob(v.Id)
if !ok { if err != nil {
continue continue
} }
task := item.(*model.TaskJob)
result[i].Id = task.Task.Id result[i].Id = task.Task.Id
result[i].Running = task.Running result[i].Running = task.Running
result[i].Enable = task.Task.Enable result[i].Enable = task.Task.Enable
@@ -133,11 +138,10 @@ func (t *taskLogic) CreateTask(data model.Task) error {
} }
func (t *taskLogic) EditTask(data model.Task) error { func (t *taskLogic) EditTask(data model.Task) error {
v, ok := t.taskJobMap.Load(data.Id) tj, err := t.getTaskJob(data.Id)
if !ok { if err != nil {
return errors.New("don't exist this task id") return errors.New("don't exist this task id")
} }
tj := v.(*model.TaskJob)
if tj.Running { if tj.Running {
return errors.New("can't edit when task is running") return errors.New("can't edit when task is running")
} }
@@ -162,11 +166,10 @@ func (t *taskLogic) EditTask(data model.Task) error {
} }
func (t *taskLogic) EditTaskEnable(id int, status bool) error { func (t *taskLogic) EditTaskEnable(id int, status bool) error {
v, ok := t.taskJobMap.Load(id) tj, err := t.getTaskJob(id)
if !ok { if err != nil {
return errors.New("don't exist this task id") return errors.New("don't exist this task id")
} }
tj := v.(*model.TaskJob)
tj.Task.Enable = status tj.Task.Enable = status
repository.TaskRepository.EditTaskEnable(id, status) repository.TaskRepository.EditTaskEnable(id, status)
if tj.Cron != nil { if tj.Cron != nil {