cron

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxJobs         = 50
	RecurringMaxAge = 3 * 24 * time.Hour
)

Variables

This section is empty.

Functions

func BuildMissedNotification

func BuildMissedNotification(tasks []Job) string

BuildMissedNotification creates a prompt text asking the user about missed tasks.

func HumanSchedule

func HumanSchedule(schedule string) string

HumanSchedule returns a human-readable description of a schedule string.

func JitteredNextFire

func JitteredNextFire(expr *CronExpr, after time.Time, id string) (time.Time, bool)

JitteredNextFire computes the next fire time with jitter for a recurring cron job.

func NextMatch

func NextMatch(expr *CronExpr, after time.Time) (time.Time, bool)

NextMatch finds the next time after 'after' that matches the cron expression. Implements standard cron DOM/DOW union logic: when both are restricted, a day matches if EITHER condition is true (OR semantics).

func OneShotJitteredFireTime

func OneShotJitteredFireTime(expr *CronExpr, createdAt time.Time, id string) (time.Time, bool)

OneShotJitteredFireTime computes the fire time for a one-shot cron job. If the fire minute lands on :00 or :30, it shifts up to 90s early to avoid thundering herd.

func ProcessAlive added in v0.3.0

func ProcessAlive(pid int) bool

ProcessAlive reports whether a process with the given PID exists. Exported for reuse outside the scheduler (e.g. dream's consolidation lock).

Types

type CronExpr

type CronExpr struct {
	Minute     []int
	Hour       []int
	DayOfMonth []int
	Month      []int
	DayOfWeek  []int
}

CronExpr holds parsed 5-field cron expression values.

func ParseCron

func ParseCron(expr string) (*CronExpr, error)

ParseCron parses a 5-field cron expression.

func ParseSchedule

func ParseSchedule(s string) (cronExpr *CronExpr, interval time.Duration, err error)

ParseSchedule tries to parse s as a Go duration first, then as a 5-field cron.

type Job

type Job struct {
	ID        string `json:"id"`
	Schedule  string `json:"schedule"`
	Prompt    string `json:"prompt"`
	Recurring bool   `json:"recurring,omitempty"`
	CreatedAt int64  `json:"createdAt"` // Unix milliseconds
	Durable   bool   `json:"durable,omitempty"`
	// contains filtered or unexported fields
}

Job is a scheduled task.

func (*Job) CreatedTime

func (j *Job) CreatedTime() time.Time

CreatedTime returns CreatedAt as time.Time.

func (*Job) NextFire

func (j *Job) NextFire() time.Time

NextFire returns the next scheduled fire time.

type Scheduler

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

Scheduler ticks every second and fires ready jobs from both tracks.

func NewScheduler

func NewScheduler(cfg SchedulerConfig) *Scheduler

NewScheduler creates a scheduler (not yet started).

func (*Scheduler) Start

func (s *Scheduler) Start()

Start begins the scheduling loop in a goroutine.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop signals the scheduler to stop and waits for it to finish.

type SchedulerConfig

type SchedulerConfig struct {
	Store          *Store
	SessionID      string // for lock ownership
	RestoreDurable bool   // load durable jobs from disk (only for resumed sessions)
	OnFire         func(prompt string)
	IsBusy         func() bool
}

SchedulerConfig configures the scheduler.

type Store

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

Store manages both session and durable cron jobs.

func NewStore

func NewStore() *Store

NewStore creates an empty Store. Call SetConfigDir to enable durable storage.

func (*Store) AcquireLock

func (s *Store) AcquireLock(sessionID string) bool

AcquireLock tries to acquire the scheduler lock. Returns true if acquired.

func (*Store) ConfigDir

func (s *Store) ConfigDir() string

ConfigDir returns the configured directory for persistence.

func (*Store) Create

func (s *Store) Create(schedule, prompt string, recurring, durable bool) (*Job, error)

Create adds a new job. If durable=true it is also persisted to file.

func (*Store) Delete

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

Delete removes a job from either track.

func (*Store) DeleteAll

func (s *Store) DeleteAll() int

DeleteAll removes all jobs from both tracks and returns the count.

func (*Store) DeleteDurableByIDs

func (s *Store) DeleteDurableByIDs(ids []string)

DeleteDurableByIDs removes durable jobs by ID and persists. Used by scheduler for cleanup.

func (*Store) DurableFilePath

func (s *Store) DurableFilePath() string

DurableFilePath returns the tasks file path.

func (*Store) Get

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

Get returns a copy of the job or false.

func (*Store) Len

func (s *Store) Len() int

Len returns total count of all jobs.

func (*Store) List

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

List returns a snapshot of all jobs (session + durable) sorted by creation time.

func (*Store) LoadDurableJobs

func (s *Store) LoadDurableJobs() error

LoadDurableJobs reads scheduled_tasks.json from the session directory and loads valid jobs.

func (*Store) LockFilePath

func (s *Store) LockFilePath() string

LockFilePath returns the lock file path for the scheduler.

func (*Store) MissedDurableJobs

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

MissedDurableJobs returns durable one-shot jobs whose first fire time is in the past.

func (*Store) ReleaseLock

func (s *Store) ReleaseLock(sessionID string)

ReleaseLock releases the scheduler lock if we own it.

func (*Store) SetConfigDir

func (s *Store) SetConfigDir(dir string)

SetConfigDir enables durable storage (e.g. ~/.codebot/projects/<projectID>/<sessionID>).

func (*Store) StartWriter

func (s *Store) StartWriter()

StartWriter launches the background goroutine that writes durable jobs to disk.

func (*Store) StopWriter

func (s *Store) StopWriter()

StopWriter stops the writer goroutine and waits for the final flush.

func (*Store) TickDurableJobs

func (s *Store) TickDurableJobs(now time.Time) []string

TickDurableJobs fires ready durable jobs. Returns prompts and persists changes.

func (*Store) TickSessionJobs

func (s *Store) TickSessionJobs(now time.Time) []string

TickSessionJobs fires ready session jobs. Returns prompts and removes expired/one-shot.

Jump to

Keyboard shortcuts

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