mrun

package module
v0.0.0-...-ba57e60 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2019 License: MIT Imports: 8 Imported by: 0

README

MRun

GoDoc Go Report Card

Simplest tool to run and properly shutdown multiple components in the same process.

When your application relies on support components like an event emitter or a trace collector, those components should be shutdown properly to let them finish their work.

Conversely, an action should be taken when a support component dies prematurely. In such a case, MRun will shutdown all other components.

Example:

import (
    "github.com/pior/mrun"
)

Define your runnables:

type Server struct {}

func (s *Server) Run(ctx context.Context) error {
    // serve stuff
    <-ctx.Done()
    return nil
}

type EventEmitter struct {}

func (s *EventEmitter) Run(ctx context.Context) error {
    // emit stuff
    <-ctx.Done()
    // FLUSH STUFF !!
    return nil
}

Start your application:

func main() {
    mrun.RunAndExit(&Server{}, &EventEmitter{})
}

Which is equivalent to:

func main() {
    mr := New(&Server{}, &EventEmitter{})

    mr.SetSignalsHandler()

    ctx := context.Background()
    err := mr.Run(ctx)

    if err != nil {
        mr.Logger.Infof("Error: %s", err)
        os.Exit(1)
    }

    os.Exit(0)
}

License

The MIT License (MIT)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunAndExit

func RunAndExit(runnables ...Runnable)

RunAndExit runs the set of runnables with MRun, listens to SIGTERM/SIGINT and terminates the process with a non-zero code when a runnable fails.

Types

type Logger

type Logger interface {
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
}

Logger is the contract our logger

type MRun

type MRun struct {
	Logger Logger
	// contains filtered or unexported fields
}

MRun orchestrates the execution of a set of Runnable.

func New

func New(runnables ...Runnable) *MRun

New returns a MRun instance with a set of runnables.

func (*MRun) Run

func (m *MRun) Run(ctx context.Context) error

Run runs all the runnables and manages the graceful shutdown.

func (*MRun) SetSignalsHandler

func (m *MRun) SetSignalsHandler(signals ...os.Signal)

SetSignalsHandler installs the signal handler that trigger a shutdown. The signals defaults to SIGINT and SIGTERM.

type Runnable

type Runnable interface {
	Run(context.Context) error
}

Runnable can be any long running components that respect context cancellation and returns an error.

Directories

Path Synopsis
cmd
test command

Jump to

Keyboard shortcuts

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