scheduler

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrJobUnknown       = errors.New("scheduler: unknown job")
	ErrJobSystem        = errors.New("scheduler: job is read-only (system)")
	ErrJobAlreadyPaused = errors.New("scheduler: job already paused")
	ErrJobNotPaused     = errors.New("scheduler: job not paused")
	ErrJobBusy          = errors.New("scheduler: job currently running")
)

Error sentinels surfaced by Pause / Resume / Reschedule / RunNow.

Functions

This section is empty.

Types

type Controller

type Controller interface {
	List() []JobInfo
	Get(name string) (JobInfo, error)
	Pause(name string) error
	Resume(name string) error
	Reschedule(name string, interval time.Duration) error
	RunNow(name string) error
}

Controller is the consumer-facing surface used by REST/web handlers. *Scheduler implements it.

type JobFunc

type JobFunc func(ctx context.Context) error

type JobInfo

type JobInfo struct {
	Name     string
	Interval time.Duration
	System   bool
	Running  bool
	Paused   bool
}

JobInfo is a snapshot of a registered job.

type Option

type Option func(*registeredJob)

Option mutates a registered job at registration time.

func WithSystem

func WithSystem() Option

WithSystem marks the job as a read-only system job. UI/API reject Pause/Resume/Reschedule/RunNow on system jobs.

type Scheduler

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

Scheduler runs registered jobs on fixed intervals. Each job runs in its own goroutine. If a job is still running when its next tick fires, the tick is skipped.

func New

func New(opts ...SchedulerOption) *Scheduler

func (*Scheduler) Get

func (s *Scheduler) Get(name string) (JobInfo, error)

Get returns a snapshot of the named job. Returns ErrJobUnknown if absent.

func (*Scheduler) List

func (s *Scheduler) List() []JobInfo

List returns a snapshot of every registered job, sorted by name.

func (*Scheduler) Pause

func (s *Scheduler) Pause(name string) error

Pause stops scheduling new ticks for name. An in-flight run completes naturally. Safe to call before Start (the job stays dormant when Start runs). Returns ErrJobUnknown / ErrJobSystem / ErrJobAlreadyPaused.

func (*Scheduler) Register

func (s *Scheduler) Register(
	name string,
	interval time.Duration,
	fn JobFunc,
	opts ...Option,
)

Register adds a job. Must be called before Start. Re-registering the same name overwrites the previous entry.

func (*Scheduler) Reschedule

func (s *Scheduler) Reschedule(name string, interval time.Duration) error

Reschedule updates the job's interval and restarts its goroutine. If the job is paused or Start has not yet been called, only the interval is updated — the next active run picks it up.

func (*Scheduler) Resume

func (s *Scheduler) Resume(name string) error

Resume re-arms a paused job. If Start has already run, a fresh goroutine is launched. Otherwise the paused flag is cleared and Start will pick the job up. Returns ErrJobUnknown / ErrJobSystem / ErrJobNotPaused.

func (*Scheduler) RunNow

func (s *Scheduler) RunNow(name string) error

RunNow triggers a one-off execution. Returns ErrJobBusy if the job is already running, ErrJobSystem on system jobs, ErrJobUnknown otherwise. Allowed while the job is paused — manual override is the whole point.

The job runs on a context derived from the scheduler's root context with the caller's cancel signal detached, so a short HTTP request timeout doesn't kill a long-running job.

func (*Scheduler) Start

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

Start launches every registered job that is not paused. Each job runs immediately once, then repeats at its interval. Blocks until ctx is cancelled. Jobs marked paused (via Pause prior to Start) stay dormant until Resume is called.

type SchedulerOption

type SchedulerOption func(*Scheduler)

SchedulerOption configures the scheduler at construction time.

func WithStateHook

func WithStateHook(h StateHook) SchedulerOption

WithStateHook installs a StateHook for run lifecycle events. A nil hook is a no-op.

type StateHook

type StateHook interface {
	OnStart(ctx context.Context, name string, startedAt time.Time)
	OnEnd(
		ctx context.Context,
		name string,
		endedAt time.Time,
		status string,
		runErr error,
		duration time.Duration,
	)
}

StateHook receives lifecycle events for every job run. Implementations must be safe for concurrent calls and must not block long; errors are logged and otherwise ignored — a misbehaving hook never stops a run.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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