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 ¶
- Variables
- type Manager
- func (m *Manager) Count() int
- func (m *Manager) Get(name string) (Snapshot, bool)
- func (m *Manager) List() []Snapshot
- func (m *Manager) Register(t Task) error
- func (m *Manager) Run(name string) (Snapshot, error)
- func (m *Manager) ScheduledCount() int
- func (m *Manager) Shutdown(ctx context.Context) error
- func (m *Manager) Start() error
- type Outcome
- type Schedule
- type ScheduleInfo
- type ScheduleKind
- type Snapshot
- type Task
Constants ¶
This section is empty.
Variables ¶
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 ¶
New returns a manager that interprets calendar schedules in location. A nil location uses the server's local timezone.
func (*Manager) Register ¶
Register validates and adds a task. Registration is only allowed before Start.
func (*Manager) Run ¶
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 ¶
ScheduledCount returns the number of tasks with a schedule other than manual.
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 ¶
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 ¶
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) Kind ¶
func (s Schedule) Kind() ScheduleKind
Kind returns the schedule kind. The zero value is manual-only.
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.