trigger

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package trigger provides trigger implementations for job scheduling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyJitter

func ApplyJitter(nextFireTime *time.Time, jitter time.Duration, now time.Time) *time.Time

ApplyJitter adds a random jitter to the next fire time. If nextFireTime is nil or jitter is 0, returns nextFireTime unchanged.

func DurationPtr

func DurationPtr(d time.Duration) *time.Duration

DurationPtr is a helper to create a pointer to a time.Duration value.

func TimePtr

func TimePtr(t time.Time) *time.Time

TimePtr is a helper to create a pointer to a time.Time value.

Types

type AndTrigger

type AndTrigger struct {
	Triggers []Trigger
	Jitter   time.Duration
}

AndTrigger returns the earliest fire time that all triggers agree on. It's finished when any trigger finishes.

func NewAndTrigger

func NewAndTrigger(triggers []Trigger, jitter time.Duration) *AndTrigger

NewAndTrigger creates a new AndTrigger.

func (*AndTrigger) GetNextFireTime

func (t *AndTrigger) GetNextFireTime(previousFireTime *time.Time, now time.Time) *time.Time

GetNextFireTime finds the next time all triggers agree on.

func (*AndTrigger) GobDecode

func (t *AndTrigger) GobDecode(data []byte) error

GobDecode implements gob.GobDecoder for deserialization.

func (*AndTrigger) GobEncode

func (t *AndTrigger) GobEncode() ([]byte, error)

GobEncode implements gob.GobEncoder for serialization.

func (*AndTrigger) String

func (t *AndTrigger) String() string

String returns a string representation.

type CalendarIntervalTrigger

type CalendarIntervalTrigger struct {
	Years     int
	Months    int
	Weeks     int
	Days      int
	Hour      int
	Minute    int
	Second    int
	StartDate time.Time // Only the date portion is used
	EndDate   *time.Time
	Location  *time.Location
	Jitter    time.Duration
}

CalendarIntervalTrigger fires at calendar-based intervals (years, months, weeks, days) always at the same time of day.

func NewCalendarIntervalTrigger

func NewCalendarIntervalTrigger(opts ...CalendarOption) (*CalendarIntervalTrigger, error)

NewCalendarIntervalTrigger creates a new CalendarIntervalTrigger.

func (*CalendarIntervalTrigger) GetNextFireTime

func (t *CalendarIntervalTrigger) GetNextFireTime(previousFireTime *time.Time, now time.Time) *time.Time

GetNextFireTime calculates the next fire time.

func (*CalendarIntervalTrigger) GobDecode

func (t *CalendarIntervalTrigger) GobDecode(data []byte) error

GobDecode implements gob.GobDecoder for deserialization.

func (*CalendarIntervalTrigger) GobEncode

func (t *CalendarIntervalTrigger) GobEncode() ([]byte, error)

GobEncode implements gob.GobEncoder for serialization.

func (*CalendarIntervalTrigger) String

func (t *CalendarIntervalTrigger) String() string

String returns a string representation of the trigger.

type CalendarOption

type CalendarOption func(*CalendarIntervalTrigger)

CalendarOption configures a CalendarIntervalTrigger.

func WithCalendarDays

func WithCalendarDays(days int) CalendarOption

WithCalendarDays sets the days interval.

func WithCalendarEndDate

func WithCalendarEndDate(d time.Time) CalendarOption

WithCalendarEndDate sets the end date.

func WithCalendarJitter

func WithCalendarJitter(d time.Duration) CalendarOption

WithCalendarJitter sets the jitter.

func WithCalendarLocation

func WithCalendarLocation(loc *time.Location) CalendarOption

WithCalendarLocation sets the timezone.

func WithCalendarMonths

func WithCalendarMonths(months int) CalendarOption

WithCalendarMonths sets the months interval.

func WithCalendarStartDate

func WithCalendarStartDate(d time.Time) CalendarOption

WithCalendarStartDate sets the start date.

func WithCalendarTime

func WithCalendarTime(hour, minute, second int) CalendarOption

WithCalendarTime sets the time of day to fire.

func WithCalendarWeeks

func WithCalendarWeeks(weeks int) CalendarOption

WithCalendarWeeks sets the weeks interval.

func WithCalendarYears

func WithCalendarYears(years int) CalendarOption

WithCalendarYears sets the years interval.

type DateOption

type DateOption func(*DateTrigger)

DateOption configures a DateTrigger.

func WithDateLocation

func WithDateLocation(loc *time.Location) DateOption

WithDateLocation sets the timezone for the trigger.

type DateTrigger

type DateTrigger struct {
	RunDate  time.Time
	Location *time.Location
}

DateTrigger triggers once at a specific datetime. If RunDate is not set, it defaults to the current time.

func NewDateTrigger

func NewDateTrigger(runDate time.Time, opts ...DateOption) *DateTrigger

NewDateTrigger creates a new DateTrigger. If runDate is zero, the current time is used.

func (*DateTrigger) GetNextFireTime

