Documentation
ΒΆ
Index ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( // ErrTaskIDInUse is returned when attempting to add a task with an ID that already exists. ErrTaskIDInUse = errors.New("task id already in use") // ErrTaskFuncNil is returned when a task is created without a function to execute. ErrTaskFuncNil = errors.New("task function cannot be nil") // ErrTaskInterval is returned when a task is created without a valid interval. ErrTaskInterval = errors.New("task interval must be defined") // ErrInvalidTaskInterval is returned when a task is created with an invalid interval format. ErrInvalidTaskInterval = errors.New("invalid task interval format") // ErrTaskNotFound is returned when attempting to lookup a task that doesn't exist. ErrTaskNotFound = errors.New("could not find task within the task list") )
Functions ΒΆ
This section is empty.
Types ΒΆ
type Logger ΒΆ
type Logger interface {
WithField(key string, value any) Logger
WithError(err error) Logger
// Standard log functions
Debug(args ...any)
Info(args ...any)
}
Defines the logger interface
type Scheduler ΒΆ
type Scheduler struct {
sync.RWMutex // RWMutex for managing concurrent access to tasks.
// contains filtered or unexported fields
}
Scheduler manages a collection of scheduled tasks. It provides thread-safe operations for adding, removing, and looking up tasks by their unique identifiers.
func NewTaskScheduler ΒΆ
func NewTaskScheduler(opts ...SchedulerOpts) *Scheduler
NewTaskScheduler creates and returns a new instance of the task scheduler. The scheduler is ready to use immediately after creation.
func (*Scheduler) Add ΒΆ
Add registers a new task with the scheduler using the provided ID. The task will be validated and scheduled according to its configuration. Returns an error if the ID is already in use or if the task configuration is invalid.
Example:
task := &Task{
Interval: "5s",
TaskFunc: func(ctx context.Context) error {
fmt.Println("Task executed")
return nil
},
}
err := scheduler.Add("my-task", task)
func (*Scheduler) Del ΒΆ
Del removes a task from the scheduler by its ID. The task's context will be cancelled and its timer will be stopped. If the task doesn't exist, this operation is a no-op.
type SchedulerOpts ΒΆ
type SchedulerOpts func(*Scheduler)
SchedulerOptions defines configuration options for the Scheduler.
func WithLogger ΒΆ
func WithLogger(logger Logger) SchedulerOpts
WithLogger sets a custom logger for the Scheduler.
type Task ΒΆ
type Task struct {
sync.RWMutex // Managing concurrent modifications to task properties.
// TaskContext provides contextual information and ID for the task.
TaskContext TaskContext
// Interval specifies when the task should run. Can be a duration string (e.g., "5s", "1m")
// or a cron expression (e.g., "0 0 * * *" for daily at midnight).
Interval string
// RunOnce indicates if the task should run only once and then be removed from the scheduler.
RunOnce bool
// FirstRun indicates if the task should run immediately upon being scheduled.
FirstRun bool
// RunSingleInstance prevents concurrent instances of the task from running.
// If true, a new execution will be skipped if the previous one is still running.
RunSingleInstance bool
// StartAfter specifies the earliest time when the task should start executing.
// The task will not run before this time.
StartAfter time.Time
// TaskFunc is the main function to execute. It receives a context for cancellation.
// Either TaskFunc or FuncWithTaskContext must be provided.
TaskFunc func(ctx context.Context) error
// ErrFunc is called when TaskFunc returns an error.
ErrFunc func(error)
// FuncWithTaskContext is an alternative to TaskFunc that receives TaskContext instead of context.Context.
// Either TaskFunc or FuncWithTaskContext must be provided.
FuncWithTaskContext func(TaskContext) error
// ErrFuncWithTaskContext is called when FuncWithTaskContext returns an error.
ErrFuncWithTaskContext func(TaskContext, error)
// contains filtered or unexported fields
}
Task represents a scheduled task with its configuration and execution parameters. It supports various execution modes including one-time runs, recurring intervals, and cron-based scheduling.
type TaskContext ΒΆ
type TaskContext struct {
Context context.Context // Context for managing the task's lifecycle.
// contains filtered or unexported fields
}
TaskContext provides contextual information for task execution, including the task's unique identifier and execution context.
func (TaskContext) ID ΒΆ
func (ctx TaskContext) ID() string
ID returns the unique identifier of the task.