Documentation ¶
Index ¶
- Variables
- type ClockHealth
- type Details
- type Entry
- type Logger
- type Nexter
- type NexterFn
- type NexterTime
- type NexterTimeFn
- type OnChannelFull
- type Options
- type Schedule
- func (s *Schedule) Details() Details
- func (s *Schedule) Fn() func()
- func (s *Schedule) FnE() func() error
- func (s *Schedule) ID() ScheduleID
- func (s *Schedule) Object() interface{}
- func (s *Schedule) RescheduleAt(t utc.UTC, o ...interface{}) bool
- func (s *Schedule) RescheduleIn(d time.Duration, o ...interface{}) bool
- func (s *Schedule) S() *Schedule
- func (s *Schedule) Str() string
- func (s *Schedule) String() string
- func (s *Schedule) Time() utc.UTC
- type ScheduleID
- type ScheduleOpt
- type Scheduler
- func (s *Scheduler) Add(sc *Schedule) bool
- func (s *Scheduler) At(at utc.UTC, o interface{}) ScheduleID
- func (s *Scheduler) C() chan Entry
- func (s *Scheduler) Dump() []*Schedule
- func (s *Scheduler) DumpStop() ([]*Schedule, error)
- func (s *Scheduler) In(d time.Duration, o interface{}) ScheduleID
- func (s *Scheduler) Outlet() []*Schedule
- func (s *Scheduler) Remove(id ScheduleID) bool
- func (s *Scheduler) Run(schedules []*Schedule) error
- func (s *Scheduler) Running() bool
- func (s *Scheduler) Start() error
- func (s *Scheduler) Stop() error
- type Schedules
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Occur = occur{} // Occur has functions to give an initial date to a Schedule Recur = recur{} // Recur has functions to configure a recurrent Schedule Until = until{} // Until has functions to configure termination of a recurrent schedule )
Functions ¶
This section is empty.
Types ¶
type ClockHealth ¶
ClockHealth allows checking the system clock is in sync with the internal monotonic clock, for example in cases where the user puts the system to sleep.
type Details ¶
type Details struct { ScheduledAt utc.UTC `json:"scheduled_at"` // time the Schedule was scheduled DispatchedAt utc.UTC `json:"dispatched_at"` // when the schedule was sent to the notification channel DispatchedCount int `json:"dispatched_count,omitempty"` // how many times the schedule was dispatched RescheduledCount int `json:"rescheduled_count,omitempty"` // how many times the schedule was re-scheduled }
Details of Schedule are available for logging or troubleshooting
type Nexter ¶
type Nexter interface { // Next returns when next notification after now must occur or utc.Zero Next(now utc.UTC, s *Schedule) utc.UTC }
Nexter is the interface of objects returning the Next UTC date of a Schedule
type NexterFn ¶
type NexterFn func(now utc.UTC, s *Schedule) utc.UTC
NexterFn is a function implementing Nexter
type NexterTime ¶
type NexterTime interface { // Next returns when next notification after now must occur or the zero time Next(now time.Time, s *Schedule) time.Time }
NexterTime is the interface of objects returning the Next date of a Schedule
type NexterTimeFn ¶
NexterTimeFn is a function implementing NexterTime
type OnChannelFull ¶
type OnChannelFull struct {
MaxWait time.Duration // max duration to wait for dispatching a schedule
}
OnChannelFull options tell the scheduler what to do when the dispatching channel is full
type Options ¶
type Options struct { ChannelSize uint // size of the notification channel Logger Logger // internal logger OnChannelFull OnChannelFull // what to do when the channel is full ClockHealth ClockHealth // check the system clock is in sync with the monotonic clock }
Options are options for the Scheduler
type Schedule ¶
type Schedule struct {
// contains filtered or unexported fields
}
Schedule is a planned event.
func MustSchedule ¶
func MustSchedule(id string, o interface{}, opts ...ScheduleOpt) *Schedule
MustSchedule returns a new initialized Schedule or panics if an error occurs.
func NewSchedule ¶
func NewSchedule(id string, o interface{}, opts ...ScheduleOpt) (*Schedule, error)
NewSchedule returns a new initialized Schedule or an error if incompatible options are used. The provided options must set the next scheduled time (e.g. through an Occur or a Recur option)
func (*Schedule) Fn ¶
func (s *Schedule) Fn() func()
Fn returns the object attached to this Schedule as a func() or nil if not such a function.
func (*Schedule) FnE ¶
FnE returns the object attached to this Schedule as a func() error or nil if not such a function.
func (*Schedule) Object ¶
func (s *Schedule) Object() interface{}
Object returns the interface object attached to the Schedule
func (*Schedule) RescheduleAt ¶
RescheduleAt reschedules the schedule at the given date. The function returns false if the schedule was built via a Recur option or if the schedule was not dispatched by the scheduler. Returns true if the schedule was sent to the scheduler.
func (*Schedule) RescheduleIn ¶
RescheduleIn reschedules the schedule at a date in d after now. The date is computed immediately. The function returns false if the schedule was built via a Recur option or if the schedule was not dispatched by the scheduler. Returns true if the schedule was sent to the scheduler.
type ScheduleOpt ¶
ScheduleOpt is a configuration function of Schedule. The predefined variables Occur, Recur and Until provide such functions.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler represents a sequence of planned events which are notified through channel C().
func New ¶
func New() *Scheduler
New returns a new Scheduler initialized with default options.
Example ¶
sched := scheduler.New() _ = sched.Start() wg := sync.WaitGroup{} wg.Add(1) go func(sched *scheduler.Scheduler) { defer wg.Done() //for s := range sched.C() { // s.Fn()() //} for { select { case entry, ok := <-sched.C(): if !ok { return } s := entry.S() // do something with the schedule // potentially in a goroutine // go func(s scheduler.Schedule) { s.Fn()() }(s) s.Fn()() } } }(sched) in := time.Millisecond * 200 sched.At(utc.Now().Add(in), func() { fmt.Println("fired") }) time.Sleep(time.Millisecond * 205) _ = sched.Stop() wg.Wait()
Output: fired
func NewScheduler ¶
NewScheduler returns a new Scheduler initialized with the given options. Default options are used if opts is nil.
func (*Scheduler) Add ¶
Add adds the given schedule to the scheduler. Returns false if the scheduler is not running.
func (*Scheduler) At ¶
func (s *Scheduler) At(at utc.UTC, o interface{}) ScheduleID
At adds a schedule that will run at the given date. Returns false if the scheduler is not running.
func (*Scheduler) In ¶
func (s *Scheduler) In(d time.Duration, o interface{}) ScheduleID
In adds a schedule that will run after the given duration. Returns false if the scheduler is not running.
func (*Scheduler) Outlet ¶
Outlet returns the schedules that could not be dispatched because the channel was full
func (*Scheduler) Remove ¶
func (s *Scheduler) Remove(id ScheduleID) bool
Remove removes schedules(s) with the given ScheduleID Returns false if the scheduler is not running.