Documentation ¶
Overview ¶
Package scheduler provides a framework for managing and executing jobs periodically with configurable options for interval, maximum executions, and panic recovery. It supports concurrent job execution using worker goroutines and provides functionality to start, stop, and retrieve jobs by ID.
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job represents a periodic task that executes at specified intervals.
A Job continues to run until one of the following conditions is met:
- It is explicitly stopped using Stop().
- It reaches its maximum execution count, set with WithMaxExecutions().
- Its run function returns false, indicating it should not be rescheduled.
- A panic occurs during execution and job created WithStopOnPanic(true).
A RecoveryHandler can be provided via WithRecoveryHandler() to handle panics that occur during the execution of the run function. The default Recovery Handler logs an error.
Users can configure the Job to stop running after a panic using the WithStopOnPanic(). The default behavior is to continue running after a panic.
func NewJob ¶
NewJob creates and returns a pointer to a job that runs periodically when addded to a Scheduler. The job includes an ID, that must be unique within each Scheduler, a positive interval between executions, and a `run` function that executes at each interval. Optional settings can be applied using JobOptions.
A Job continues to run until one of the following conditions is met:
- It is explicitly stopped using Stop().
- It reaches its maximum execution count, set with WithMaxExecutions().
- Its run function returns false, indicating it should not be rescheduled.
- A panic occurs during execution and job created WithStopOnPanic(true).
If the interval is non-positive or `run` is nil, NewJob panics.
func (*Job) ID ¶
ID returns the unique identifier for the Job, which can be used to reference or manage the job within a scheduler.
type JobOption ¶
type JobOption func(*Job)
JobOption configures optional Job parameters.
func WithMaxExecutions ¶
WithMaxExecutions limits the number of times a job will run. If `n` is 0, the job runs indefinitely. Use this option to control how many times the job executes before stopping automatically.
func WithRecoveryHandler ¶
WithRecoveryHandler configures a custom function to handle panics during the job's execution. If the job's `run` function panics, this recovery function is called with the Job and the panic value, enabling custom error handling or cleanup.
func WithStopOnPanic ¶
WithStopOnPanic configures the Job to stop running after a panic occurs in the run function. If set to true, the Job will not be rescheduled after a panic. The default behavior is to continue running after a panic.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler manages job scheduling and execution using worker goroutines.
func NewScheduler ¶
func NewScheduler(bufferSize int, workerCount int, opts ...SchedulerOption) *Scheduler
NewScheduler creates a Scheduler with a job queue buffer and a specified number of worker goroutines.
bufferSize controls the maximum number of jobs that can be queued at once. If the queue is full, new jobs may be rejected until space becomes available.
workerCount sets the number of goroutines that process jobs concurrently. More workers allow multiple jobs to run at the same time.
opts like WithLogger allow further customization.
func (*Scheduler) AddJob ¶
AddJob adds a job to the scheduler for execution. It returns an error if the job ID already exists.
type SchedulerOption ¶
type SchedulerOption func(*Scheduler)
SchedulerOption configures optional settings for a Scheduler.
func WithLogger ¶
func WithLogger(l *slog.Logger) SchedulerOption
WithLogger sets a custom logger for the Scheduler.