聊聊dbsync的Schedulable

聊聊dbsync的Schedulable

技术杂谈小彩虹2021-08-24 22:06:23240A+A-

本文主要研究一下dbsync的Schedulable

Schedulable

//Schedulable represent an abstraction that can be schedule
type Schedulable struct {
	URL string
	ID  string
	*contract.Sync
	Schedule *contract.Schedule
	Status   string
	status   uint32
}

//NewSchedulableFromURL create a new scheduleable from URL
func NewSchedulableFromURL(URL string) (*Schedulable, error) {
	result := &Schedulable{}
	resource := url.NewResource(URL)
	err := resource.Decode(result)
	return result, err
}

Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性;NewSchedulableFromURL方法根据URL来创建Schedulable

Clone

func (s *Schedulable) Clone() *Schedulable {
	return &Schedulable{
		URL:      s.URL,
		ID:       s.ID,
		Sync:     s.Sync.Clone(),
		Schedule: s.Schedule,
		Status:   s.Status,
	}
}

Clone方法会复制一份Schedulable

Done

//Done return true if schedulable is not running
func (s *Schedulable) Done() {
	atomic.StoreUint32(&s.status, statusScheduled)
}

Done方法更新status为statusScheduled

IsRunning

//IsRunning return true if schedulable is running
func (s *Schedulable) IsRunning() bool {
	return atomic.LoadUint32(&s.status) == statusRunning
}

IsRunning方法更新status为statusRunning

ScheduleNexRun

//ScheduleNexRun schedules next run
func (s *Schedulable) ScheduleNexRun(baseTime time.Time) error {
	return s.Schedule.Next(baseTime)
}

ScheduleNexRun方法执行Schedule.Next(baseTime)

Init

//Init initializes scheduleable
func (s *Schedulable) Init() error {
	if s.ID == "" {
		s.ID = uRLToID(s.URL)
	}
	now := time.Now()
	if s.Schedule == nil {
		return nil
	}

	if s.Schedule.Frequency != nil && s.Schedule.Frequency.Value == 0 {
		s.Schedule.Frequency.Value = 1
	}
	if s.Schedule.NextRun == nil {
		if s.Schedule.Frequency != nil {
			s.Schedule.NextRun = &now
		} else {
			return s.Schedule.Next(now)
		}
	}
	return nil

}

Init方法执行Schedulable的初始化

Validate

//Validate checks if Schedulable is valid
func (s *Schedulable) Validate() error {
	if s.Schedule == nil {
		return fmt.Errorf("schedule was emtpy")
	}
	if s.Schedule.Frequency == nil && s.Schedule.At == nil {
		return fmt.Errorf("schedule.Frequency and schedule.At were emtpy")
	}
	if s.ID == "" {
		return fmt.Errorf("ID were emtpy")
	}
	return nil
}

Validate方法校验Schedule、Schedule.Frequency、Schedule.At、ID值

小结

dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Done、IsRunning、ScheduleNexRun、Init、Validate方法。

doc

点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1
本网站由 提供CDN/云存储服务

联系我们