github.com/songzhibin97/gkit@v1.2.13/distributed/schedule/constantdelay.go (about)

     1  package schedule
     2  
     3  import "time"
     4  
     5  // ConstantDelaySchedule represents a simple recurring duty cycle, e.g. "Every 5 minutes".
     6  // It does not support jobs more frequent than once a second.
     7  type ConstantDelaySchedule struct {
     8  	Delay time.Duration
     9  }
    10  
    11  // Every returns a crontab Schedule that activates once every duration.
    12  // Delays of less than a second are not supported (will round up to 1 second).
    13  // Any fields less than a Second are truncated.
    14  func Every(duration time.Duration) ConstantDelaySchedule {
    15  	if duration < time.Second {
    16  		duration = time.Second
    17  	}
    18  	return ConstantDelaySchedule{
    19  		Delay: duration - time.Duration(duration.Nanoseconds())%time.Second,
    20  	}
    21  }
    22  
    23  // Next returns the next time this should be run.
    24  // This rounds so that the next activation time will be on the second.
    25  func (schedule ConstantDelaySchedule) Next(t time.Time) time.Time {
    26  	return t.Add(schedule.Delay - time.Duration(t.Nanosecond())*time.Nanosecond)
    27  }