Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunForever ¶ added in v0.2.0
RunForever runs the given runners every specified interval. It continuously executes the `Run` method of each provided Runner until the context is done. The runners are ran sequentially in a single thread.
Parameters: - ctx: The context to control the lifecycle of the function. When the context is done, the function stops running. - interval: The duration between each execution of the runners. - runners: A variadic parameter of Runner interfaces to be executed.
The function uses a ticker to trigger the execution of the runners at the specified interval. If the context is done, the function stops and returns.
Example usage:
ctx, cancel := context.WithCancel(context.Background()) defer cancel() RunForever(ctx, time.Minute, runner1, runner2)
Types ¶
type Periodic ¶
type Periodic struct {
// contains filtered or unexported fields
}
func NewScheduler ¶
func (*Periodic) ScheduleEvery ¶
func (s *Periodic) ScheduleEvery(interval time.Duration, name string, fn func(ctx context.Context) error)
ScheduleEvery schedules a function to run at a specified interval. It continuously executes the provided function until the scheduler is closed. Scheduler implements Elector interface. This allows the scheduler to run only on the leader node.
Parameters: - interval: The duration between each execution of the function. - name: A string representing the name of the scheduled task, used for logging purposes. - fn: The function to be executed at each interval. It receives a context parameter.
The function uses a ticker to trigger the execution of the provided function at the specified interval. If the scheduler is closed, the function stops running.
Example usage:
scheduler := NewScheduler(elector)
scheduler.Open(ctx)
scheduler.ScheduleEvery(time.Minute, "example-task", func(ctx context.Context) error {
// Task implementation
return nil
})