Documentation ¶
Overview ¶
package xrun provides utilities around starting multiple components which are long running components, example: an HTTP server or a background worker
package main import ( "net/http" "os" "github.com/ajatprabha/xrun" ) func main() { m := xrun.NewRunnableManager() server := http.Server{ Addr: ":9090", } _ = m.Add(xrun.HttpServer(&server, xrun.NoTimeout)) if err := m.Start(xrun.SetupSignalHandler()); err != nil { os.Exit(1) } }
Index ¶
Constants ¶
const NoTimeout = time.Duration(0)
Variables ¶
This section is empty.
Functions ¶
func SetupSignalHandler ¶
SetupSignalHandler registers for SIGTERM and SIGINT. A stop channel is returned which is closed on one of these signals. If a second signal is caught, the program is terminated with exit code 1.
Types ¶
type Option ¶
type Option func(*manager)
Option changes behaviour of RunnableManager
func WithGracefulShutdownTimeout ¶
WithGracefulShutdownTimeout allows max timeout after which RunnableManager exits
type Runnable ¶
type Runnable interface { // Start starts running the component. The component will stop running // when the context is closed. Start blocks until the context is closed or // an error occurs. Start(context.Context) error }
Runnable allows a component to be started. It's very important that Start blocks until it's done running.
type RunnableFunc ¶
RunnableFunc implements Runnable using a function. It's very important that the given function block until it's done running.
func HttpServer ¶
func HttpServer(s *http.Server, shutdownTimeout time.Duration) RunnableFunc
HttpServer adds an http.Server to RunnableManager and waits for shutdownTimeout while shutting it down
type RunnableManager ¶
type RunnableManager interface { Runnable // Add will include the component, and the component will be // started when Start is called. Add(Runnable) error }
RunnableManager helps to start multiple components and waits for them to complete
func NewRunnableManager ¶
func NewRunnableManager(opts ...Option) RunnableManager