Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Server ¶
Server runs the top-level task of your program similar To Tool.
The difference is in signal handling: if the top-level task exits with (possibly wrapped) context.Canceled while handling the signal, the program exits with code 0.
Note that any other error returned during signal handling is still considered an error and makes Server exit with code 1.
func Tool ¶
Tool runs the top-level task of your program, watching for signals.
The context passed to the task will contain a logger and an ID generator.
If an interruption or termination signal arrives, the context passed to the task will be closed.
Tool does not return. It exits with code 0 if the task returns nil, and with code 1 if the task returns an error.
Any defer handlers installed before calling Main are ignored. For this reason, it is recommended that most or all your main code is inside the task.
Simple example:
func main() { pflag.Parse() srv := service.New(...) run.Tool(srv.Run) }
Medium-complexity example:
func main() { pflag.Parse() run.Tool(func(ctx context.Context) error { if err := Step1(ctx); err != nil { return err } if err := Step2(ctx); err != nil { return err } return nil }) }
Complex example:
func main() { pflag.Parse() run.Tool(func(ctx context.Context) error { return parallel.Run(func(ctx context.Context, spawn SpawnFn) { s1, err := service1.New(...) if err != nil { return err } s2, err := service2.New(...) if err != nil { return err } if err := s1.HeavyInit(ctx); err != nil { return err } spawn("service1", parallel.Fail, s1.Run) spawn("serivce2", parallel.Fail, s2.Run) return nil }) }) }
Types ¶
type WithExitCode ¶
type WithExitCode interface {
ExitCode() int
}
WithExitCode is an optional interface that can be implemented by an error.
When a (possibly wrapped) error implementing WithExitCode reaches the top level, the value returned by the ExitCode method becomes the exit code of the process. The default exit code for other errors is 1.