专栏首页码匠的流水账聊聊dbsync的Schedulable

聊聊dbsync的Schedulable

本文主要研究一下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

  • dbsync

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2021-04-22

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊dbsync的Schedulable

    dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Do...

    codecraft
  • 聊聊dbsync的jobs

    dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Do...

    codecraft
  • 聊聊dbsync的Criterion

    dbsync的Criterion是一个fmt.Stringer类型,它内置了greaterOrEqual、greaterThan、lessOrEqual、bet...

    codecraft
  • 聊聊dbsync的jobs

    dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Do...

    codecraft
  • 聊聊dbsync的Criterion

    dbsync的Criterion是一个fmt.Stringer类型,它内置了greaterOrEqual、greaterThan、lessOrEqual、bet...

    codecraft
  • 聊聊flink的TimeCharacteristic

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/Time...

    codecraft
  • 聊聊flink的SourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的CsvReader

    flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/ExecutionEnvironment.jav...

    codecraft
  • 聊聊flink的BoltWrapper

    flink-storm_2.11-1.6.2-sources.jar!/org/apache/flink/storm/wrappers/BoltWrapper....

    codecraft
  • 聊聊flink的EventTime

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的ParallelIteratorInputFormat

    本文主要研究一下flink的ParallelIteratorInputFormat

    codecraft
  • 聊聊flink的RichParallelSourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的InputFormatSourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/envi...

    codecraft
  • 聊聊flink的MemoryStateBackend

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/StateBacken...

    codecraft
  • 聊聊flink的OperatorStateBackend

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/OperatorSta...

    codecraft
  • 聊聊flink的MemCheckpointStreamFactory

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/CheckpointS...

    codecraft
  • 聊聊storm的OpaquePartitionedTridentSpoutExecutor

    本文主要研究一下storm的OpaquePartitionedTridentSpoutExecutor

    codecraft
  • 聊聊storm的ICommitterTridentSpout

    storm-core-1.2.2-sources.jar!/org/apache/storm/trident/spout/ICommitterTridentSp...

    codecraft
  • 聊聊flink的FsStateBackend

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/StateBacken...

    codecraft

扫码关注云+社区

领取腾讯云代金券