2019-06-17 21:56:14 +00:00
|
|
|
// Package m implements functionality specific to how I like my programs to
|
|
|
|
// work. It acts as glue between many of the other packages in this framework,
|
|
|
|
// putting them together in the way I find most useful.
|
2018-05-28 08:00:31 +00:00
|
|
|
package m
|
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2019-01-25 03:04:58 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-06-17 21:56:14 +00:00
|
|
|
"time"
|
2019-01-25 03:04:58 +00:00
|
|
|
|
2018-05-28 08:00:31 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
2019-06-17 21:56:14 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
2019-02-09 19:08:30 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
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
|
|
|
)
|
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
type cmpKey int
|
2019-04-06 20:30:55 +00:00
|
|
|
|
|
|
|
const (
|
2019-06-17 21:56:14 +00:00
|
|
|
cmpKeyCfgSrc cmpKey = iota
|
|
|
|
cmpKeyInfoLog
|
2019-04-06 20:30:55 +00:00
|
|
|
)
|
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
func debugLog(cmp *mcmp.Component, msg string, ctxs ...context.Context) {
|
|
|
|
level := mlog.DebugLevel
|
2019-04-06 20:30:55 +00:00
|
|
|
if len(ctxs) > 0 {
|
2019-06-17 21:56:14 +00:00
|
|
|
if ok, _ := ctxs[0].Value(cmpKeyInfoLog).(bool); ok {
|
|
|
|
level = mlog.InfoLevel
|
2019-04-06 20:30:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 21:56:14 +00:00
|
|
|
|
|
|
|
mlog.From(cmp).Log(mlog.Message{
|
|
|
|
Level: level,
|
|
|
|
Description: msg,
|
|
|
|
Contexts: ctxs,
|
|
|
|
})
|
2019-04-06 20:30:55 +00:00
|
|
|
}
|
2019-01-25 03:04:58 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
// RootComponent returns a Component which should be used as the root Component
|
|
|
|
// when implementing most programs.
|
2019-02-03 00:27:28 +00:00
|
|
|
//
|
2019-06-17 21:56:14 +00:00
|
|
|
// The returned Component will automatically handle setting up global
|
|
|
|
// configuration parameters like "log-level", as well as parsing those
|
|
|
|
// and all other parameters when the Init even is triggered on it.
|
|
|
|
func RootComponent() *mcmp.Component {
|
|
|
|
cmp := new(mcmp.Component)
|
2019-02-03 00:27:28 +00:00
|
|
|
|
2019-04-03 03:35:45 +00:00
|
|
|
// embed confuration source which should be used into the context.
|
2019-06-17 21:56:14 +00:00
|
|
|
cmp.SetValue(cmpKeyCfgSrc, mcfg.Source(new(mcfg.SourceCLI)))
|
2019-04-03 03:35:45 +00:00
|
|
|
|
2019-02-03 00:27:28 +00:00
|
|
|
// set up log level handling
|
2019-02-05 20:18:17 +00:00
|
|
|
logger := mlog.NewLogger()
|
2019-06-17 21:56:14 +00:00
|
|
|
mlog.SetLogger(cmp, logger)
|
|
|
|
|
|
|
|
// set up parameter parsing
|
|
|
|
mrun.InitHook(cmp, func(context.Context) error {
|
|
|
|
src, _ := cmp.Value(cmpKeyCfgSrc).(mcfg.Source)
|
|
|
|
if src == nil {
|
|
|
|
return merr.New("Component not sourced from m package", cmp.Context())
|
|
|
|
} else if err := mcfg.Populate(cmp, src); err != nil {
|
|
|
|
return merr.Wrap(err, cmp.Context())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
logLevelStr := mcfg.String(cmp, "log-level",
|
|
|
|
mcfg.ParamDefault("info"),
|
|
|
|
mcfg.ParamUsage("Maximum log level which will be printed."))
|
|
|
|
mrun.InitHook(cmp, func(context.Context) error {
|
2019-02-03 00:27:28 +00:00
|
|
|
logLevel := mlog.LevelFromString(*logLevelStr)
|
|
|
|
if logLevel == nil {
|
2019-06-17 21:56:14 +00:00
|
|
|
return merr.New("invalid log level", cmp.Context(),
|
|
|
|
mctx.Annotated("log-level", *logLevelStr))
|
2019-02-03 00:27:28 +00:00
|
|
|
}
|
2019-02-05 20:18:17 +00:00
|
|
|
logger.SetMaxLevel(logLevel)
|
2019-02-03 00:27:28 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
return cmp
|
2019-02-03 00:27:28 +00:00
|
|
|
}
|
2019-01-25 03:04:58 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
// RootServiceComponent extends RootComponent so that it better supports long
|
|
|
|
// running processes which are expected to handle requests from outside clients.
|
2019-04-03 03:35:45 +00:00
|
|
|
//
|
|
|
|
// Additional behavior it adds includes setting up an http endpoint where debug
|
|
|
|
// information about the running process can be accessed.
|
2019-06-17 21:56:14 +00:00
|
|
|
func RootServiceComponent() *mcmp.Component {
|
|
|
|
cmp := RootComponent()
|
2019-04-03 03:35:45 +00:00
|
|
|
|
|
|
|
// services expect to use many different configuration sources
|
2019-06-17 21:56:14 +00:00
|
|
|
cmp.SetValue(cmpKeyCfgSrc, mcfg.Source(mcfg.Sources{
|
2019-04-04 14:57:37 +00:00
|
|
|
new(mcfg.SourceEnv),
|
|
|
|
new(mcfg.SourceCLI),
|
2019-04-03 03:35:45 +00:00
|
|
|
}))
|
|
|
|
|
2019-04-06 20:30:55 +00:00
|
|
|
// it's useful to show debug entries (from this package specifically) as
|
|
|
|
// info logs for long-running services.
|
2019-06-17 21:56:14 +00:00
|
|
|
cmp.SetValue(cmpKeyInfoLog, true)
|
2019-04-06 20:30:55 +00:00
|
|
|
|
2019-04-03 03:35:45 +00:00
|
|
|
// TODO set up the debug endpoint.
|
2019-06-17 21:56:14 +00:00
|
|
|
return cmp
|
2019-04-03 03:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
// MustInit will call mrun.Init on the given Component, which must have been
|
|
|
|
// created in this package, and exit the process if mrun.Init does not complete
|
|
|
|
// successfully.
|
|
|
|
func MustInit(cmp *mcmp.Component) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
2019-04-03 03:35:45 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
debugLog(cmp, "initializing")
|
|
|
|
if err := mrun.Init(ctx, cmp); err != nil {
|
|
|
|
mlog.From(cmp).Fatal("initialization failed", merr.Context(err))
|
2019-01-25 03:04:58 +00:00
|
|
|
}
|
2019-06-17 21:56:14 +00:00
|
|
|
debugLog(cmp, "initialization completed successfully")
|
2019-02-24 22:23:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
// MustShutdown is like MustInit, except that it triggers the Shutdown event on
|
|
|
|
// the Component.
|
|
|
|
func MustShutdown(cmp *mcmp.Component) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
debugLog(cmp, "shutting down")
|
|
|
|
if err := mrun.Shutdown(ctx, cmp); err != nil {
|
|
|
|
mlog.From(cmp).Fatal("shutdown failed", merr.Context(err))
|
|
|
|
}
|
|
|
|
debugLog(cmp, "shutting down completed successfully")
|
|
|
|
}
|
2019-01-25 03:04:58 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
// Exec calls MustInit on the given Component, then blocks until an interrupt
|
|
|
|
// signal is received, then calls MustShutdown on the Component, until finally
|
|
|
|
// exiting the process.
|
|
|
|
func Exec(cmp *mcmp.Component) {
|
|
|
|
MustInit(cmp)
|
2019-01-25 03:04:58 +00:00
|
|
|
{
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
s := <-ch
|
2019-06-17 21:56:14 +00:00
|
|
|
debugLog(cmp, "signal received, stopping", mctx.Annotated("signal", s))
|
2019-01-25 03:04:58 +00:00
|
|
|
}
|
2019-06-17 21:56:14 +00:00
|
|
|
MustShutdown(cmp)
|
2019-01-25 03:04:58 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
debugLog(cmp, "exiting process")
|
|
|
|
os.Stdout.Sync()
|
|
|
|
os.Stderr.Sync()
|
|
|
|
os.Exit(0)
|
2019-01-25 03:04:58 +00:00
|
|
|
}
|