Documentation
¶
Overview ¶
Package cron provides scheduled execution of serverless functions using cron expressions.
The scheduler executes functions via internal HTTP calls rather than direct invocation. This design was chosen to:
- Maintain compatibility with the existing function execution pipeline
- Reuse all middleware, logging, and metrics already in place
- Avoid duplicating execution logic and error handling
- Allow the scheduler to be stateless and easily replaceable
- Enable the same function to be triggered by both HTTP and cron without code changes
When a cron job triggers, it makes an HTTP POST request to the function's endpoint with special headers to indicate the execution source. The function handler then records the execution with the appropriate trigger type.
Index ¶
Constants ¶
const ( // HeaderTrigger identifies the execution trigger source HeaderTrigger = "X-Trigger" // HeaderCronSchedule contains the cron expression that triggered the execution HeaderCronSchedule = "X-Cron-Schedule" // HeaderCronFunctionID contains the function ID being executed HeaderCronFunctionID = "X-Cron-Function-Id" // HeaderCronFunctionName contains the function name being executed HeaderCronFunctionName = "X-Cron-Function-Name" // HeaderCronScheduledTime contains the scheduled execution time (Unix timestamp) HeaderCronScheduledTime = "X-Cron-Scheduled-Time" // TriggerValueCron is the value for X-Trigger header when triggered by cron TriggerValueCron = "cron" )
Header constants for cron-triggered executions
Variables ¶
This section is empty.
Functions ¶
func FormatNextRun ¶
FormatNextRun formats a time as a human-friendly relative string.
Types ¶
type FunctionScheduler ¶
type FunctionScheduler struct {
// contains filtered or unexported fields
}
FunctionScheduler manages cron schedules for serverless functions.
It executes functions via internal HTTP calls when their schedules trigger. This approach maintains compatibility with the existing execution pipeline, reusing all middleware, logging, metrics, and error handling already in place.
The scheduler is thread-safe and can be used concurrently. It maintains a mapping of function IDs to cron entry IDs to allow dynamic updates to schedules.
func NewScheduler ¶
func NewScheduler(db store.DB, baseURL string) *FunctionScheduler
NewScheduler creates a new function scheduler. baseURL should be the internal URL where functions can be invoked (e.g., "http://localhost:8080").
func (*FunctionScheduler) GetNextRun ¶
func (s *FunctionScheduler) GetNextRun(functionID string) *time.Time
GetNextRun calculates the next scheduled run time for a function. Returns nil if the function has no active schedule.
func (*FunctionScheduler) RefreshFunction ¶
func (s *FunctionScheduler) RefreshFunction(functionID string) error
RefreshFunction updates the cron schedule for a specific function. Call this after updating a function's cron settings.
func (*FunctionScheduler) Start ¶
func (s *FunctionScheduler) Start() error
Start initializes and starts the scheduler. It loads all functions with active cron schedules and begins scheduling them.
func (*FunctionScheduler) Stop ¶
func (s *FunctionScheduler) Stop()
Stop gracefully stops the scheduler.