app

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 10 Imported by: 0

README

app

Light-weight utilities for managing app life-cycle events

Documentation

Overview

Package app provides light-weight utilities around app life cycle events.

Index

Constants

View Source
const (
	DefaultShutdownTimeout = time.Second * 15
)

Variables

View Source
var ProviderSet = wire.NewSet(New, wire.Value(([]AppOptFunc)(nil)))

Functions

This section is empty.

Types

type App

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

At its core, and app is a place to register work to be done during the life-cycle of the app. And, a central place to signal when to begin shutdown. The zero value of an App is invalid; use New to get a new instance of an App.

Once an app is initialized by calling New, it can be in one of three states.

Running - the normal running state. In this state, it is valid to queue more work by calling App.Go and to queue more shutdown jobs by calling App.OnShutdown. An app will transition out of the running state once any of its tasks, queued via App.Go, return a non-nil error, or if its parent context is cancelled.

Stopping - a limited period of time after shutdown has been signaled to allow for clean-up and finalization tasks to run. In this state, it is valid to queue more work by calling App.OnShutdown. It is invalid to call App.Go in this phase.

Terminated - after all clean-up tasks have complete, or the shutdown grace period has elapsed, the app is in its final terminal state. New work can no longer be queued.

func New

func New(parent context.Context, opts ...AppOptFunc) *App

New initializes an new app for use. By default, it will have the DefaultShutdownTimeout, it will use slog.Default to log, and it will be set to keep-alive. These options can be modified by passing the appropriate opts.

func (*App) Go

func (a *App) Go(f func(context.Context) error) error

Go schedules a task (f) to run as apart of this app. The task will be passed a context that, once canceled, indicates that the app is entering is Stopping phase and this task should exit. If it does not, it will be forcibly terminated after the stopping grace period has passed.

func (*App) OnShutdown

func (a *App) OnShutdown(f func() error) error

OnShutdown queues a task to run once this app begins entering its shutdown phase.

func (*App) Wait

func (a *App) Wait() error

Wait blocks until the shutdown phase of this app has completed (gracefully or forcibly), then returns the any error that occurred from tasks queued by calling App.Go during the Running phase.

type AppOptFunc

type AppOptFunc func(*App)

AppOptFunc is an option's function that can be used to override default values of an app during initialization by New.

func WithKeepAlive

func WithKeepAlive(keepAlive bool) AppOptFunc

WithKeepAlive sets the app's keep-alive value.

func WithLogger

func WithLogger(logger *slog.Logger) AppOptFunc

WithLogger sets the app's logger.

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) AppOptFunc

WithShutdownTimeout sets the shutdown grace period to timeout.

Jump to

Keyboard shortcuts

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