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.
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)
}
The MIT License (MIT)