feat: 添加分布式锁以防止多个任务同时执行

This commit is contained in:
TsMask
2025-10-11 15:53:15 +08:00
parent 13b2ce3e64
commit 7261f80826
20 changed files with 192 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package cron
import (
"errors"
"fmt"
"time"
@@ -194,6 +195,9 @@ func (qj QueueJob) Run() {
// 获取队列处理器接口实现
processor := *job.queueProcessor
result, err := processor.Execute(job.Data)
if errors.Is(err, ErrTaskRunning) {
return
}
if err != nil {
job.Status = Failed
cronLog.Error(err, "failed", job)

View File

@@ -2,6 +2,7 @@ package cron
import (
"encoding/json"
"errors"
"time"
"be.ems/src/framework/constants"
@@ -123,3 +124,6 @@ type JobData struct {
// 定时任务调度表记录信息
SysJob monitorModel.SysJob
}
// ErrTaskRunning 任务正在运行错误
var ErrTaskRunning = errors.New("task is running")