m: rename Start to StartWaitStop, Start is a different function which only populates config and runs start hooks

This commit is contained in:
Brian Picciano 2019-02-24 17:23:38 -05:00
parent 3aa69c5878
commit dd7bc43cf8
2 changed files with 18 additions and 12 deletions

View File

@ -106,5 +106,5 @@ func main() {
}) })
ctx, _ = mhttp.WithListeningServer(ctx, authHandler) ctx, _ = mhttp.WithListeningServer(ctx, authHandler)
m.Run(ctx) m.StartWaitStop(ctx)
} }

28
m/m.go
View File

@ -54,27 +54,33 @@ func ServiceContext() context.Context {
return ctx return ctx
} }
// Run performs the work of populating configuration parameters, triggering the // Start performs the work of populating configuration parameters and triggering
// start event, waiting for an interrupt, and then triggering the stop event. // the start event. It will return once the Start event has completed running.
// Run will block until the stop event is done. If any errors are encountered a func Start(ctx context.Context) {
// fatal is thrown. mlog.Info("populating configuration", ctx)
func Run(ctx context.Context) {
log := mlog.From(ctx)
if err := mcfg.Populate(ctx, CfgSource()); err != nil { if err := mcfg.Populate(ctx, CfgSource()); err != nil {
log.Fatal("error populating configuration", ctx, merr.Context(err)) mlog.Fatal("error populating configuration", ctx, merr.Context(err))
} else if err := mrun.Start(ctx); err != nil { } else if err := mrun.Start(ctx); err != nil {
log.Fatal("error triggering start event", ctx, merr.Context(err)) mlog.Fatal("error triggering start event", ctx, merr.Context(err))
} }
}
// StartWaitStop performs the work of populating configuration parameters,
// triggering the start event, waiting for an interrupt, and then triggering the
// stop event. Run will block until the stop event is done. If any errors are
// encountered a fatal is thrown.
func StartWaitStop(ctx context.Context) {
Start(ctx)
{ {
ch := make(chan os.Signal, 1) ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt) signal.Notify(ch, os.Interrupt)
s := <-ch s := <-ch
log.Info("signal received, stopping", mctx.Annotate(ctx, "signal", s)) mlog.Info("signal received, stopping", mctx.Annotate(ctx, "signal", s))
} }
if err := mrun.Stop(ctx); err != nil { if err := mrun.Stop(ctx); err != nil {
log.Fatal("error triggering stop event", ctx, merr.Context(err)) mlog.Fatal("error triggering stop event", ctx, merr.Context(err))
} }
log.Info("exiting process", ctx) mlog.Info("exiting process", ctx)
} }