Documentation
¶
Overview ¶
Package cron provides scheduling functionality for executing tasks at specified intervals.
This library offers three execution strategies:
- Expression-based scheduling using cron expressions (e.g., "@every 1h", "0 */15 * * * ?")
- Duration-based intervals for repeated execution at fixed time intervals
- One-time execution for tasks that should run only once
The package includes configurable options for timeouts, metrics collection, and parallel execution control. All schedulers support graceful context-based cancellation.
Index ¶
- func CreateDefaultParser() cron.Parser
- func NewCronJob(oneTime bool, expression Expression, wait libtime.Duration, ...) run.Runnable
- func NewCronJobWithOptions(oneTime bool, expression Expression, wait libtime.Duration, ...) run.Runnable
- func NewExpressionCron(expression Expression, action run.Runnable) run.Runnable
- func NewExpressionCronWithOptions(expression Expression, action run.Runnable, options Options) run.Runnable
- func NewIntervalCron(wait libtime.Duration, action run.Runnable) run.Runnable
- func NewIntervalCronWithOptions(wait libtime.Duration, action run.Runnable, options Options) run.Runnable
- func NewOneTimeCron(action run.Runnable) run.Runnable
- func NewOneTimeCronWithOptions(action run.Runnable, options Options) run.Runnable
- func NewWaitCron(wait libtime.Duration, action run.Runnable) run.Runnable
- func WrapWithMetrics(name string, fn run.Runnable) run.Runnable
- func WrapWithOptions(action run.Runnable, options Options) run.Runnable
- func WrapWithTimeout(name string, timeout libtime.Duration, fn run.Runnable) run.Runnable
- type Cron
- type Expression
- type Metrics
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateDefaultParser ¶ added in v1.2.0
CreateDefaultParser creates a cron expression parser with second-level precision. The parser supports the standard cron format with optional seconds field.
func NewCronJob ¶
func NewCronJob( oneTime bool, expression Expression, wait libtime.Duration, action run.Runnable, ) run.Runnable
NewCronJob creates a new cron job with automatic strategy selection based on parameters. Uses one-time execution if oneTime is true, expression-based scheduling if expression is provided, or duration-based intervals if wait duration is specified.
func NewCronJobWithOptions ¶ added in v1.4.0
func NewCronJobWithOptions( oneTime bool, expression Expression, wait libtime.Duration, action run.Runnable, options Options, ) run.Runnable
NewCronJobWithOptions creates a new cron job with configurable options. Applies the same strategy selection as NewCronJob but with additional wrappers for timeout, metrics, and parallel execution control based on the provided options.
func NewExpressionCron ¶
func NewExpressionCron( expression Expression, action run.Runnable, ) run.Runnable
NewExpressionCron creates a cron job that executes based on a cron expression. The expression supports standard cron format and common descriptors like '@every 1h'.
func NewExpressionCronWithOptions ¶ added in v1.5.0
func NewExpressionCronWithOptions( expression Expression, action run.Runnable, options Options, ) run.Runnable
NewExpressionCronWithOptions creates an expression-based cron job with configurable options. Applies timeout, metrics, and parallel execution controls to individual action executions.
func NewIntervalCron ¶ added in v1.3.1
NewIntervalCron creates a cron job that executes at fixed time intervals. The job runs continuously with the specified wait duration between executions.
func NewIntervalCronWithOptions ¶ added in v1.5.0
func NewIntervalCronWithOptions( wait libtime.Duration, action run.Runnable, options Options, ) run.Runnable
NewIntervalCronWithOptions creates an interval-based cron job with configurable options. Applies timeout, metrics, and parallel execution controls to individual action executions.
func NewOneTimeCron ¶
NewOneTimeCron creates a cron job that executes only once. The job completes after a single execution of the provided action.
func NewOneTimeCronWithOptions ¶ added in v1.5.0
NewOneTimeCronWithOptions creates a one-time cron job with configurable options. Applies timeout, metrics, and parallel execution controls to the single action execution.
func NewWaitCron ¶
NewWaitCron Deprecated: use NewIntervalCron instead
func WrapWithMetrics ¶ added in v1.4.0
WrapWithMetrics wraps a runnable with Prometheus metrics collection. Records start, completion, failure counts and execution duration for the named job.
func WrapWithOptions ¶ added in v1.5.0
WrapWithOptions applies all configured wrappers to an action based on the provided options. Wrappers are applied in this order (innermost to outermost): 1. Timeout wrapper (if timeout > 0) 2. Metrics wrapper (if enabled) 3. Parallel skip wrapper (if enabled)
func WrapWithTimeout ¶ added in v1.4.0
WrapWithTimeout wraps a runnable with timeout enforcement. If timeout is <= 0, the original runnable is returned unchanged. Otherwise, executions are cancelled if they exceed the specified duration.
Types ¶
type Cron ¶
type Cron interface {
// Run executes the cron job until the context is cancelled or an error occurs.
// For recurring jobs, this method blocks and continues scheduling executions.
// For one-time jobs, this method returns after a single execution.
Run(ctx context.Context) error
}
Cron represents a scheduled task that can be executed with context-based cancellation.
type Expression ¶ added in v1.2.0
type Expression string
Expression of the cron cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor every second: * * * * * ? every minute 0 * * * * ? every 15 minute 0 */15 * * * ? every hour: 0 0 * * * ? every hour on sunday: 0 0 * * * 0
func (Expression) Bytes ¶ added in v1.2.0
func (e Expression) Bytes() []byte
Bytes returns the cron expression as a byte slice.
func (Expression) Ptr ¶ added in v1.2.0
func (e Expression) Ptr() *Expression
Ptr returns a pointer to the Expression value.
func (Expression) String ¶ added in v1.2.0
func (e Expression) String() string
String returns the cron expression as a string.
type Metrics ¶ added in v1.4.0
type Metrics interface {
// IncreaseStarted increments the counter for cron job start events.
IncreaseStarted(name string)
// IncreaseFailed increments the counter for cron job failure events.
IncreaseFailed(name string)
// IncreaseCompleted increments the counter for cron job completion events.
IncreaseCompleted(name string)
// SetLastSuccessToCurrent records the timestamp of the last successful execution.
SetLastSuccessToCurrent(name string)
// ObserveDuration records the execution duration in seconds for the named cron job.
ObserveDuration(name string, durationSeconds float64)
}
Metrics provides methods for collecting and reporting cron job execution statistics.
func NewMetrics ¶ added in v1.4.0
func NewMetrics() Metrics
NewMetrics creates a new Metrics instance that reports to Prometheus.
type Options ¶ added in v1.5.1
type Options struct {
// Name identifies the cron job for logging and metrics.
Name string
// EnableMetrics enables duration and execution metrics collection.
EnableMetrics bool
// Timeout sets the maximum duration allowed for individual action executions.
// A value of 0 disables timeout enforcement.
Timeout libtime.Duration
// ParallelSkip prevents multiple instances of the same cron from running concurrently.
ParallelSkip bool
}
Options configures behavior for cron jobs with wrappers applied.
func DefaultOptions ¶ added in v1.5.1
func DefaultOptions() Options
DefaultOptions returns a new Options with default values. The default configuration has metrics disabled, no timeout, and allows parallel execution.