cron

package
v0.48.1 Latest Latest
Warning

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

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

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

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

func NewFromTasks(c *tasks.Client) *Cron

NewFromTasks wraps an existing tasks.Client.

func (*Cron) Add

func (c *Cron) Add(jobId string, cronExpr string, fn func()) error

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

func (c *Cron) HasJob(jobId string) bool

HasJob reports whether a job with the given id is registered.

func (*Cron) HasStarted

func (c *Cron) HasStarted() bool

HasStarted reports whether at least one job is present and actively ticking.

func (*Cron) Jobs

func (c *Cron) Jobs() []*Job

Jobs returns a snapshot of all registered jobs ordered by id.

func (*Cron) MustAdd

func (c *Cron) MustAdd(jobId string, cronExpr string, fn func())

MustAdd is Add that panics on error.

func (*Cron) Remove

func (c *Cron) Remove(jobId string)

Remove deletes a registered job. No-op if the id is unknown.

func (*Cron) RemoveAll

func (c *Cron) RemoveAll()

RemoveAll deletes every registered job.

func (*Cron) SetInterval

func (c *Cron) SetInterval(time.Duration)

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

func (c *Cron) SetTimezone(l *time.Location)

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().

func (*Cron) Stop

func (c *Cron) Stop()

Stop pauses every local ticker without removing jobs. Registered schedules remain discoverable via Jobs() / HasJob() and can be resumed with Start(). To tear down the scheduler fully, call RemoveAll() or tasks.Client.Stop() on the underlying client.

func (*Cron) Total

func (c *Cron) Total() int

Total returns the number of registered jobs.

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

func (j *Job) Expression() string

Expression returns the cron expression or duration string the job was registered with.

func (*Job) Id

func (j *Job) Id() string

Id returns the job id.

func (Job) MarshalJSON

func (j Job) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for the admin list endpoint.

func (*Job) Run

func (j *Job) Run()

Run invokes the job's registered callback immediately, outside the normal schedule. No-op if the callback runs server-side (durable tasks) and is therefore not available in-process.

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.

func NewMoment

func NewMoment(t time.Time) *Moment

NewMoment creates a new Moment from the specified time.

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

func NewSchedule(cronExpr string) (*Schedule, error)

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

func (*Schedule) IsDue

func (s *Schedule) IsDue(m *Moment) bool

IsDue checks whether the provided Moment satisfies the current Schedule.

Jump to

Keyboard shortcuts

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