cron

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cron registers and runs scheduled jobs alongside the app's HTTP surface. Jobs run bare — no middleware chain applies — but the scheduler publishes request-lifecycle events to trace.Bus so the dashboard shows each run like any other traced call.

Jobs are identified by name. Re-registering a name replaces the prior job (its history is dropped). Schedules use robfig cron 5-field syntax; @every and @hourly style descriptors are also accepted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(ctx context.Context) error

HandlerFunc is the user-supplied work. Return an error to mark the run failed; it surfaces on the dashboard and the next scheduled tick still fires.

type Job

type Job struct {
	Name        string
	Schedule    string
	Description string
	Service     string
	Handler     HandlerFunc
}

Job is the user-facing registration value. Service is optional — when set, the dashboard can group crons under the service node.

type RunRecord

type RunRecord struct {
	Started    time.Time `json:"started"`
	DurationMs int64     `json:"durationMs"`
	Success    bool      `json:"success"`
	Error      string    `json:"error,omitempty"`
	Manual     bool      `json:"manual,omitempty"`
}

RunRecord is one execution's outcome.

type Scheduler

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

Scheduler owns the underlying cron and the set of registered jobs. One per App; safe for concurrent use.

func NewScheduler

func NewScheduler(bus *trace.Bus, historyCap int) *Scheduler

NewScheduler builds a Scheduler. historyCap is the per-job run buffer; pass 0 for the default of 20.

func (*Scheduler) Register

func (s *Scheduler) Register(j Job) error

Register adds or replaces a job. If the scheduler has already been Started, the job is scheduled immediately; otherwise it schedules on Start.

func (*Scheduler) SetPaused

func (s *Scheduler) SetPaused(name string, paused bool) bool

SetPaused toggles the pause bit on a job. Paused jobs still appear in the snapshot (with Paused: true) but the scheduler skips their tick fires. Manual Trigger bypasses the pause flag on purpose — operators often pause to then run ad-hoc.

func (*Scheduler) Snapshot

func (s *Scheduler) Snapshot(name string) (Snapshot, bool)

Snapshot returns the view of one named job, or false if unknown.

func (*Scheduler) Snapshots

func (s *Scheduler) Snapshots() []Snapshot

Snapshots returns the current view of every registered job, sorted by name.

func (*Scheduler) Start

func (s *Scheduler) Start()

Start begins dispatching ticks. Safe to call once; subsequent calls no-op.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop halts the dispatcher and waits for any in-flight run to finish.

func (*Scheduler) Trigger

func (s *Scheduler) Trigger(name string) bool

Trigger runs the named job immediately in a goroutine, marked as a manual run. Returns false if the name is unknown.

type Snapshot

type Snapshot struct {
	Name        string      `json:"name"`
	Schedule    string      `json:"schedule"`
	Description string      `json:"description,omitempty"`
	Service     string      `json:"service,omitempty"`
	Paused      bool        `json:"paused"`
	Running     bool        `json:"running"`
	NextRun     *time.Time  `json:"nextRun,omitempty"`
	LastRun     *RunRecord  `json:"lastRun,omitempty"`
	History     []RunRecord `json:"history,omitempty"`
}

Snapshot is the serializable view the dashboard reads.

Jump to

Keyboard shortcuts

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