Inity is a small task manager for bootstrapping Go services and shutting them down in reverse dependency order.
- Starts registered tasks concurrently.
- Cancels the manager context when any task returns an error.
- Shuts tasks down in reverse registration order.
- Supports an optional
Quit()fast path for forced shutdown.
Any type with Start() error and Close() methods can be registered. If the type also implements Quit(), Inity uses it when another task fails and the remaining tasks should stop immediately.
go get github.com/inquisico/go-initypackage main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"github.com/inquisico/go-inity"
)
type server struct{}
func (server) Start(ctx context.Context) error {
return nil
}
func (server) Close() {}
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigs)
manager := inity.New("app", inity.WithSignals(sigs))
manager.Register(server{})
if err := manager.Start(context.Background()); err != nil {
log.Fatal(err)
}
}Start()blocks until every task exits.- If the parent context is canceled or a configured signal arrives, Inity calls
Close()on registered tasks in reverse order. - If a task returns an error, Inity marks shutdown as forced and calls
Quit()on remaining tasks when available. - Tasks should treat their own normal shutdown path as success and return
nilfromStart()afterClose()orQuit()unblocks them.
Example programs live under examples/:
examples/simpleexamples/hierarchicalexamples/zerolog
Run the simple example from the repository root:
make runmake update
make test
make lint