sen

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 10 Imported by: 4

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"

	"github.com/bongnv/sen/pkg/sen"
)

func main() {
	app := sen.New()

	runHook := sen.OnRun(func(_ context.Context) error {
		fmt.Println("OnRun is executed")
		return nil
	})

	shutdownHook := sen.OnShutdown(func(_ context.Context) error {
		fmt.Println("OnShutdown is executed")
		return nil
	})

	postRunHook := sen.PostRun(func(_ context.Context) error {
		fmt.Println("PostRun is executed")
		return nil
	})

	_ = app.With(runHook, shutdownHook, postRunHook)
	err := app.Run(context.Background())
	if err != nil {
		fmt.Println(err)
	}

}
Output:

OnRun is executed
OnShutdown is executed
PostRun is executed

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrComponentNotRegistered = errors.New("sen: the component is not registered")

ErrComponentNotRegistered is returned when the expected component isn't registered so it couldn't be found by name.

Functions

This section is empty.

Types

type Application

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

Application represents an application. To construct an application from plugins use: app.With. For example:

app := sen.New()
if err := app.With(plugin1, plugin2); err != nil {
	handleError(err)
}

func New

func New() *Application

New creates a new Application.

func (*Application) Run

func (app *Application) Run(ctx context.Context) error

Run runs the application by executing all run hooks in parallel. After that it will execute shutdown hooks and afterRun hooks.

func (*Application) Shutdown

func (app *Application) Shutdown(ctx context.Context) error

Shutdown runs the application by executing all the registered OnShutdown hooks.

func (*Application) With

func (app *Application) With(plugins ...Plugin) error

With applies a plugin or multiple plugins. While applying a plugin, the plugin will be injected with dependencies and Initialize method will be called.

type Hook

type Hook func(ctx context.Context) error

Hook represents a hook to add custom logic in the application life cycle.

type Hub added in v0.2.0

type Hub interface {
	// Register injects dependencies into a component and register the component into the depdenency container
	// for the next injection.
	Register(name string, component interface{}) error

	// Retrieve retrieves a component via name. It returns an error if there is any.
	Retrieve(name string) (interface{}, error)

	// Inject injects dependencies into a component.
	Inject(component interface{}) error
}

Hub is a container of components. It allows registering new components by names as well as injecting dependencies into a component via tags or types.

type Lifecycle

type Lifecycle interface {
	OnRun(h Hook)
	OnShutdown(h Hook)
	PostRun(h Hook)
	Run(ctx context.Context) error
	Shutdown(ctx context.Context) error
}

Lifecycle manages the lifecycle of an application. An application starts with .Run(ctx) and will be stopped when .Shutdown(ctx) is called. It also allows to hook into the application via OnRun, OnShutdown and PostRun.

type Plugin

type Plugin interface {
	// Initialize initialises the plugin and installs the plugin into the application.
	Initialize() error
}

Plugin represents a plugin in a sen application. It enhances the application by providing one or multiple functionalities. A plugin can have from zero to many dependencies and they can be injected by declaring "inject" tag.

func Bundle added in v0.3.0

func Bundle(plugins ...Plugin) Plugin

Bundle is a collection of plugins.

func Component

func Component(name string, c any) Plugin

Component creates a new component plugin. The simple plugin adds a component into the application under the given name.

func GracefulShutdown

func GracefulShutdown() Plugin

GracefulShutdown creates a new GracefulShutdownPlugin. The plugin will allow the application calling its Shutdown when an interrupt signal (Ctrl+C) is received.

func OnRun

func OnRun(hooks ...Hook) Plugin

OnRun adds multiple hooks to run with the application.

func OnShutdown

func OnShutdown(hooks ...Hook) Plugin

OnShutdown adds multiple hooks to run with the application.

func PostRun

func PostRun(hooks ...Hook) Plugin

PostRun adds additional logic after all services stop running and shutdown logic is executed. It's useful for syncing logs, etc.

Jump to

Keyboard shortcuts

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