Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 1.4 KB

README.md

File metadata and controls

70 lines (52 loc) · 1.4 KB

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)