tasks

package
v0.0.0-...-ae5926d Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tasks owns the application task registry and scheduler. Manual-only, fixed-interval, and calendar tasks share one manager so that every invocation goes through the same overlap guard and run-state tracking.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when a task name is not registered.
	ErrNotFound = errors.New("task not found")
	// ErrAlreadyRunning is returned when a task is already running.
	ErrAlreadyRunning = errors.New("task already running")
	// ErrStopped is returned when the scheduler is shutting down or stopped.
	ErrStopped = errors.New("task scheduler is stopped")
	// ErrDuplicateName is returned when a task name is registered twice.
	ErrDuplicateName = errors.New("duplicate task name")
	// ErrRegistrationClosed is returned when registration happens after Start.
	ErrRegistrationClosed = errors.New("task registration is closed")
)

Functions

This section is empty.

Types

type Manager

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

Manager registers tasks and runs them either on their schedule or on explicit request. A task never runs concurrently with itself, regardless of how it was started.

func New

func New(location *time.Location) *Manager

New returns a manager that interprets calendar schedules in location. A nil location uses the server's local timezone.

func (*Manager) Count

func (m *Manager) Count() int

Count returns the number of registered tasks.

func (*Manager) Get

func (m *Manager) Get(name string) (Snapshot, bool)

Get returns a snapshot of one task.

func (*Manager) List

func (m *Manager) List() []Snapshot

List returns snapshots in registration order.

func (*Manager) Register

func (m *Manager) Register(t Task) error

Register validates and adds a task. Registration is only allowed before Start.

func (*Manager) Run

func (m *Manager) Run(name string) (Snapshot, error)

Run starts a task asynchronously using the application lifecycle context. It returns ErrAlreadyRunning if the task is running and ErrStopped after shutdown has begun. The caller's context is deliberately not used.

func (*Manager) ScheduledCount

func (m *Manager) ScheduledCount() int

ScheduledCount returns the number of tasks with a schedule other than manual.

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context) error

Shutdown rejects new runs, stops scheduled firing, cancels running task contexts, and waits for in-flight runs until ctx expires.

func (*Manager) Start

func (m *Manager) Start() error

Start starts the calendar and interval scheduler. Manual-only tasks stay available for explicit runs. It is safe to call Start once.

type Outcome

type Outcome string

Outcome is the result of the most recent run.

const (
	OutcomeSuccess Outcome = "success"
	OutcomeError   Outcome = "error"
	OutcomePanic   Outcome = "panic"
)

type Schedule

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

Schedule describes when a task runs. Construct schedules with Manual, Every, or Cron; the zero value is manual-only.

func Cron

func Cron(spec string) Schedule

Cron returns a calendar schedule from a standard five-field crontab expression or a named descriptor such as "@daily". The expression is interpreted in the manager timezone. The "@every" interval descriptor and per-expression timezone prefixes are rejected so the effective zone stays explicit.

func Every

func Every(d time.Duration) Schedule

Every returns a fixed-interval schedule. The interval is fixed between scheduled fire times, not measured from task completion; a tick that arrives while the previous run is still active is skipped. Intervals shorter than one second and intervals with sub-second precision are rejected at registration time.

func Manual

func Manual() Schedule

Manual returns a schedule for a task that only runs on explicit request.

func (Schedule) CronSpec

func (s Schedule) CronSpec() string

CronSpec returns the crontab expression for a calendar schedule.

func (Schedule) Interval

func (s Schedule) Interval() time.Duration

Interval returns the fixed interval for an interval schedule.

func (Schedule) Kind

func (s Schedule) Kind() ScheduleKind

Kind returns the schedule kind. The zero value is manual-only.

func (Schedule) Label

func (s Schedule) Label() string

Label returns a short human-readable schedule description. Calendar labels omit the timezone; callers show ScheduleInfo.Timezone separately.

type ScheduleInfo

type ScheduleInfo struct {
	Kind     ScheduleKind `json:"kind"`
	Label    string       `json:"label"`
	Interval string       `json:"interval,omitempty"`
	Cron     string       `json:"cron,omitempty"`
	Timezone string       `json:"timezone,omitempty"`
}

ScheduleInfo is the API-facing description of a task schedule.

type ScheduleKind

type ScheduleKind string

ScheduleKind is the typed form of a task schedule.

const (
	// ScheduleManual marks a task that only runs when explicitly started.
	ScheduleManual ScheduleKind = "manual"
	// ScheduleInterval marks a task that runs every fixed duration.
	ScheduleInterval ScheduleKind = "interval"
	// ScheduleCalendar marks a task that runs on a crontab expression in the
	// application timezone.
	ScheduleCalendar ScheduleKind = "calendar"
)

type Snapshot

type Snapshot struct {
	Name            string       `json:"name"`
	Description     string       `json:"description"`
	Schedule        ScheduleInfo `json:"schedule"`
	Running         bool         `json:"running"`
	LastStartedAt   *time.Time   `json:"last_started_at,omitempty"`
	LastCompletedAt *time.Time   `json:"last_completed_at,omitempty"`
	LastDurationMS  *int64       `json:"last_duration_ms,omitempty"`
	LastOutcome     Outcome      `json:"last_outcome,omitempty"`
	LastError       string       `json:"last_error,omitempty"`
	NextRun         *time.Time   `json:"next_run,omitempty"`
	// PreviousScheduledAt is the scheduled occurrence immediately before
	// NextRun. It is used with NextRun to compute next-run progress and is not
	// the last actual execution; a manual run does not change it.
	PreviousScheduledAt *time.Time `json:"previous_scheduled_at,omitempty"`
	RunCount            uint64     `json:"run_count"`
	SkippedCount        uint64     `json:"skipped_count"`
}

Snapshot is a concurrency-safe view of one task and its run state.

type Task

type Task struct {
	Name        string
	Description string
	Schedule    Schedule
	Run         func(ctx context.Context) error
}

Task is one registered unit of work. Name must be a lowercase kebab-case identifier. Run is invoked asynchronously and must honor its context.

Jump to

Keyboard shortcuts

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