This the multi-page printable view of this section.Click here to print.

Return to the regular view of this page.

PostInit

Table of Contents

After the Init stage the kernel enters PostInit. Here the command flags have been parsed so any reference to them are now valid. Any Service's which implements the PostInitialisableService service will have their PostInit() function called so that they can check they are in a valid state.

1type PostInitialisableService interface {
2    PostInit() error
3}

This stage is provided to allow services to stop the kernel if they are in an invalid state before any Service has been started.

For example, if the service created command line flags then it can check that they are valid.

A service must never call a function in another service from inside the PostInit() method, nor create any external resources like go routines or open files.

Doing so could call a service which has not yet been initialised and leave resources open if the kernel exits due to an error.

Example: Checking command line flags are valid failing if it's not been set

 1type Config struct {
 2    configFile *string
 3}
 4
 5func (a *Config) Name() string {
 6	return "Config"
 7}
 8
 9func (a *Config) Init(k *kernel.Kernel) error {
10	a.configFile = flag.String("c", "", "The config file to use")
11
12	return nil
13}
14
15func (a *Config) PostInit() error {
16	if *a.configFile == "" {
17        return fmt.Errorf("No default config defined, provide with -c")
18	}
19
20	return nil
21}