Skip to content

Conversation

@dezhishen
Copy link
Member

@dezhishen dezhishen commented Jan 11, 2026

Description / 描述

这是任务调度器的核心能力封装,基于 github.com/go-co-op/gocron/v2

  1. 将任务参数封装为 JobBuilder,降低 Job定义难度,隐藏 gocron 内部细节,降低业务侧代码与gocron的耦合
  2. scheduler 本身的options 未封装,原因是:scheduler由主干来控制,不涉及到业务侧的使用,暴露原始参数,方便处理
  3. tags []string 处理为 labels map[string]string,方便外部管理,如区分job所属用户

Motivation and Context / 背景

目前部分需求依赖定时任务的机制,让服务能定时处理某些事项。

Closes #XXXX

Relates to #XXXX

How Has This Been Tested? / 测试

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it’s needed).
    我已相应更新了相关仓库(若适用)。

This comment was marked as outdated.

This comment was marked as outdated.

@dezhishen dezhishen changed the title feat(scheuler): 增加调度器的相关方法 feat(scheuler): add the module of scheduler Jan 11, 2026

This comment was marked as outdated.

This comment was marked as outdated.

@dezhishen dezhishen added the Module: Task Task, scheduling and other goroutine-based features related label Jan 13, 2026

This comment was marked as outdated.

@dezhishen dezhishen changed the title feat(scheuler): add the module of scheduler feat(scheduler): add the module of scheduler Jan 13, 2026
@dezhishen dezhishen requested a review from Copilot January 14, 2026 10:50

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

@dezhishen dezhishen marked this pull request as ready for review January 15, 2026 06:08
@dezhishen dezhishen requested a review from KirCute January 15, 2026 09:31
jobName string
cron gocron.JobDefinition
disabled bool
labels JobLabels
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个labels是做什么的,我没有看出来

Comment on lines +18 to +19
runner JobRunner
params []any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个runner直接写成闭包会不会好一些,本来对于调用者而言params大概率并不是一开始就是组织成[]any这种结构的,现在这种写法还要用反射检查参数数量和类型,让调用者直接提供func(ctx context.Context) error类型的闭包不仅写法更简单,还能在调用测实现编译期安全

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我又想了一下,我觉得这块可以参考驱动的设计,每种定时任务就像一种驱动,params就是驱动的addition,交给业务层去序列化和反序列化,jobBuilder可以只接收一个model.ScheduleTask结构体,从其config得到name,uuid,disabled,crontab等信息,然后model.ScheduleTask固定实现一个Execute(ctx context.Context) error方法,当做这里的runner

Name string
scheduler gocron.Scheduler
jobsMap jobsMapType
jobDisabledMap jobDisabledMapType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个map是不是value一定全是true,改成map[uuid]any,value存struct{}{}会不会好一些,不容易让人误以为存的是所有jobs: true/false

Comment on lines +153 to +154
lastRun, lastRunErr := job.LastRun()
nextRun10, nextRunErr := job.NextRuns(10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

立即获取LastRunNextRuns应该不对吧,这两个随着任务的进行会发生变化,应该在OpJob里储存job,然后LastRun函数直接转发o.job.LastRun

Comment on lines +30 to +33
type safeMap[K comparable, V any] struct {
lock sync.RWMutex
data map[K]V
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种safeMap我记得库里有应该不用自己实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Module: Task Task, scheduling and other goroutine-based features related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants