cron

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	ID             string   `json:"id"`
	Name           string   `json:"name"`
	Enabled        bool     `json:"enabled"`
	Schedule       Schedule `json:"schedule"`
	Payload        Payload  `json:"payload"`
	State          JobState `json:"state"`
	CreatedAtMS    int64    `json:"created_at_ms"`
	UpdatedAtMS    int64    `json:"updated_at_ms"`
	DeleteAfterRun bool     `json:"delete_after_run,omitempty"`
}

Job represents a scheduled task.

func NewJob

func NewJob(name string, schedule Schedule, payload Payload) *Job

NewJob creates a new job with a generated ID and timestamps.

func (*Job) ScheduleDescription

func (j *Job) ScheduleDescription() string

ScheduleDescription returns a human-readable schedule summary.

func (*Job) ShortID

func (j *Job) ShortID() string

ShortID returns a truncated ID for display.

type JobHandler

type JobHandler func(*Job) error

JobHandler is called when a job fires.

type JobState

type JobState struct {
	NextRunAtMS *int64 `json:"next_run_at_ms,omitempty"`
	LastRunAtMS *int64 `json:"last_run_at_ms,omitempty"`
	LastStatus  string `json:"last_status,omitempty"`
	LastError   string `json:"last_error,omitempty"`
}

JobState holds runtime state for a job.

type Payload

type Payload struct {
	Kind    string `json:"kind"`    // "agent_turn"
	Message string `json:"message"` // message sent to agent
	Channel string `json:"channel"` // target channel name
	ChatID  string `json:"chat_id"` // target chat ID
	Deliver bool   `json:"deliver"` // true=deliver directly, false=agent processes
}

Payload defines what a job does when triggered.

type Schedule

type Schedule struct {
	Kind    string `json:"kind"`               // "at" | "every" | "cron"
	AtMS    *int64 `json:"at_ms,omitempty"`    // one-shot timestamp (milliseconds)
	EveryMS *int64 `json:"every_ms,omitempty"` // interval (milliseconds)
	Expr    string `json:"expr,omitempty"`     // cron expression (5-field)
}

Schedule defines when a job should run.

type Service

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

Service manages scheduled jobs with a ticker-based polling loop.

func NewService

func NewService(storePath string, handler JobHandler) *Service

NewService creates a cron service backed by the given store path.

func (*Service) AddJob

func (s *Service) AddJob(name, message string, schedule Schedule, channel, chatID string, deliver bool) (*Job, error)

AddJob creates and persists a new job.

func (*Service) EnableJob

func (s *Service) EnableJob(id string, enabled bool) (*Job, error)

EnableJob sets a job's enabled state.

func (*Service) GetJob

func (s *Service) GetJob(id string) (*Job, bool)

GetJob retrieves a single job by ID.

func (*Service) ListJobs

func (s *Service) ListJobs(includeDisabled bool) []*Job

ListJobs returns all jobs, optionally including disabled ones.

func (*Service) RemoveJob

func (s *Service) RemoveJob(id string) error

RemoveJob deletes a job by ID.

func (*Service) RunJob added in v0.2.1

func (s *Service) RunJob(id string) (*Job, error)

RunJob executes a single job immediately by ID, regardless of next-run timing. It reuses the same execution semantics as scheduled runs.

func (*Service) Start

func (s *Service) Start() error

Start loads jobs from disk and begins the polling loop.

func (*Service) Status

func (s *Service) Status() map[string]any

Status returns a summary of the cron service.

func (*Service) Stop

func (s *Service) Stop()

Stop gracefully shuts down the polling loop.

type Store

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

Store persists cron jobs as a JSON file.

func NewStore

func NewStore(path string) *Store

NewStore creates a store backed by the given file path.

func (*Store) All

func (s *Store) All() []*Job

All returns deep copies of all jobs.

func (*Store) Delete

func (s *Store) Delete(id string) bool

Delete removes a job by ID.

func (*Store) Get

func (s *Store) Get(id string) (*Job, bool)

Get returns a deep copy of the job with the given ID.

func (*Store) Load

func (s *Store) Load() error

Load reads jobs from disk. If the file does not exist, the store starts empty.

func (*Store) Put

func (s *Store) Put(job *Job)

Put stores a deep copy of the job. The caller may continue to mutate the original without racing with Save or other readers.

func (*Store) Save

func (s *Store) Save() error

Save writes all jobs to disk. Serialization happens under the read lock so that concurrent mutations via Put cannot race with encoding.

Jump to

Keyboard shortcuts

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