Documentation
¶
Overview ¶
Package cron is a thin alias over Hanzo Tasks (tools/tasks) kept for backward compatibility. New code should call app.Tasks() directly.
Scheduled jobs registered through this package are delegated to a *tasks.Client — durable when TASKS_URL is set, local goroutine tickers otherwise. The Schedule / NewSchedule helpers remain for cron-expression validation (used by settings validators).
Index ¶
- type Cron
- func (c *Cron) Add(jobId string, cronExpr string, fn func()) error
- func (c *Cron) HasJob(jobId string) bool
- func (c *Cron) HasStarted() bool
- func (c *Cron) Jobs() []*Job
- func (c *Cron) MustAdd(jobId string, cronExpr string, fn func())
- func (c *Cron) Remove(jobId string)
- func (c *Cron) RemoveAll()
- func (c *Cron) SetInterval(time.Duration)
- func (c *Cron) SetTimezone(l *time.Location)
- func (c *Cron) Start()
- func (c *Cron) Stop()
- func (c *Cron) Total() int
- type Job
- type Moment
- type Schedule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cron ¶
type Cron struct {
// contains filtered or unexported fields
}
Cron is a crontab-like scheduler. Since the v2 refactor it is a shim that delegates to a *tasks.Client.
func New ¶
func New() *Cron
New creates a Cron backed by a new local-only tasks.Client. Use NewFromTasks to share an existing client with the rest of the app.
func NewFromTasks ¶ added in v0.46.0
NewFromTasks wraps an existing tasks.Client.
func (*Cron) Add ¶
Add registers a recurring job. cronExpr is any expression accepted by tasks.Client.Add (standard 5-field cron, macros like @daily, or a Go duration string like "30s"). Re-adding with the same id replaces the previous registration.
func (*Cron) HasJob ¶ added in v0.46.0
HasJob reports whether a job with the given id is registered.
func (*Cron) HasStarted ¶
HasStarted reports whether at least one job is present and actively ticking.
func (*Cron) SetInterval ¶
SetInterval is retained for API compatibility. No-op: the tick cadence is dictated by each schedule's expression, not a global interval.
func (*Cron) SetTimezone ¶
SetTimezone updates the timezone used for cron-expression evaluation. Kept for API compatibility; the underlying tasks.Client does not currently take a per-client timezone — server-side schedules run in UTC.
func (*Cron) Start ¶
func (c *Cron) Start()
Start resumes all paused tickers. Schedules are auto-started at Add() time, so this is only meaningful after a prior Stop().
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job is a read-only view of a registered schedule, returned by Cron.Jobs().
func (*Job) Expression ¶
Expression returns the cron expression or duration string the job was registered with.
func (Job) MarshalJSON ¶
MarshalJSON implements json.Marshaler for the admin list endpoint.
type Moment ¶
type Moment struct {
Minute int `json:"minute"`
Hour int `json:"hour"`
Day int `json:"day"`
Month int `json:"month"`
DayOfWeek int `json:"dayOfWeek"`
}
Moment represents a parsed single time moment.
type Schedule ¶
type Schedule struct {
Minutes map[int]struct{} `json:"minutes"`
Hours map[int]struct{} `json:"hours"`
Days map[int]struct{} `json:"days"`
Months map[int]struct{} `json:"months"`
DaysOfWeek map[int]struct{} `json:"daysOfWeek"`
// contains filtered or unexported fields
}
Schedule stores parsed information for each time component when a cron job should run.
func NewSchedule ¶
NewSchedule creates a new Schedule from a cron expression.
A cron expression could be a macro OR 5 segments separated by space, representing: minute, hour, day of the month, month and day of the week.
The following segment formats are supported:
- wildcard: *
- range: 1-30
- step: */n or 1-30/n
- list: 1,2,3,10-20/n
The following macros are supported:
- @yearly (or @annually)
- @monthly
- @weekly
- @daily (or @midnight)
- @hourly