runners

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 7 Imported by: 2

README

go-runners

Provides a means a simple means of managing long-running functions and services

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithCancel added in v1.1.0

func ContextWithCancel() (context.Context, context.CancelFunc)

ContextWithCancel returns a new context.Context impl with cancel.

func Run added in v1.1.0

func Run(fn func() error) (err error)

Run will execute the supplied 'fn' catching any panics. Returns either function-returned error or formatted panic.

Types

type FuncRunner added in v1.1.0

type FuncRunner struct {
	// HandOff is the time after which a blocking function will be considered handed off
	HandOff time.Duration

	// ErrorHandler is the function that errors are passed to when encountered by the
	// provided function. This can be used both for logging, and for error filtering
	ErrorHandler func(err error) error
	// contains filtered or unexported fields
}

FuncRunner provides a means of managing long-running functions e.g. main logic loops.

func (*FuncRunner) Err added in v1.1.0

func (r *FuncRunner) Err() error

Err returns the last-set error value.

func (*FuncRunner) Go added in v1.1.0

func (r *FuncRunner) Go(fn func(ctx context.Context) error) bool

Go will attempt to run 'fn' asynchronously. The provided context is used to propagate requested cancel if FuncRunner.Stop() is called. Any returned error will be passed to FuncRunner.ErrorHandler for filtering/logging/etc. Any blocking functions will be waited on for FuncRunner.HandOff amount of time before considering the function as handed off. Returned bool is success state, i.e. returns true if function is successfully handed off or returns within hand off time with nil error.

func (*FuncRunner) Stop added in v1.1.0

func (r *FuncRunner) Stop() bool

Stop will cancel the context supplied to the running function.

type Service

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

Service provides a means of tracking a single long-running service, provided protected state changes and preventing multiple instances running. Also providing service state information.

func (*Service) Done

func (svc *Service) Done() <-chan struct{}

Done returns a channel that's closed when Service.Stop() is called. It is the same channel provided to the currently running service function.

func (*Service) GoRun added in v1.3.0

func (svc *Service) GoRun(fn func(context.Context)) bool

GoRun will run the supplied function until completion in a goroutine, using given context to propagate cancel. Immediately returns boolean indicating success, or that service is already running.

func (*Service) Run

func (svc *Service) Run(fn func(context.Context)) bool

Run will run the supplied function until completion, using given context to propagate cancel. Immediately returns false if the Service is already running, and true after completed run.

func (*Service) Running

func (svc *Service) Running() bool

Running returns if Service is running (i.e. state NOT stopped / stopping).

func (*Service) Stop

func (svc *Service) Stop() bool

Stop will attempt to stop the service, cancelling the running function's context. Immediately returns false if not running, and true only after Service is fully stopped.

type WorkerFunc added in v1.2.0

type WorkerFunc func(context.Context)

WorkerFunc represents a function processable by a worker in WorkerPool. Note that implementations absolutely MUST check whether passed context is <-ctx.Done() otherwise stopping the pool may block indefinitely.

type WorkerPool added in v1.2.0

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

WorkerPool provides a means of enqueuing asynchronous work.

func (*WorkerPool) Enqueue added in v1.2.0

func (pool *WorkerPool) Enqueue(fn WorkerFunc)

Enqueue will add provided WorkerFunc to the queue to be performed when there is a free worker. This will block until function is queued or pool is stopped. In all cases, the WorkerFunc will be executed, with the state of the pool being indicated by <-ctx.Done() of the passed ctx. WorkerFuncs MUST respect the passed context.

func (*WorkerPool) EnqueueCtx added in v1.3.0

func (pool *WorkerPool) EnqueueCtx(ctx context.Context, fn WorkerFunc)

EnqueueCtx is functionally identical to WorkerPool.Enqueue() but returns early in the case that caller provided <-ctx.Done() is closed, WITHOUT running the WorkerFunc.

func (*WorkerPool) EnqueueNow added in v1.3.0

func (pool *WorkerPool) EnqueueNow(fn WorkerFunc) bool

EnqueueNow attempts Enqueue but returns false if not executed.

func (*WorkerPool) Queue added in v1.2.0

func (pool *WorkerPool) Queue() int

Queue returns the number of currently queued WorkerFuncs.

func (*WorkerPool) Start added in v1.2.0

func (pool *WorkerPool) Start(workers int, queue int) bool

Start will start the main WorkerPool management loop in a new goroutine, along with requested number of child worker goroutines. Returns false if already running.

func (*WorkerPool) Stop added in v1.2.0

func (pool *WorkerPool) Stop() bool

Stop will stop the WorkerPool management loop, blocking until stopped.

Jump to

Keyboard shortcuts

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