聊聊dkron的Scheduler
序
本文主要研究一下dkron的Scheduler
Scheduler
// Scheduler represents a dkron scheduler instance, it stores the cron engine
// and the related parameters.
type Scheduler struct {
Cron *cron.Cron
Started bool
EntryJobMap sync.Map
}
// NewScheduler creates a new Scheduler instance
func NewScheduler() *Scheduler {
schedulerStarted.Set(0)
return &Scheduler{
Cron: nil,
Started: false,
EntryJobMap: sync.Map{},
}
}
// Start the cron scheduler, adding its corresponding jobs and
// executing them on time.
func (s *Scheduler) Start(jobs []*Job, agent *Agent) error {
s.Cron = cron.New(cron.WithParser(extcron.NewParser()))
metrics.IncrCounter([]string{"scheduler", "start"}, 1)
for _, job := range jobs {
job.Agent = agent
s.AddJob(job)
}
s.Cron.Start()
s.Started = true
schedulerStarted.Set(1)
return nil
}
// Stop stops the scheduler effectively not running any job.
func (s *Scheduler) Stop() {
if s.Started {
log.Debug("scheduler: Stopping scheduler")
s.Cron.Stop()
s.Started = false
// Keep Cron exists and let the jobs which have been scheduled can continue to finish,
// even the node's leadership will be revoked.
// Ignore the running jobs and make s.Cron to nil may cause whole process crashed.
//s.Cron = nil
// expvars
cronInspect.Do(func(kv expvar.KeyValue) {
kv.Value = nil
})
}
schedulerStarted.Set(0)
}
// Restart the scheduler
func (s *Scheduler) Restart(jobs []*Job, agent *Agent) {
s.Stop()
s.ClearCron()
s.Start(jobs, agent)
}
// Clear cron separately, this can only be called when agent will be stop.
func (s *Scheduler) ClearCron() {
s.Cron = nil
}
Scheduler定义了Cron、Started、EntryJobMap属性;NewScheduler方法创建默认的Scheduler;Start方法遍历jobs,挨个设置job.Agent,然后添加到Scheduler中,之后执行Scheduler.Cron.Start();Stop方法执行Scheduler.Cron.Stop();Restart方法执行Stop、ClearCron、Start方法;ClearCron设置Cron为nil
AddJob
// AddJob Adds a job to the cron scheduler
func (s *Scheduler) AddJob(job *Job) error {
// Check if the job is already set and remove it if exists
if _, ok := s.EntryJobMap.Load(job.Name); ok {
s.RemoveJob(job)
}
if job.Disabled || job.ParentJob != "" {
return nil
}
log.WithFields(logrus.Fields{
"job": job.Name,
}).Debug("scheduler: Adding job to cron")
// If Timezone is set on the job, and not explicitly in its schedule,
// AND its not a descriptor (that don't support timezones), add the
// timezone to the schedule so robfig/cron knows about it.
schedule := job.Schedule
if job.Timezone != "" &&
!strings.HasPrefix(schedule, "@") &&
!strings.HasPrefix(schedule, "TZ=") &&
!strings.HasPrefix(schedule, "CRON_TZ=") {
schedule = "CRON_TZ=" + job.Timezone + " " + schedule
}
id, err := s.Cron.AddJob(schedule, job)
if err != nil {
return err
}
s.EntryJobMap.Store(job.Name, id)
cronInspect.Set(job.Name, job)
metrics.IncrCounterWithLabels([]string{"scheduler", "job_add"}, 1, []metrics.Label{{Name: "job", Value: job.Name}})
return nil
}
AddJob方法先移除EntryJobMap中的同名job,然后执行Cron.AddJob(schedule, job),最后存储到EntryJobMap
RemoveJob
// RemoveJob removes a job from the cron scheduler
func (s *Scheduler) RemoveJob(job *Job) {
log.WithFields(logrus.Fields{
"job": job.Name,
}).Debug("scheduler: Removing job from cron")
if v, ok := s.EntryJobMap.Load(job.Name); ok {
s.Cron.Remove(v.(cron.EntryID))
s.EntryJobMap.Delete(job.Name)
cronInspect.Delete(job.Name)
metrics.IncrCounterWithLabels([]string{"scheduler", "job_delete"}, 1, []metrics.Label{{Name: "job", Value: job.Name}})
}
}
RemoveJob方法先从EntryJobMap移除同名job,然后执行cronInspect.Delete(job.Name)
小结
dkron的Scheduler定义了Cron、Started、EntryJobMap属性;NewScheduler方法创建默认的Scheduler;它提供了Start、Stop、Restart、ClearCron、AddJob、RemoveJob方法。
doc
code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun
推荐阅读
Java20的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...
codecraft赞 1阅读 927
Golang 中 []byte 与 string 转换
string 类型和 []byte 类型是我们编程时最常使用到的数据结构。本文将探讨两者之间的转换方式,通过分析它们之间的内在联系来拨开迷雾。
机器铃砍菜刀赞 24阅读 58.5k评论 2
万字详解,吃透 MongoDB!
MongoDB 是一个基于 分布式文件存储 的开源 NoSQL 数据库系统,由 C++ 编写的。MongoDB 提供了 面向文档 的存储方式,操作起来比较简单和容易,支持“无模式”的数据建模,可以存储比较复杂的数据类型,是一款非常...
JavaGuide赞 8阅读 1.8k
数据结构与算法:二分查找
一、常见数据结构简单数据结构(必须理解和掌握)有序数据结构:栈、队列、链表。有序数据结构省空间(储存空间小)无序数据结构:集合、字典、散列表,无序数据结构省时间(读取时间快)复杂数据结构树、 堆图二...
白鲸鱼赞 9阅读 5.4k
PHP转Go实践:xjson解析神器「开源工具集」
我和劲仔都是PHP转Go,身边越来越多做PHP的朋友也逐渐在用Go进行重构,重构过程中,会发现php的json解析操作(系列化与反序列化)是真的香,弱类型语言的各种隐式类型转换,很大程度的减低了程序的复杂度。
王中阳Go赞 11阅读 2.8k评论 4
Git操作不规范,战友提刀来相见!
年终奖都没了,还要扣我绩效,门都没有,哈哈。这波骚Git操作我也是第一次用,担心闪了腰,所以不仅做了备份,也做了笔记,分享给大家。问题描述小A和我在同时开发一个功能模块,他在优化之前的代码逻辑,我在开...
王中阳Go赞 6阅读 2.9k评论 4
妙啊,空结构体还能这么用?Go语言的结构体看这篇就够了
本文详解了Go语言结构体的各个知识点,最后介绍了空结构体的3种妙用。希望对你有帮助。定义结构体,是一种自定义的数据类型,由多个数据类型组合而成。用于描述一类事物相关属性。定义方式: {代码...} 实例化结...
王中阳Go赞 6阅读 1.3k
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。