The first lifecycle stage a service goes through is injection. Here a service can declare any other services it has a dependency on or any command line flags it requires by use of field tags within its structure.
For example, here we have a simple service showing the possible combinations available by the Injection lifecycle:
1type Example struct {
2 config *conf.Config `kernel:"inject"`
3 worker task.Queue `kernel:"worker"`
4 _ *PostCSS `kernel:"inject"`
5 server *bool `kernel:"flag,s,Run hugo in server mode"`
6}
Service dependencies
A service can declare a dependency against another service by including th
e kernel:"inject"
tag against a field of the type of the service.
In the above example we have the field config which has the type *conf.Config
.
As that field has the tag then the kernel will inject that service's pointer into that field.
When the service starts, it will be started before this one.
Unreferenced Service dependencies
An unreferenced dependency is a dependency on another service, but we won't call that service directly. An example of this is a webserver - we need the service to be running whilst this service is running, but we won't call the service directly, e.g. we might make an HTTP connection to it but not via code.
In the above example, the PostCSS service is one of these. We want that service to be deployed however we won't
be using it directly so the field name is _
.
This ensures that service is deployed but not injected into the struct.
Worker Task Queue
The worker task queue is a kernel service allowing for tasks to be queued and prioritised for execution.
It's injected using the kernel:"worker"
tag.
Command line flags
Command line flags can be injected using the kernel:"flag"
tag.
This is a composite tag consisting of up to 4 comma separated fields.
The first field is always flag
.
To inject a flag, simply create a field with its type being one of: *bool, *string, *int, *int64 or *float64. Any other type will cause the service to fail.
The tag takes the following format:
flag,{name,{description,{default}}}
- name
-
The name of this flag. It will have '-' prepended to it when used on the command line.
If absent or "" then this will take the name of the field being tagged. - description
-
The description for this flag, used when listing the available flags on the command line.
If absent or "" then this will take the value used for the flag name. - default
-
The default value for this flag.
If absent or "" then this will take a suitable default for the type: "" for string, false for boolean, 0 for integers or 0.0 for floats.