Documentation
¶
Index ¶
- Constants
- Variables
- func StrategyRegister(st StrategyType, strategy Strategy)
- type Config
- type Middleware
- type MiddlewareFunc
- type Option
- type Repeater
- type Strategy
- type StrategyType
- type Task
- func NewTask(name string, process runner.Process, delay time.Duration, ...) *Task
- func NewTaskWithLock(name string, process runner.Process, delay time.Duration, options ...Option) *Task
- func NewTaskWithUnlock(name string, process runner.Process, delay time.Duration, options ...Option) *Task
- func SkipFirstRun(task *Task) *Task
Constants ¶
const (
DelayFieldName = "repeater.delay"
)
Variables ¶
var Component = &component.Component{ Dependencies: component.Components{ runner.Component, }, Init: component.StepFunc(func(container container.Container) error { return container.Provides( NewConfig, NewRepeater, ) }), BindFlags: component.BindFlags(func(flagSet flag.FlagSet, container container.Container) error { return container.Invoke(func(config *Config) { flagSet.DurationVar(&config.Delay, DelayFieldName, DelayDefault, "") }) }), Configuration: component.StepFunc(func(container container.Container) error { return container.Invoke(Configuration) }), Execute: component.StepFunc(func(container container.Container) error { return container.Invoke(func(r runner.Runner, repeater Repeater) error { return r.RunTask(runner.NewTask("repeater", repeater)) }) }), Stop: component.StepFunc(func(container container.Container) error { return container.Invoke(func(repeater Repeater) error { return repeater.Close() }) }), }
Component is a ready-to-use Compogo component that provides the Repeater. It automatically:
- Registers Config and Repeater in the DI container
- Adds command-line flags for ticker interval
- Starts the Repeater as a runner task during Run phase
- Stops the Repeater during Stop phase
Usage:
compogo.WithComponents(
runner.Component,
repeater.Component,
)
var (
DelayDefault = time.Second / 60
)
Functions ¶
func StrategyRegister ¶
func StrategyRegister(st StrategyType, strategy Strategy)
StrategyRegister registers a new strategy implementation. This allows extending the repeater with custom strategies.
Types ¶
type Config ¶
func Configuration ¶
func Configuration(config *Config, configurator configurator.Configurator) *Config
type Middleware ¶
type Middleware interface {
// Middleware wraps a periodic task's process function.
// It receives the periodic task and the next Process in the chain,
// and returns a new Process that will be executed instead.
Middleware(task *Task, next runner.Process) runner.Process
}
Middleware defines the interface for task middleware. Middlewares can wrap a task's Process function to add cross-cutting concerns such as logging, metrics, or error handling specific to periodic tasks.
type MiddlewareFunc ¶
MiddlewareFunc is a function adapter that allows ordinary functions to be used as Middleware implementations for periodic tasks.
func (MiddlewareFunc) Middleware ¶
Middleware implements the Middleware interface by calling the underlying function.
type Option ¶
Option is a function that configures a Task during creation. Options are applied in the order they are provided.
type Repeater ¶
type Repeater interface {
// Closer stops all tasks and cleans up resources.
io.Closer
// Process implements runner.Process, allowing Repeater to be run as a task.
// It periodically checks for tasks to execute and launches them via Runner.
runner.Process
// AddTasks adds multiple periodic tasks to the scheduler.
AddTasks(tasks ...*Task) error
// AddTask adds a single periodic task to the scheduler.
AddTask(task *Task) error
// RemoveTask removes a periodic task and stops all its running instances.
RemoveTask(task *Task) error
// RemoveTaskByName removes a periodic task by name and stops all its instances.
RemoveTaskByName(name string) error
// HasTaskByName checks if a periodic task with the given name exists.
HasTaskByName(name string) bool
// HasTask checks if the specific task instance exists in the scheduler.
HasTask(task *Task) bool
// Use registers middlewares that wrap all periodic task executions.
Use(middlewares ...Middleware)
}
Repeater defines the interface for a periodic task scheduler. It manages a collection of tasks, executes them on schedule, and ensures proper cleanup on shutdown.
type Strategy ¶
type Strategy interface {
// IsTaskRun determines whether a new instance of the task should be started.
IsTaskRun(task *Task, runner runner.Runner) bool
// GenerateName creates a unique name for a task instance.
// For Lock strategy, this returns the base name; for Unlock, it appends a counter.
GenerateName(task *Task) string
}
Strategy defines the interface for task execution strategies. Each strategy decides when a task should run and how to name its instances.
type StrategyType ¶
type StrategyType uint8
StrategyType defines how a periodic task handles concurrent executions.
const ( // Lock strategy ensures only one instance of the task runs at any time. // If a task is still running when its next execution is due, it is skipped. Lock StrategyType = iota // Unlock strategy allows multiple instances of the task to run concurrently. // Each execution creates a new task instance with a unique name. Unlock )
func (StrategyType) String ¶
func (s StrategyType) String() string
type Task ¶
type Task struct {
// contains filtered or unexported fields
}
Task represents a periodic task managed by the Repeater. It contains the task logic, execution schedule, and strategy for concurrent execution.
func NewTask ¶
func NewTask(name string, process runner.Process, delay time.Duration, strategy StrategyType, options ...Option) *Task
NewTask creates a new periodic task with the specified strategy. This is the base constructor that all other constructors use.
func NewTaskWithLock ¶
func NewTaskWithLock(name string, process runner.Process, delay time.Duration, options ...Option) *Task
NewTaskWithLock creates a new periodic task with Lock strategy. Lock strategy ensures only one instance of this task runs at any given time. The task will execute every 'delay' duration.
func NewTaskWithUnlock ¶
func NewTaskWithUnlock(name string, process runner.Process, delay time.Duration, options ...Option) *Task
NewTaskWithUnlock creates a new periodic task with Unlock strategy. Unlock strategy allows multiple instances of this task to run concurrently. The task will execute every 'delay' duration.
func SkipFirstRun ¶
SkipFirstRun configures the task to skip its first scheduled execution. Useful when you want tasks to start after a delay rather than immediately.
func (*Task) RunNumbers ¶
RunNumbers returns the number of times this task has been executed.
func (*Task) Strategy ¶
func (t *Task) Strategy() StrategyType
Strategy returns the task's execution strategy (Lock/Unlock).