boomerang

package module
v0.0.0-...-52c1b24 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 6 Imported by: 0

README

Boomerang 🪃

Simple distributed recurring task schedule for golang implemented on top of redis.

Test

Usage

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/redis/go-redis/redis/v9"
    "github.com/opsway-io/boomerang"
)

func main() {
    ctx := context.Background()

    cli := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
    })

    sch := boomerang.NewSchedule(cli)

    task := boomerang.NewTask(
        "greeter",
        "some-unique-id",
        []byte("Hello!"),
    )

    // Schedule task for execution every second starting from now
    if err := sch.Add(ctx, task, time.Second, time.Now()); err != nil {
        panic(err)
    }

    sch.On(ctx, "greeter", func(ctx context.Context, task *boomerang.Task) {
        fmt.Printf("%s\n", task.Data)
    })
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnexpectedReturnCode     = errors.New("unexpected return code from redis")
	ErrUnexpectedReturnCodeType = errors.New("unexpected return code type from redis, expected integer")
	ErrTaskAlreadyExists        = errors.New("task already exists")
	ErrTaskDoesNotExist         = errors.New("task does not exist")
	ErrTaskDataDoesNotExist     = errors.New("task data does not exist")
	ErrTaskDataInvalidFormat    = errors.New("task data has invalid format, expected JSON")
	ErrIntervalTooSmall         = errors.New("interval must be at least 1 millisecond")
)

Functions

This section is empty.

Types

type Schedule

type Schedule interface {
	Add(ctx context.Context, task *Task, interval time.Duration, firstExecution time.Time) error
	Remove(ctx context.Context, kind string, id string) error
	Exists(ctx context.Context, kind string, id string) (bool, error)
	RunNow(ctx context.Context, kind string, id string) error
	On(ctx context.Context, kind string, handler func(ctx context.Context, task *Task)) error
}

func NewSchedule

func NewSchedule(redisClient *redis.Client) Schedule

type ScheduleImpl

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

func (*ScheduleImpl) Add

func (s *ScheduleImpl) Add(ctx context.Context, task *Task, interval time.Duration, firstExecution time.Time) error

func (*ScheduleImpl) Exists

func (s *ScheduleImpl) Exists(ctx context.Context, kind string, id string) (bool, error)

func (*ScheduleImpl) On

func (s *ScheduleImpl) On(ctx context.Context, kind string, handler func(ctx context.Context, task *Task)) error

func (*ScheduleImpl) Remove

func (s *ScheduleImpl) Remove(ctx context.Context, kind string, id string) error

func (*ScheduleImpl) RunNow

func (s *ScheduleImpl) RunNow(ctx context.Context, kind string, id string) error

type Task

type Task struct {
	Kind string
	ID   string
	Data []byte
}

func NewTask

func NewTask(kind string, id string, data []byte) *Task

type TaskData

type TaskData struct {
	Interval time.Duration
	Data     []byte
}

Jump to

Keyboard shortcuts

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