Documentation
¶
Index ¶
- Variables
- type Controller
- type JobFunc
- type JobInfo
- type Option
- type Scheduler
- func (s *Scheduler) Get(name string) (JobInfo, error)
- func (s *Scheduler) List() []JobInfo
- func (s *Scheduler) Pause(name string) error
- func (s *Scheduler) Register(name string, interval time.Duration, fn JobFunc, opts ...Option)
- func (s *Scheduler) Reschedule(name string, interval time.Duration) error
- func (s *Scheduler) Resume(name string) error
- func (s *Scheduler) RunNow(name string) error
- func (s *Scheduler) Start(ctx context.Context)
- type SchedulerOption
- type StateHook
Constants ¶
This section is empty.
Variables ¶
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 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) Pause ¶
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 ¶
Register adds a job. Must be called before Start. Re-registering the same name overwrites the previous entry.
func (*Scheduler) Reschedule ¶
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 ¶
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 ¶
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.
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.