func (t *DateTrigger) GetNextFireTime(previousFireTime *time.Time, now time.Time) *time.Time

GetNextFireTime returns the run date if this is the first call, or nil for subsequent calls (one-shot trigger).

func (*DateTrigger) GobDecode

func (t *DateTrigger) GobDecode(data []byte) error

GobDecode implements gob.GobDecoder for deserialization.

func (*DateTrigger) GobEncode

func (t *DateTrigger) GobEncode() ([]byte, error)

GobEncode implements gob.GobEncoder for serialization.

func (*DateTrigger) String

func (t *DateTrigger) String() string

String returns a string representation of the trigger.

type IntervalOption

type IntervalOption func(*IntervalTrigger)

IntervalOption configures an IntervalTrigger.

func WithIntervalEndDate

func WithIntervalEndDate(t time.Time) IntervalOption

WithIntervalEndDate sets the end date after which no more triggers occur.

func WithIntervalJitter

func WithIntervalJitter(d time.Duration) IntervalOption

WithIntervalJitter sets the maximum jitter to add to each fire time.

func WithIntervalLocation

func WithIntervalLocation(loc *time.Location) IntervalOption

WithIntervalLocation sets the timezone for the trigger.

func WithIntervalStartDate

func WithIntervalStartDate(t time.Time) IntervalOption

WithIntervalStartDate sets the start date for the interval calculation.

type IntervalTrigger

type IntervalTrigger struct {
	Interval  time.Duration
	StartDate time.Time
	EndDate   *time.Time
	Location  *time.Location
	Jitter    time.Duration
}

IntervalTrigger triggers at fixed intervals.

func NewIntervalTrigger

func NewIntervalTrigger(interval time.Duration, opts ...IntervalOption) *IntervalTrigger

NewIntervalTrigger creates a new IntervalTrigger with the given interval. If the interval is zero or negative, it defaults to 1 second.

func NewIntervalTriggerFromParts

func NewIntervalTriggerFromParts(weeks, days, hours, minutes, seconds int, opts ...IntervalOption) *IntervalTrigger

NewIntervalTriggerFromParts creates an IntervalTrigger from individual time components.

func (*IntervalTrigger) GetJitter

func (t *IntervalTrigger) GetJitter() time.Duration

GetJitter returns the jitter duration.

func (*IntervalTrigger) GetNextFireTime

func (t *IntervalTrigger) GetNextFireTime(previousFireTime *time.Time, now time.Time) *time.Time

GetNextFireTime calculates the next fire time based on the interval.

func (*IntervalTrigger) GobDecode

func (t *IntervalTrigger) GobDecode(data []byte) error

GobDecode implements gob.GobDecoder for deserialization.

func (*IntervalTrigger) GobEncode

func (t *IntervalTrigger) GobEncode() ([]byte, error)

GobEncode implements gob.GobEncoder for serialization.

func (*IntervalTrigger) SetJitter

func (t *IntervalTrigger) SetJitter(d time.Duration)

SetJitter sets the jitter duration.

func (*IntervalTrigger) String

func (t *IntervalTrigger) String() string

String returns a string representation of the trigger.

type Jitterable

type Jitterable interface {
	Trigger
	SetJitter(d time.Duration)
	GetJitter() time.Duration
}

Jitterable is an optional interface for triggers that support jitter.

type OrTrigger

type OrTrigger struct {
	Triggers []Trigger
	Jitter   time.Duration
}

OrTrigger returns the earliest fire time from any trigger. It's finished when all triggers finish.

func NewOrTrigger

func NewOrTrigger(triggers []Trigger, jitter time.Duration) *OrTrigger

NewOrTrigger creates a new OrTrigger.

func (*OrTrigger) GetNextFireTime

func (t *OrTrigger) GetNextFireTime(previousFireTime *time.Time, now time.Time) *time.Time

GetNextFireTime returns the earliest fire time from any trigger.

func (*OrTrigger) GobDecode

func (t *OrTrigger) GobDecode(data []byte) error

GobDecode implements gob.GobDecoder for deserialization.

func (*OrTrigger) GobEncode

func (t *OrTrigger) GobEncode() ([]byte, error)

GobEncode implements gob.GobEncoder for serialization.

func (*OrTrigger) String

func (t *OrTrigger) String() string

String returns a string representation.

type Trigger

type Trigger interface {
	// GetNextFireTime returns the next datetime to fire on.
	// Returns nil if no such datetime can be calculated (schedule finished).
	//
	// previousFireTime: the previous time the trigger was fired (nil for first calculation)
	// now: current datetime
	GetNextFireTime(previousFireTime *time.Time, now time.Time) *time.Time

	// String returns a human-readable representation of the trigger.
	String() string
}

Trigger defines the interface that all triggers must implement. A trigger determines when a job should be executed.

Directories

Path Synopsis
Package cron provides a cron-style trigger for scheduling jobs.
Package cron provides a cron-style trigger for scheduling jobs.

Jump to

Keyboard shortcuts

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