scheduler

package module
v0.0.0-...-6fba78e Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2019 License: MIT Imports: 5 Imported by: 1

README

scheduler

this package provides an easy way to schedule functions using an event trigger.

scheduling a function

A timeout trigger can be defined as follows:

trigger := scheduler.NewTimeoutTrigger(time.Second)

which can then be used to schedule functions as follows:

group, ctx := scheduler.New(ctx, trigger)

where, ctx in input is the parent context and one in output is to be used to pass during scheduling functions.

group.Go(ctx, func() error { 
	// your logic
	return nil
})

Finally wait for all functions to return using Wait:

// wait for func executions to be over.
err := group.Wait()

triggers

triggers can be of following types:

// a timeout trigger allows execution on a timeout
timeoutTrigger := scheduler.NewTimeoutTrigger(timeDuration)
// trigger now causes functions to execute immediately
triggerNow := scheduler.TriggerNow()
// similarly, an event can used to trigger functions, event being ctx.Done()
triggerOnEvent := scheduler.NewContextTrigger(ctx)

Documentation

Overview

package scheduler defines func scheduling interface and provides implementation for deferred execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

Error is used to build error constants.

const (
	FuncExecCancelled Error = "function execution was cancelled by context"
	KeyNotFound       Error = "key not found"
)

func (Error) Error

func (e Error) Error() string

Error implements Error interface for Error type.

type Scheduler

type Scheduler interface {
	// Go executes function and returns a key which can be used to cancel func execution.
	Go(ctx context.Context, f func() error) string
	// Cancel cancels execution of func if pending execution.
	Cancel(key string) error
	// Wait waits for all functions dispatched using Go to finish.
	Wait() error
}

Scheduler defines interface to schedule execution of a func.

func New

func New(ctx context.Context, trig Trigger) (Scheduler, context.Context)

New creates a new scheduler based on an input context and trigger mode. Scheduler will cancel all pending jobs if the input context is cancelled. Scheduler will trigger jobs based on trigger. It returns a context that should be used in each function. This output context is cancelled on first error in any of the scheduled jobs.

type Trigger

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

func NewChannelTrigger

func NewChannelTrigger(c chan struct{}) Trigger

NewChannelTrigger provides a new trigger based on a channel. Trigger fires when input chan is read.

func NewContextTrigger

func NewContextTrigger(ctx context.Context) Trigger

NewContextTrigger provides a new trigger based on a context. Trigger fires when input context context is done.

func NewTimeoutTrigger

func NewTimeoutTrigger(dur time.Duration) Trigger

NewTimeoutTrigger provides a new trigger based on a timeout.

func TriggerNow

func TriggerNow() Trigger

TriggerNow provides an immediate trigger mechanism

Jump to

Keyboard shortcuts

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