scheduler

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 10 Imported by: 0

README

go-scheduler

CI Go Report Card Go Coverage

Go Reference License

Library for delayed sending message to channel with ability to cancel sending.

Installation

go get github.com/horockey/go-scheduler@latest

Examples

See /example directory

Events

Library is working with entities called events. Event is simply payload, wrapped with tags and headers:

type Event[T any] struct {
	Payload T
	tags    map[string]struct{}
	headers map[string]string
}

Creating event via NewEvent() method sets 2 default headers to it:

  • CREATED_AT header is set to time.Now()
  • ID header is set to uuid.NewString()

You are free to add any tags and headers to your events.

Note

Adding new tags and headers to event keep in mind, that they will be canonicalized (strings.ToUpper()) before actual addition to event

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotRunning          = fmt.Errorf("scheduller is not running")
	ErrEventNotFound       = fmt.Errorf("event for given filter not found")
	ErrUnexpectedEmptyList = fmt.Errorf("unexpected empty shedule event list")
	ErrEventWithNoIDHeader = fmt.Errorf("got event with no ID header. It will be generated")
)

Functions

func After

func After[T any](dur time.Duration) options.Option[model.Node[T]]

Emit event after given duration. Duration must be positive.

func At

func At[T any](t time.Time) options.Option[model.Node[T]]

Emit event at given time.

func ErrorCB

func ErrorCB[T any](cb func(error)) options.Option[Scheduler[T]]

Add custom error handler. By default it does noothing.

func EventHeaderCreatedAt added in v0.0.2

func EventHeaderCreatedAt() string

Get standart header for event creation time.

func EventHeaderID added in v0.0.2

func EventHeaderID() string

Get standart header for event ID.

func Every

func Every[T any](dur time.Duration) options.Option[model.Node[T]]

Continue eminitg event every given duration.

func Header[T any](k, v string) options.Option[model.Node[T]]

Add header to event. Header's key will be canonicalized before adding.

func OutChan

func OutChan[T any](ch chan *model.Event[T]) options.Option[Scheduler[T]]

Set custom out chan. Strongly recommended to be left default! Change it only if you know what you are doing!

func Tag

func Tag[T any](t string) options.Option[model.Node[T]]

Add a tag to event. Tag will be canonicalized before adding.

Types

type Scheduler

type Scheduler[T any] struct {
	// contains filtered or unexported fields
}

func NewScheduler

func NewScheduler[T any](opts ...options.Option[Scheduler[T]]) (*Scheduler[T], error)

Create new scheduler with given opts.

func (*Scheduler[T]) EmitChan

func (s *Scheduler[T]) EmitChan() <-chan *model.Event[T]

Get channel, that emits scheduled events.

func (*Scheduler[T]) Schedule

func (s *Scheduler[T]) Schedule(payload T, opts ...options.Option[model.Node[T]]) (*model.Event[T], error)

Schedule new event. Scheduler must be started to call this method properly.

func (*Scheduler[T]) Start

func (s *Scheduler[T]) Start(ctx context.Context) error

Start scheduler. To stop it, given context should be canceled.

func (*Scheduler[T]) Unschedule

func (s *Scheduler[T]) Unschedule(id string) error

Unschedule scheduled event by id. Scheduler must be started to call this method properly.

func (*Scheduler[T]) UnscheduleByHeader added in v0.1.0

func (s *Scheduler[T]) UnscheduleByHeader(key, value string) error

Unschedule all scheduled events with given header key-value pair. Scheduler must be started to call this method properly.

func (*Scheduler[T]) UnscheduleByTag added in v0.1.0

func (s *Scheduler[T]) UnscheduleByTag(tag string) error

Unschedule all scheduled events with given tag. Scheduler must be started to call this method properly.

Directories

Path Synopsis
example
internal

Jump to

Keyboard shortcuts

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