Documentation ¶
Overview ¶
Package life manages life cycle of application. An application has follow life state:
Config/init. If a package need initialization, provides Init() function. App main() function call these Init() functions in proper order. TODO: support united config framework, get config settings from config files and command arguments.
Starting. App call life.Start() function indicate going to starting state. Each package register a function by life.OnStart(), they will called in register order.
After life.Start() complete, going to running state.
Stopping. Calling life.Shutdown() function going to shutdown state. Each package can register a function by life.OnShutdown(), they will called in reversed order.
Use life package, app do not need to remember start every package in correct order. Keep calling of Start() inside the package itself, clean and elegant. Shutdown state enforces all package and go routines exit properly, without unpredictable state and corrupting data.
Index ¶
- Constants
- func Abort()
- func EnsureState(exp StateT, msg string)
- func EnsureStatef(exp StateT, format string, a ...interface{})
- func Exit(n int)
- func Register(name string, onStart, onShutdown Callback, depends ...string)
- func RegisterHook(name string, order int, typ hookType, fn HookFunc)
- func Shutdown()
- func Start()
- func WaitToEnd()
- type Callback
- type HookFunc
- type StateT
Constants ¶
const ( // BeforeStarting hooks called before entering staring state BeforeStarting hookType = iota // BeforeRunning hooks called before entering running state BeforeRunning // BeforeShutingdown hooks called before entering shutingdown state BeforeShutingdown // OnAbort hooks called on abnormal error before exit. Abort hooks run in any state, // even before your package initialized, check your hooks to work on any states, // do not assume opened file, socket, channel, etc. OnAbort )
Variables ¶
This section is empty.
Functions ¶
func Abort ¶
func Abort()
Abort calling Abort hooks, and then exit. It is useful when fatal error occurred outside life package, ensure abort hooks done its job (such as: spork/errrpt, async log).
func EnsureState ¶
EnsureState ensure current state is expected, panic with specific message if failed.
func EnsureStatef ¶
EnsureStatef ensure current state is expected, panic with formatted message if failed.
func Exit ¶
func Exit(n int)
Exit the problem with n as exit code after executing all OnAbort hooks. Like Abort() but can set exit code.
func Register ¶
Register a package, optionally includes depended packages. If not provides depended package, it will run as registered order. Depends need not to be exist, it will check and sort in Start().
func RegisterHook ¶
RegisterHook register a function that executed when typ hook event occurred. Name is used in log only. If multiple function hook to one hookType, they executed by order, smaller execute first, If two hooks have the same order, they will execute in any order.
func Shutdown ¶
func Shutdown()
Shutdown put state to shutdown, Run all registered OnShutdown() function in reserved order.
Types ¶
type HookFunc ¶
type HookFunc func()
HookFunc called when a hook event occurred. See hookType constants.
type StateT ¶
type StateT int32
StateT indicate current application life state.
const ( // Initing is the default state of life, in this state, all packages doing // init stuff using init() func. Initing StateT = iota // Starting state runs all package's start functions, they are running in // dependent order Starting // Running is the normal running state, after all packages started. Running // Shutingdown is the state to do the packages shutdown work. Shutingdown // Halt is a temporary state after all package shutdown and application not exit, // it is mainly used inside life package, normally outside package won't got a change // to saw Halt state. Halt )