优化本地运行的定时任务日志打印

This commit is contained in:
Yun
2025-09-28 19:49:23 +08:00
parent 16a392a266
commit e14305f66c
2 changed files with 12 additions and 8 deletions
+2 -2
View File
@@ -9,7 +9,7 @@ import (
type Options struct { type Options struct {
logger logger.Logger logger logger.Logger
location *time.Location location *time.Location
timeout time.Duration timeout time.Duration // 任务最长执行时间
usePriority bool usePriority bool
priorityVal int64 priorityVal int64
batchSize int batchSize int
@@ -20,7 +20,7 @@ func defaultOptions() Options {
return Options{ return Options{
logger: logger.NewLogger(), logger: logger.NewLogger(),
location: time.Local, location: time.Local,
timeout: time.Hour, timeout: time.Hour, //
usePriority: false, usePriority: false,
priorityVal: 0, priorityVal: 0,
batchSize: 100, batchSize: 100,
+10 -6
View File
@@ -34,6 +34,7 @@ type Single struct {
timerIndex int64 timerIndex int64
stopChan chan struct{} stopChan chan struct{}
hasRun sync.Map hasRun sync.Map
timeout time.Duration
} }
// 定时器类 // 定时器类
@@ -50,6 +51,7 @@ func InitSingle(ctx context.Context, opts ...Option) *Single {
location: op.location, location: op.location,
nextTime: time.Now(), nextTime: time.Now(),
stopChan: make(chan struct{}), stopChan: make(chan struct{}),
timeout: op.timeout,
} }
sin.wg.Add(1) sin.wg.Add(1)
@@ -393,6 +395,12 @@ func (l *Single) iterator(ctx context.Context) {
// 执行任务 // 执行任务
func (s *Single) executeTask(ctx context.Context, timer timerStr, originTime time.Time) { func (s *Single) executeTask(ctx context.Context, timer timerStr, originTime time.Time) {
// 创建带追踪ID的上下文
traceCtx := context.WithValue(ctx, "trace_id", uuid.NewV4().String())
s.logger.Infof(traceCtx, "timer: 开始执行任务 %s", timer.TaskId)
traceCtx, cancel := context.WithTimeout(traceCtx, s.timeout) // 设置执行超时
defer cancel()
select { select {
case timer.CanRunning <- struct{}{}: case timer.CanRunning <- struct{}{}:
defer func() { defer func() {
@@ -405,15 +413,10 @@ func (s *Single) executeTask(ctx context.Context, timer timerStr, originTime tim
// 检查任务是否已执行 // 检查任务是否已执行
taskKey := fmt.Sprintf("%s:%d", timer.TaskId, originTime.UnixNano()) taskKey := fmt.Sprintf("%s:%d", timer.TaskId, originTime.UnixNano())
if _, loaded := s.hasRun.LoadOrStore(taskKey, time.Now()); loaded { if _, loaded := s.hasRun.LoadOrStore(taskKey, time.Now()); loaded {
s.logger.Errorf(ctx, "timer: 任务已执行,跳过本次执行 %s", timer.TaskId) s.logger.Errorf(traceCtx, "timer: 任务已执行,跳过本次执行 %s", timer.TaskId)
return return
} }
// 创建带追踪ID的上下文
traceCtx := context.WithValue(ctx, "trace_id", uuid.NewV4().String())
traceCtx, cancel := context.WithTimeout(traceCtx, 30*time.Second) // 设置执行超时
defer cancel()
// 执行回调 // 执行回调
if err := s.doTask(traceCtx, timer, originTime); err != nil { if err := s.doTask(traceCtx, timer, originTime); err != nil {
s.logger.Errorf(traceCtx, "timer: 任务执行失败: %s", err.Error()) s.logger.Errorf(traceCtx, "timer: 任务执行失败: %s", err.Error())
@@ -421,6 +424,7 @@ func (s *Single) executeTask(ctx context.Context, timer timerStr, originTime tim
default: default:
// 任务正在执行中,跳过本次 // 任务正在执行中,跳过本次
s.logger.Infof(traceCtx, "timer: 任务正在执行中,跳过本次 %s", timer.TaskId)
} }
} }