seqmgr

package
v0.0.0-...-dada8fb Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package seqmgr Created by Teocci. Author: teocci@yandex.com on 2021-Aug-28

Package seqmgr Created by Teocci. Author: teocci@yandex.com on 2021-Aug-28 Desc: this package provides a general-purpose boot sequence manager with separate startup/shutdown phases, cancellation and a simple abstraction that allows easy control over execution order and concurrency.

Quick Start

seq := seqmgr.New("My Boot Sequence")
seq.Add("some-service", someServiceUp, someServiceDown)
seq.Add("other-service", otherServiceUp, otherServiceDown)
seq.Add("third-service", thirdServiceUp, thirdServiceDown)

// Execute "some-service" and "other-service" concurrently, followed by "third-service".
seq.Sequence("(some-service : other-service) > third-service")
up := seq.Up(context.Background())
up.Wait()

// Your application is now ready!

down := up.Down(context.Background())
down.Wait()

Please refer to the enclosed README for more details.

Package seqmgr Created by Teocci. Author: teocci@yandex.com on 2021-Aug-28

Package seqmgr Created by Teocci. Author: teocci@yandex.com on 2021-Aug-28

Package seqmgr Created by Teocci. Author: teocci@yandex.com on 2021-Aug-28

Package seqmgr Created by Teocci. Author: teocci@yandex.com on 2021-Aug-28

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NoOp

func NoOp() error

NoOp (no operation) is a convenience function you can use in place of a Service Func for when you want a function that does nothing.

Types

type Agent

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

Agent represents the execution of a sequence of Services. For any sequence, there will be two agents in play: one for the startup sequence, and another for the shutdown sequence. The only difference between these two are the order in which the sequence is executed. Each Agent keeps track of its progress and handles execution of sequence Services.

func (*Agent) Down

func (a *Agent) Down(ctx context.Context, progressFn func(Progress)) error

Down runs the shutdown sequence. Down returns an error if the Agent's current state doesn't allow the sequence to start.

func (*Agent) ServiceCount

func (a *Agent) ServiceCount() uint16

ServiceCount returns the number of services currently registered with the Agent.

func (*Agent) String

func (a *Agent) String() string

String returns a string representation of the registered Services ordered by priority. Service names are wrapped in parentheses, and separated by a colon when it might run concurrently with one or more other services, and a right-arrow when it will run before another service. Services that have the same priority are sorted alphabetically for reasons of reproducibility.

func (*Agent) Up

func (a *Agent) Up(ctx context.Context, progressFn func(Progress)) error

Up runs the startup sequence. Up returns an error if the Agent's current state doesn't allow the sequence to start.

type CalleeError

type CalleeError string

CalleeError indicates that a "callee" was called after another one already has been called: Wait/Progress.

func (CalleeError) Error

func (c CalleeError) Error() string

Error returns the error message for a CalleeError.

type CyclicReferenceError

type CyclicReferenceError string

CyclicReferenceError indicates that two Services are referencing each other.

func (CyclicReferenceError) Error

func (c CyclicReferenceError) Error() string

Error returns the error message for a CyclicReferenceError.

type EmptySequenceError

type EmptySequenceError string

EmptySequenceError indicates an empty boot sequence.

func (EmptySequenceError) Error

func (e EmptySequenceError) Error() string

Error returns the error message for a EmptySequenceError.

type Func

type Func func() error

Func is the type used for any function that can be executed as a service in a boot sequence. Any function that you wish to register and execute as a service must satisfy this type.

type InvalidStateError

type InvalidStateError string

InvalidStateError indicates that the Agent was unable to run the boot sequence, either because it is already running, or because it has already completed.

func (InvalidStateError) Error

func (i InvalidStateError) Error() string

Error returns the error message for a InvalidStateError.

type Manager

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

Manager provides registration and storage of boot sequence Services. Manager can instantiate an Agent, which is responsible for running the actual startup and shutdown sequences.

func New

func New(name string) *Manager

New returns a new and uninitialised boot sequence Manager.

func (*Manager) Agent

func (m *Manager) Agent() (agent *Agent, err error)

Agent orders the registered services by priority and returns an Agent for controlling the startup and shutdown sequences. Agent returns an error if any of the registered Services refer to other Services that are not registered.

func (*Manager) Register

func (m *Manager) Register(name string, up, down Func) *Service

Register registers a single named Service to the boot sequence, with the given "up" and "down" functions. If a Service with the given name already exists, the provided up- and down functions replace those already registered. Add returns a pointer to the added Service, that you can call After() on, in order to influence order of execution.

func (*Manager) ServiceCount

func (m *Manager) ServiceCount() uint16

ServiceCount returns the number of services currently registered with the Manager.

func (*Manager) ServiceNames

func (m *Manager) ServiceNames() []string

ServiceNames returns the name of each registered service, in no particular order.

func (*Manager) Validate

func (m *Manager) Validate() error

Validate cycles through each registered service and checks if they refer to other service names that don't exist, or if they refer to themselves. Validate returns an error if this is the case, or nil otherwise.

type NilFuncError

type NilFuncError string

NilFuncError indicates that a Service contains one or more nil Funcs.

func (NilFuncError) Error

func (n NilFuncError) Error() string

Error returns the error message for a CalleeError.

type Progress

type Progress struct {
	Service string
	Err     error
}

Progress is the boot sequence feedback medium. Progress is communicated on channels returned by methods Up() and Down() and provides feedback on the current progress of the boot sequence. This includes the name of the Service that was last executed, along with an optional error if the Service Func failed. Err will be nil on success. Progress satisfies the error interface.

func (Progress) Error

func (p Progress) Error() string

Error returns the error message for the receiver. Error returns an empty string if there is no error.

type SelfReferenceError

type SelfReferenceError string

SelfReferenceError indicates a service that references itself in an After method call.

func (SelfReferenceError) Error

func (s SelfReferenceError) Error() string

Error returns the error message for a SelfReferenceError.

type Service

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

Service contains the functions required in order to execute a single Service Func in a sequence, the up() and down() functions, respectively.

func (*Service) After

func (s *Service) After(name string)

After sets the receiver Service to be executed after the one defined by the given name.

type UnregisteredServiceError

type UnregisteredServiceError string

UnregisteredServiceError indicates a service has not been registered with the boot sequence manager.

func (UnregisteredServiceError) Error

func (u UnregisteredServiceError) Error() string

Error returns the error message for a UnregisteredServiceError.

Directories

Path Synopsis
Package seqmgr Created by Teocci.
Package seqmgr Created by Teocci.

Jump to

Keyboard shortcuts

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