sched

package module
v0.0.0-...-9b9b10d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2022 License: BSD-2-Clause Imports: 3 Imported by: 0

README

sched

Package sched is used to find the next time event from multiple rules.

A Rule is a simple and flexible interface with one method:

Next(now time.Time) time.Time

Schedule is a priority queue of rules and their next occurance. Scheduler is thread safe service to manage a schedule and trigger on events.

s := sched.New(time.Now(), func(e sched.Event){
	fmt.Println("happy birthday!", e.Time)
})
s.Add(myBirthday)

License

sched is BSD licensed, Copyright (c) 2016 Martin Schnabel

Documentation

Overview

Package sched is used to find the next time event from multiple rules.

Example (SchedScheduler)
package main

import (
	"fmt"
	"github.com/mb0/sched"
	"sync/atomic"
	"time"
)

func main() {
	type action struct {
		sched.Interval
		Do func(time.Time)
	}
	var (
		now   = time.Now()
		done  = make(chan struct{})
		count int32
	)
	a := &action{
		sched.Interval{Start: now, Duration: 10 * time.Millisecond},
		func(t time.Time) {
			fmt.Println("action triggered after", t.Sub(now))
		},
	}
	trigger := func(ev sched.Event) {
		switch a := ev.Rule.(type) {
		case *action:
			a.Do(ev.Time)
		default:
			fmt.Println("unknown rule", a)
		}
		if atomic.AddInt32(&count, 1) >= 3 {
			close(done)
		}
	}
	s := sched.New(now, trigger)
	// equivalent to s.Sched.Add(a); s.Start()
	s.Add(a)
	<-done
	s.Stop()
}
Output:

action triggered after 10ms
action triggered after 20ms
action triggered after 30ms

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Next

func Next(start time.Time, interval time.Duration, now time.Time) time.Time

Next returns the next time after now that is intervals more than start. It can be used to implement common repeating rules with a fixed interval.

Types

type Event

type Event struct {
	Rule
	time.Time
}

Event is an occurance of Rule at a specific point in time.

type Interval

type Interval struct {
	Start time.Time
	time.Duration
}

Interval is simple Rule implementation with start time and interval duration.

func (Interval) Next

func (i Interval) Next(now time.Time) time.Time

Next returns the next point in time for this rule after now.

type Rule

type Rule interface {
	// Next returns the next point in time after now.
	// If the returned time is not after now this rule has ended.
	Next(now time.Time) time.Time
}

Rule represents a deterministic sequence of points in time. Implementations must be comparable.

type Schedule

type Schedule struct {
	Last Event
	// contains filtered or unexported fields
}

Schedule is a time based queue of events sourced from multiple rules.

func NewSchedule

func NewSchedule(now time.Time) *Schedule

NewSchedule returns an empty Schedule beginning at now.

func (*Schedule) Add

func (s *Schedule) Add(r Rule) bool

Add schedules a rule or returns false.

func (Schedule) Len

func (q Schedule) Len() int

func (Schedule) Less

func (q Schedule) Less(i, j int) bool

func (*Schedule) Next

func (s *Schedule) Next() Event

Next returns the next event. The event rule is nil if no event is scheduled. The returned event rule is rescheduled or removed from the queue.

func (*Schedule) Pop

func (q *Schedule) Pop() interface{}

func (*Schedule) Push

func (q *Schedule) Push(x interface{})

func (*Schedule) Remove

func (s *Schedule) Remove(r Rule) bool

Remove removes a rule from the schedule or returns false.

func (Schedule) Swap

func (q Schedule) Swap(i, j int)

type Scheduler

type Scheduler struct {
	sync.Mutex
	Sched *Schedule
	// contains filtered or unexported fields
}

Scheduler schedules rules and calls its handler with events.

func New

func New(now time.Time, handler func(Event)) *Scheduler

New returns a scheduler that calls handler with events.

func (*Scheduler) Add

func (s *Scheduler) Add(r Rule) bool

Add adds rule r to the schedule. Adding a rule will automatically start the scheduler.

func (*Scheduler) Remove

func (s *Scheduler) Remove(r Rule) bool

Remove removes rule r from the schedule.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the scheduler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL