sched

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: MIT Imports: 10 Imported by: 2

README

go-sched

A simple job (both run-once and recurring) queueing library with down-to millisecond precision.

Precision estimates based on test output (running on i7-11800h): 1ms precision with 80% tolerance.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	// contains filtered or unexported fields
}

Job encapsulates logic for a scheduled job to be run according to a set Timing, executing the job with a set panic handler, and holding onto a next execution time safely in a concurrent environment.

func NewJob

func NewJob(fn func(now time.Time)) *Job

NewJob returns a new Job to run given function.

func (*Job) At

func (job *Job) At(at time.Time) *Job

At sets this Job to execute at time, by passing (*sched.Once)(&at) to .With(). See .With() for details.

func (*Job) Every

func (job *Job) Every(period time.Duration) *Job

Every sets this Job to execute every period, by passing sched.Period(period) to .With(). See .With() for details.

func (*Job) EveryAt

func (job *Job) EveryAt(at time.Time, period time.Duration) *Job

EveryAt sets this Job to execute every period starting at time, by passing &PeriodicAt{once: Once(at), period: Periodic(period)} to .With(). See .With() for details.

func (*Job) Next

func (job *Job) Next() time.Time

Next returns the next time this Job is expected to run.

func (*Job) OnPanic added in v1.1.1

func (job *Job) OnPanic(fn func(interface{})) *Job

OnPanic specifies how this job handles panics, default is an actual panic.

func (*Job) Run

func (job *Job) Run(now time.Time)

Run will execute this Job and pass through given now time.

func (*Job) String added in v1.0.3

func (job *Job) String() string

String provides a debuggable string representation of Job including ID, next time and Timing type.

func (*Job) With

func (job *Job) With(t Timing) *Job

With sets this Job's timing to given implementation, or if already set will wrap existing using sched.TimingWrap{}.

type Once

type Once time.Time

Once implements Timing to provide a run-once Job execution.

func (*Once) Next

func (o *Once) Next(time.Time) time.Time

type Periodic

type Periodic time.Duration

Periodic implements Timing to provide a recurring Job execution.

func (Periodic) Next

func (p Periodic) Next(now time.Time) time.Time

type PeriodicAt

type PeriodicAt struct {
	Once   Once
	Period Periodic
}

PeriodicAt implements Timing to provide a recurring Job execution starting at 'Once' time.

func (*PeriodicAt) Next

func (p *PeriodicAt) Next(now time.Time) time.Time

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler provides a means of running jobs at specific times and regular intervals, all while sharing a single underlying timer.

func (*Scheduler) Done added in v1.2.1

func (sch *Scheduler) Done() <-chan struct{}

Done returns a channel that's closed when Scheduler.Stop() is called.

func (*Scheduler) Running

func (sch *Scheduler) Running() bool

Running will return whether Scheduler is running (i.e. NOT stopped / stopping).

func (*Scheduler) Schedule

func (sch *Scheduler) Schedule(job *Job) (cancel func())

Schedule will add provided Job to the Scheduler, returning a cancel function.

func (*Scheduler) Start

func (sch *Scheduler) Start(gorun func(func())) bool

Start will attempt to start the Scheduler. Immediately returns false if the Service is already running, and true after completed run.

func (*Scheduler) Stop

func (sch *Scheduler) Stop() bool

Stop will attempt to stop the Scheduler. Immediately returns false if not running, and true only after Scheduler is fully stopped.

type Timing

type Timing interface {
	Next(time.Time) time.Time
}

Timing provides scheduling for a Job, determining the next time for given current time that execution is required. Please note that calls to .Next() may alter the results of the next call, and should only be called by the Scheduler.

type TimingWrap

type TimingWrap struct {
	Outer Timing
	Inner Timing
	// contains filtered or unexported fields
}

TimingWrap allows combining two different Timing implementations.

func (*TimingWrap) Next

func (t *TimingWrap) Next(now time.Time) time.Time

Jump to

Keyboard shortcuts

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