scheduler

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunForever added in v0.2.0

func RunForever(ctx context.Context, interval time.Duration, runners ...Runner)

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 Elector

type Elector interface {
	IsLeader() bool
}

type Periodic

type Periodic struct {
	// contains filtered or unexported fields
}

func NewScheduler

func NewScheduler(elector Elector) *Periodic

func (*Periodic) Close

func (s *Periodic) Close() error

func (*Periodic) Open

func (s *Periodic) Open(ctx context.Context) error

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
})

type Runner added in v0.2.0

type Runner interface {
	Run(ctx context.Context) error
	Name() string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL