scheduler

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package scheduler handles time-based job scheduling. Supports cron expressions, intervals, and time window constraints.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCron     = errors.New("invalid cron expression")
	ErrInvalidInterval = errors.New("invalid interval duration")
	ErrInvalidWindow   = errors.New("invalid time window")
	ErrInvalidTimezone = errors.New("invalid timezone")
	ErrNoSchedule      = errors.New("no schedule configured (need cron or interval)")
	ErrAlreadyRunning  = errors.New("scheduler already running")
	ErrNotRunning      = errors.New("scheduler not running")
)

Errors for scheduler operations.

Functions

This section is empty.

Types

type Job

type Job func(ctx context.Context) error

Job is a function to execute on schedule.

type Scheduler

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

Scheduler manages scheduled nightshift runs.

func New

func New() *Scheduler

New creates an empty scheduler.

func NewFromConfig

func NewFromConfig(cfg *config.ScheduleConfig) (*Scheduler, error)

NewFromConfig creates a scheduler from configuration.

func (*Scheduler) AddJob

func (s *Scheduler) AddJob(job Job)

AddJob adds a job to be executed on schedule.

func (*Scheduler) IsInWindow

func (s *Scheduler) IsInWindow(t time.Time) bool

IsInWindow checks if the given time is within the allowed execution window. Returns true if no window is configured.

func (*Scheduler) IsRunning

func (s *Scheduler) IsRunning() bool

IsRunning returns whether the scheduler is currently running.

func (*Scheduler) NextRun

func (s *Scheduler) NextRun() time.Time

NextRun returns the next scheduled run time.

func (*Scheduler) NextRuns

func (s *Scheduler) NextRuns(n int) ([]time.Time, error)

NextRuns returns the next N scheduled run times without starting the scheduler.

func (*Scheduler) Schedule

func (s *Scheduler) Schedule(at time.Time, job func())

Schedule adds a one-time job to run at the specified time.

func (*Scheduler) ScheduleCron

func (s *Scheduler) ScheduleCron(expr string, job func()) error

ScheduleCron adds a recurring job using cron expression. Deprecated: Use SetCron and AddJob instead.

func (*Scheduler) ScheduleInterval

func (s *Scheduler) ScheduleInterval(d time.Duration, job func()) error

ScheduleInterval adds a recurring job using interval.

func (*Scheduler) SetCron

func (s *Scheduler) SetCron(expr string) error

SetCron sets the cron expression for scheduling.

func (*Scheduler) SetInterval

func (s *Scheduler) SetInterval(d time.Duration) error

SetInterval sets the interval for scheduling.

func (*Scheduler) SetWindow

func (s *Scheduler) SetWindow(cfg *config.WindowConfig) error

SetWindow sets the time window constraint.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context) error

Start begins the scheduler loop.

func (*Scheduler) Stop

func (s *Scheduler) Stop() error

Stop stops the scheduler.

type TimeOfDay

type TimeOfDay struct {
	Hour   int
	Minute int
}

TimeOfDay represents a time within a day (hour and minute).

func ParseTimeOfDay

func ParseTimeOfDay(s string) (TimeOfDay, error)

ParseTimeOfDay parses a time string like "22:00" or "06:30".

func (TimeOfDay) Minutes

func (t TimeOfDay) Minutes() int

Minutes returns the time as minutes since midnight.

func (TimeOfDay) String

func (t TimeOfDay) String() string

String returns the time in HH:MM format.

type Window

type Window struct {
	Start    TimeOfDay // Start time (e.g., 22:00)
	End      TimeOfDay // End time (e.g., 06:00)
	Location *time.Location
}

Window represents a time window constraint.

func (*Window) Contains

func (w *Window) Contains(t time.Time) bool

Contains checks if a time falls within the window.

Jump to

Keyboard shortcuts

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