2018-05-28 08:00:31 +00:00
|
|
|
// Package m is the glue which holds all the other packages in this project
|
|
|
|
// together. While other packages in this project are intended to be able to be
|
|
|
|
// used separately and largely independently, this package combines them in ways
|
|
|
|
// which I specifically like.
|
|
|
|
package m
|
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2019-01-25 03:04:58 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2018-05-28 08:00:31 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
2019-01-25 03:04:58 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/merr"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
2018-05-28 08:00:31 +00:00
|
|
|
)
|
|
|
|
|
2018-08-14 01:09:20 +00:00
|
|
|
// CfgSource returns an mcfg.Source which takes in configuration info from the
|
|
|
|
// environment and from the CLI.
|
|
|
|
func CfgSource() mcfg.Source {
|
|
|
|
return mcfg.Sources{
|
|
|
|
mcfg.SourceEnv{},
|
|
|
|
mcfg.SourceCLI{},
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 03:04:58 +00:00
|
|
|
|
2019-02-03 00:27:28 +00:00
|
|
|
// NewServiceCtx returns a Context which should be used as the root Context when
|
|
|
|
// creating long running services, such as an RPC service or database.
|
|
|
|
//
|
|
|
|
// The returned Context will automatically handle setting up global
|
|
|
|
// configuration parameters like "log-level", as well as an http endpoint where
|
|
|
|
// debug information about the running process can be accessed.
|
|
|
|
//
|
|
|
|
// TODO set up the debug endpoint.
|
2019-02-05 20:18:17 +00:00
|
|
|
func NewServiceCtx() context.Context {
|
|
|
|
ctx := context.Background()
|
2019-02-03 00:27:28 +00:00
|
|
|
|
|
|
|
// set up log level handling
|
2019-02-05 20:18:17 +00:00
|
|
|
logger := mlog.NewLogger()
|
|
|
|
ctx = mlog.Set(ctx, logger)
|
|
|
|
ctx, logLevelStr := mcfg.String(ctx, "log-level", "info", "Maximum log level which will be printed.")
|
|
|
|
ctx = mrun.OnStart(ctx, func(context.Context) error {
|
2019-02-03 00:27:28 +00:00
|
|
|
logLevel := mlog.LevelFromString(*logLevelStr)
|
|
|
|
if logLevel == nil {
|
|
|
|
return merr.New("invalid log level", "log-level", *logLevelStr)
|
|
|
|
}
|
2019-02-05 20:18:17 +00:00
|
|
|
logger.SetMaxLevel(logLevel)
|
2019-02-03 00:27:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
2019-01-25 03:04:58 +00:00
|
|
|
|
|
|
|
// Run 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.
|
2019-02-05 20:18:17 +00:00
|
|
|
func Run(ctx context.Context) {
|
2019-01-25 03:04:58 +00:00
|
|
|
log := mlog.From(ctx)
|
|
|
|
if err := mcfg.Populate(ctx, CfgSource()); err != nil {
|
2019-02-05 20:18:17 +00:00
|
|
|
log.Fatal(ctx, "error populating configuration", merr.KV(err))
|
2019-01-25 03:04:58 +00:00
|
|
|
} else if err := mrun.Start(ctx); err != nil {
|
2019-02-05 20:18:17 +00:00
|
|
|
log.Fatal(ctx, "error triggering start event", merr.KV(err))
|
2019-01-25 03:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
s := <-ch
|
2019-02-05 20:18:17 +00:00
|
|
|
log.Info(ctx, "signal received, stopping", mlog.KV{"signal": s})
|
2019-01-25 03:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := mrun.Stop(ctx); err != nil {
|
2019-02-05 20:18:17 +00:00
|
|
|
log.Fatal(ctx, "error triggering stop event", merr.KV(err))
|
2019-01-25 03:04:58 +00:00
|
|
|
}
|
2019-02-05 20:18:17 +00:00
|
|
|
log.Info(ctx, "exiting process")
|
2019-01-25 03:04:58 +00:00
|
|
|
}
|