scheduler

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NextRun added in v0.1.5

func NextRun(cronExpr string, from time.Time) (time.Time, error)

NextRun computes the next run time for a cron expression from a given point in time.

func ValidateCron

func ValidateCron(expr string) error

ValidateCron validates a standard 5-field cron expression.

Types

type CronScheduler

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

CronScheduler manages scheduled workflow executions.

func NewCronScheduler

func NewCronScheduler(trigger WorkflowTrigger) *CronScheduler

NewCronScheduler creates a new CronScheduler.

func (*CronScheduler) Create

func (s *CronScheduler) Create(job *ScheduledJob) error

Create adds a new scheduled job. It validates the cron expression and returns an error if invalid.

func (*CronScheduler) Delete

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

Delete soft-deletes a job and stops its execution loop.

func (*CronScheduler) ExecuteNow

func (s *CronScheduler) ExecuteNow(ctx context.Context, id string) (*ExecutionRecord, error)

ExecuteNow triggers immediate execution of a job (bypasses schedule).

func (*CronScheduler) Get

func (s *CronScheduler) Get(id string) (*ScheduledJob, bool)

Get returns a job by ID.

func (*CronScheduler) History

func (s *CronScheduler) History(jobID string) []*ExecutionRecord

History returns execution records for a job, newest first.

func (*CronScheduler) List

func (s *CronScheduler) List() []*ScheduledJob

List returns all non-deleted jobs sorted by creation time.

func (*CronScheduler) NextRuns

func (s *CronScheduler) NextRuns(cronExpr string, n int) ([]time.Time, error)

NextRuns returns up to n upcoming execution times for a given cron expression.

func (*CronScheduler) Pause

func (s *CronScheduler) Pause(id string) error

Pause pauses a job.

func (*CronScheduler) Resume

func (s *CronScheduler) Resume(id string) error

Resume resumes a paused job.

func (*CronScheduler) SetNextRunFunc

func (s *CronScheduler) SetNextRunFunc(fn func(string, time.Time) (time.Time, error))

SetNextRunFunc allows overriding the next-run calculation (useful for testing).

func (*CronScheduler) Update

func (s *CronScheduler) Update(id string, name, cronExpr, workflowType, action string, params map[string]any) error

Update modifies a scheduled job.

type ExecutionRecord

type ExecutionRecord struct {
	ID        string          `json:"id"`
	JobID     string          `json:"jobId"`
	Status    ExecutionStatus `json:"status"`
	StartedAt time.Time       `json:"startedAt"`
	Duration  time.Duration   `json:"duration"`
	Error     string          `json:"error,omitempty"`
}

ExecutionRecord records the result of a single job execution.

type ExecutionStatus

type ExecutionStatus string

ExecutionStatus represents the result of a job execution.

const (
	ExecStatusSuccess ExecutionStatus = "success"
	ExecStatusFailed  ExecutionStatus = "failed"
	ExecStatusSkipped ExecutionStatus = "skipped"
)

type Handler

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

Handler provides HTTP endpoints for scheduled job management.

func NewHandler

func NewHandler(scheduler *CronScheduler) *Handler

NewHandler creates a new scheduler HTTP handler.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers scheduler API routes on the given mux.

type JobStatus

type JobStatus string

JobStatus represents the status of a scheduled job.

const (
	JobStatusActive  JobStatus = "active"
	JobStatusPaused  JobStatus = "paused"
	JobStatusDeleted JobStatus = "deleted"
)

type ScheduledJob

type ScheduledJob struct {
	ID           string         `json:"id"`
	Name         string         `json:"name"`
	CronExpr     string         `json:"cronExpr"`
	WorkflowType string         `json:"workflowType"`
	Action       string         `json:"action"`
	Params       map[string]any `json:"params,omitempty"`
	Status       JobStatus      `json:"status"`
	CreatedAt    time.Time      `json:"createdAt"`
	UpdatedAt    time.Time      `json:"updatedAt"`
	LastRunAt    *time.Time     `json:"lastRunAt,omitempty"`
	NextRunAt    *time.Time     `json:"nextRunAt,omitempty"`
}

ScheduledJob represents a job that runs on a cron schedule.

type WorkflowTrigger

type WorkflowTrigger func(ctx context.Context, workflowType, action string, data map[string]any) error

WorkflowTrigger is the function signature for triggering workflows.

Jump to

Keyboard shortcuts

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