scheduler

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultShutdownTimeout = 60 * time.Second
	DefaultJobTimeout      = time.Hour
)

Variables

This section is empty.

Functions

func ShouldRunLocalHour added in v1.6.0

func ShouldRunLocalHour(tz string, hour int) bool

func ShouldRunLocalTime added in v1.6.4

func ShouldRunLocalTime(tz string, hour, minute int) bool

ShouldRunLocalTime returns true if the local time in tzName matches hour:minute exactly.

Types

type Job

type Job func(ctx context.Context)

Job is a unit of work to be run by the Scheduler.

type Option added in v1.6.0

type Option func(*Scheduler)

Option configures the Scheduler.

func WithJobTimeout added in v1.6.0

func WithJobTimeout(d time.Duration) Option

WithJobTimeout sets the default per-job timeout if ctx has no deadline.

func WithLogger added in v1.6.0

func WithLogger(l logger.Logger) Option

WithLogger sets the logger. Nil => Noop logger.

func WithShutdownTimeout added in v1.6.0

func WithShutdownTimeout(d time.Duration) Option

WithShutdownTimeout sets how long Start() waits for jobs to drain after ctx cancel.

type Scheduler

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

Scheduler runs Jobs on a fixed tick schedule. It also supports one-shot jobs and prevents concurrent runs of the same logical task (by ID). For each distinct id, only one instance may run at the same time.

func New

func New(tick time.Duration, opts ...Option) (*Scheduler, error)

New creates a Scheduler that fires every tick duration.

func (*Scheduler) Active added in v1.6.2

func (s *Scheduler) Active() int64

Active returns the number of currently running jobs.

func (*Scheduler) Add

func (s *Scheduler) Add(id string, slot int, job Job) error

Add registers a recurring job at the given slot (must be a multiple of granularity). id must be unique for each logical task; concurrent duplicates will be skipped.

func (*Scheduler) AddMany

func (s *Scheduler) AddMany(id string, slots []int, job Job) error

AddMany registers the same job on multiple slots. IDs must be distinct per logical task.

func (*Scheduler) Idled added in v1.6.2

func (s *Scheduler) Idled() <-chan struct{}

Idled returns a channel that receives a value whenever the scheduler drains all current jobs. Consumers should check Active() after receiving since new jobs may already have started.

func (*Scheduler) RunOnce

func (s *Scheduler) RunOnce(ctx context.Context, id string, job Job) bool

RunOnce fires a one-shot job immediately. Returns true if the job was started, false if skipped because it's already running or invalid.

func (*Scheduler) Start

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

Start blocks until ctx is canceled. It aligns to the next tick boundary, then fires runJobs on each tick.

Jump to

Keyboard shortcuts

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