2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 03:07:20 +08:00

feat: 防止任务重复提交

This commit is contained in:
耗子
2024-11-04 17:47:39 +08:00
parent c308b8e42c
commit e18d40b0ab
2 changed files with 11 additions and 1 deletions

View File

@@ -8,6 +8,6 @@ import (
)
func initQueue() {
app.Queue = queue.New(40)
app.Queue = queue.New(100)
go app.Queue.Run(context.Background())
}

View File

@@ -1,6 +1,7 @@
package data
import (
"fmt"
"log/slog"
"sync"
@@ -47,9 +48,18 @@ func (r *taskRepo) UpdateStatus(id uint, status biz.TaskStatus) error {
}
func (r *taskRepo) Push(task *biz.Task) error {
var count int64
if err := app.Orm.Model(&biz.Task{}).Where("shell = ? and (status = ? or status = ?)", task.Shell, biz.TaskStatusWaiting, biz.TaskStatusRunning).Count(&count).Error; err != nil {
return err
}
if count > 0 {
return fmt.Errorf("任务重复提交,请等待上一个任务完成")
}
if err := app.Orm.Create(task).Error; err != nil {
return err
}
return app.Queue.Push(queuejob.NewProcessTask(r), []any{
task.ID,
})