app

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2021 License: MIT Imports: 7 Imported by: 0

README

app

build Go Reference Go Report Card GitHub go.mod Go version codecov

app is a Go package that provides useful application lifecycle management functionality.

Each registered app.Runners will be gracefully shutdown upon system signals such as SIGINT, SIGTERM or upon an error from one of the components.

Installation

go get -u github.com/dacturne/app

Usage

func main() {
    component := NewComponent()
    otherComponent := NewOtherComponent()

    app.New().Add(component).Add(otherComponent).Start()
}

For more examples check the examples/ directory.

Released under the MIT License.

Documentation

Overview

Package app implements functionality useful for bootstraping an application.

Manager handles the lifecycle of registered components and graceful shutdowns in case of errors or system signals.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseFunc

type CloseFunc func() error

CloseFun is use to close a runner component.

type Manager

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

Manager is the core app component tasked with coordination of the registered runners and their lifecycle, as well as responding to system signals.

For example, in case of a SIGINT or SIGTERM the underlying context will be canceled and runner.Close() methods will be invoked.

func New

func New() *Manager

New returns a new app Manager.

func (*Manager) Add

func (m *Manager) Add(r Runner) *Manager

Add a runner to the manager.

Returns the manager instance for method chaining.

func (*Manager) Close

func (m *Manager) Close() (errors error)

Close attempts a graceful shutdown on all of the runners.

func (*Manager) Start

func (m *Manager) Start()

Start the application manager.

Starts all of the registered runners with the underlying context, that gets canceled upon receiving SIGINT, SIGTERM signals.

type RunFunc

type RunFunc func(ctx context.Context) error

RunFunc is used to run a runner component.

type Runner

type Runner interface {
	// Run the component.
	// Runner might perform a blocking call.
	//
	// Upon receiving a context.Done() no further "units work" should be
	// accepted by the runner.
	//
	// If the runner wants to signal an issue to the manager, then an error
	// should be returned.
	Run(context.Context) error

	// Close is used to shutdown the runner in a graceful manner.
	Close() error
}

Runner is an app component, who's lifecycle will be managed by the Manager.

func NewRunner

func NewRunner(fn RunFunc, close CloseFunc) Runner

NewRunner creates a new runner from a run and close function.

Useful when a struct doesn't implement the Runner interface.

func NopCloseRunner

func NopCloseRunner(fn RunFunc) Runner

NopCloseRunner creates a runner with a noop closer.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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