您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)dkron中Scheduler的作用是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
// 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方法創(chuàng)建默認(rèn)的Scheduler;Start方法遍歷jobs,挨個(gè)設(shè)置job.Agent,然后添加到Scheduler中,之后執(zhí)行Scheduler.Cron.Start();Stop方法執(zhí)行Scheduler.Cron.Stop();Restart方法執(zhí)行Stop、ClearCron、Start方法;ClearCron設(shè)置Cron為nil
// 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,然后執(zhí)行Cron.AddJob(schedule, job),最后存儲(chǔ)到EntryJobMap
// 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,然后執(zhí)行cronInspect.Delete(job.Name)
dkron的Scheduler定義了Cron、Started、EntryJobMap屬性;NewScheduler方法創(chuàng)建默認(rèn)的Scheduler;它提供了Start、Stop、Restart、ClearCron、AddJob、RemoveJob方法。
關(guān)于dkron中Scheduler的作用是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。