Help us shape the future of Go! Take the 2026 Go Developer Survey

schedule

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGraphWorkflowRequired = errors.New("graph workflow id is required")
	ErrGraphWorkflowNotFound = errors.New("graph workflow not found")
)
View Source
var ErrScheduleMaxConcurrent = errors.New("schedule max concurrent reached")

ErrScheduleMaxConcurrent is returned by RunNow when a manual trigger is refused because the schedule already has maxConcurrent runs in flight. The HTTP layer maps it to 409 Conflict and, unlike a real trigger failure, it is NOT recorded on the task (no slot was taken, no run was attempted).

Functions

func NextCronTime

func NextCronTime(expr string, after time.Time) *time.Time

NextCronTime calculates the next fire time after `after` for the given cron expression.

func ValidateCronExpr

func ValidateCronExpr(expr string) error

ValidateCronExpr checks if a cron expression is syntactically valid.

Types

type Scheduler

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

Scheduler manages cron-based execution of scheduled tasks.

func NewScheduler

func NewScheduler(svc Service, trigger TriggerFunc) *Scheduler

func (*Scheduler) MarkDone

func (s *Scheduler) MarkDone(ctx context.Context, scheduleID, jobID string, status model.JobStatus)

MarkDone updates a task's LastStatus when its job completes and decrements the running count so the scheduler can trigger new runs.

jobID is the ID of the job that actually finished. It is NOT read from task.LastRunJobID: with maxConcurrent>1 (or a fast-completing job whose RecordTrigger hasn't landed yet) LastRunJobID can point at a sibling run, and using it here would make logs and status reflect the wrong job.

func (*Scheduler) RecordTrigger

func (s *Scheduler) RecordTrigger(ctx context.Context, scheduleID, jobID, cronExpr string, triggerErr error)

RecordTrigger atomically updates a scheduled task's status after a trigger attempt. Called by both tryTrigger (cron) and RunNow (manual) paths to avoid duplicated logic.

func (*Scheduler) Reload

func (s *Scheduler) Reload()

Reload is a no-op signal for future use. The tick-based scheduler re-reads all tasks every minute, so changes take effect automatically.

func (*Scheduler) RunNow

func (s *Scheduler) RunNow(ctx context.Context, task *model.ScheduledTask) (string, error)

RunNow manually triggers a task, respecting maxConcurrent. Returns the job ID or an error.

RunNow records the trigger outcome itself (success or failure) via RecordTrigger, so cron (tryTrigger) and manual paths share one failure-record rule per the design's "手动立即运行与 cron 触发统一失败记录". An over-limit refusal returns ErrScheduleMaxConcurrent and is intentionally NOT recorded: no slot was taken and no run was attempted. RecordTrigger runs on a background context so a disconnected HTTP request can't cancel the status write-back.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context)

Start begins the scheduling loop. It checks every minute for tasks that should fire.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop gracefully stops the scheduler.

type Service

type Service interface {
	Create(ctx context.Context, req *model.CreateScheduleRequest) (*model.ScheduledTask, error)
	Get(ctx context.Context, id string) (*model.ScheduledTask, error)
	List(ctx context.Context) ([]*model.ScheduledTask, error)
	ListByWorkspace(ctx context.Context, wsID string) ([]*model.ScheduledTask, error)
	Update(ctx context.Context, id string, req *model.UpdateScheduleRequest) (*model.ScheduledTask, error)
	Toggle(ctx context.Context, id string) (*model.ScheduledTask, error)
	Delete(ctx context.Context, id string) error
	SaveState(ctx context.Context, task *model.ScheduledTask) error
}

Service provides CRUD operations for scheduled tasks.

func NewService

func NewService() (Service, error)

type TriggerFunc

type TriggerFunc func(ctx context.Context, task *model.ScheduledTask) (jobID string, err error)

TriggerFunc is called by the scheduler to create and start a job for the given task.

Jump to

Keyboard shortcuts

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