Documentation ¶
Index ¶
- Variables
- func Logger(ctx context.Context) *zap.Logger
- func New(ctx context.Context, logger *zap.Logger, rootRunnable Runnable, ...) *supervisor
- func Run(ctx context.Context, name string, runnable Runnable) error
- func RunGroup(ctx context.Context, runnables map[string]Runnable) error
- func Signal(ctx context.Context, signal SignalType)
- type Runnable
- type SignalType
- type SupervisorOpt
Constants ¶
This section is empty.
Variables ¶
var ( // WithPropagatePanic prevents the Supervisor from catching panics in runnables and treating them as failures. // This is useful to enable for testing and local debugging. WithPropagatePanic = func(s *supervisor) { s.propagatePanic = true } )
Functions ¶
func Logger ¶
Logger returns a Zap logger that will be named after the Distinguished Name of a the runnable (ie its place in the supervision tree, dot-separated).
func New ¶
func New(ctx context.Context, logger *zap.Logger, rootRunnable Runnable, opts ...SupervisorOpt) *supervisor
New creates a new supervisor with its root running the given root runnable. The given context can be used to cancel the entire supervision tree.
func RunGroup ¶
RunGroup starts a set of runnables as a group. These runnables will run together, and if any one of them quits unexpectedly, the result will be canceled and restarted. The context here must be an existing Runnable context, and the spawned runnables will run under the node that this context represents.
func Signal ¶
func Signal(ctx context.Context, signal SignalType)
Signal tells the supervisor that the calling runnable has reached a certain state of its lifecycle. All runnables should SignalHealthy when they are ready with set up, running other child runnables and are now 'serving'.
Types ¶
type Runnable ¶
A Runnable is a function that will be run in a goroutine, and supervised throughout its lifetime. It can in turn start more runnables as its children, and those will form part of a supervision tree. The context passed to a runnable is very important and needs to be handled properly. It will be live (non-errored) as long as the runnable should be running, and canceled (ctx.Err() will be non-nil) when the supervisor wants it to exit. This means this context is also perfectly usable for performing any blocking operations.
func Command ¶
Command will create a Runnable that starts a long-running command, whose exit is determined to be a failure.
func GRPCServer ¶
GRPCServer creates a Runnable that serves gRPC requests as longs as it's not canceled. If graceful is set to true, the server will be gracefully stopped instead of plain stopped. This means all pending RPCs will finish, but also requires streaming gRPC handlers to check their context liveliness and exit accordingly. If the server code does not support this, `graceful` should be false and the server will be killed violently instead.
type SignalType ¶
type SignalType int
const ( // The runnable is healthy, done with setup, done with spawning more Runnables, and ready to serve in a loop. // The runnable needs to check the parent context and ensure that if that context is done, the runnable exits. SignalHealthy SignalType = iota // The runnable is done - it does not need to run any loop. This is useful for Runnables that only set up other // child runnables. This runnable will be restarted if a related failure happens somewhere in the supervision tree. SignalDone )
type SupervisorOpt ¶
type SupervisorOpt func(s *supervisor)
SupervisorOpt are runtime configurable options for the supervisor.