From 85981e75a76a4553e4fc7fc8736a2e48d51557a3 Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Sun, 19 Dec 2021 13:22:44 -0500 Subject: [PATCH] Added shutdown method to container. --- Makefile | 2 +- controller/controller_test.go | 3 +++ routes/routes_test.go | 3 +++ services/container.go | 35 +++++++++++++++++++++++++---------- services/services_test.go | 3 +++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index f76da19..7bf9e44 100644 --- a/Makefile +++ b/Makefile @@ -35,4 +35,4 @@ run: .PHONY: test test: - go test ./... \ No newline at end of file + go test -p 1 ./... \ No newline at end of file diff --git a/controller/controller_test.go b/controller/controller_test.go index eb17c1b..1f79e8a 100644 --- a/controller/controller_test.go +++ b/controller/controller_test.go @@ -35,6 +35,9 @@ func TestMain(m *testing.M) { // Run tests exitVal := m.Run() + if err := c.Shutdown(); err != nil { + panic(err) + } os.Exit(exitVal) } diff --git a/routes/routes_test.go b/routes/routes_test.go index 5405fcc..03631b5 100644 --- a/routes/routes_test.go +++ b/routes/routes_test.go @@ -33,6 +33,9 @@ func TestMain(m *testing.M) { // Run tests exitVal := m.Run() srv.Close() + if err := c.Shutdown(); err != nil { + panic(err) + } os.Exit(exitVal) } diff --git a/services/container.go b/services/container.go index 0ff87c0..3f955a5 100644 --- a/services/container.go +++ b/services/container.go @@ -21,13 +21,14 @@ import ( ) type Container struct { - Web *echo.Echo - Config *config.Config - Cache *cache.Cache - Database *sql.DB - ORM *ent.Client - Mail *mail.Client - Auth *AuthClient + Web *echo.Echo + Config *config.Config + Cache *cache.Cache + cacheClient *redis.Client + Database *sql.DB + ORM *ent.Client + Mail *mail.Client + Auth *AuthClient } func NewContainer() *Container { @@ -42,6 +43,20 @@ func NewContainer() *Container { return c } +func (c *Container) Shutdown() error { + if err := c.cacheClient.Close(); err != nil { + return err + } + if err := c.ORM.Close(); err != nil { + return err + } + if err := c.Database.Close(); err != nil { + return err + } + + return nil +} + func (c *Container) initConfig() { cfg, err := config.GetConfig() if err != nil { @@ -63,14 +78,14 @@ func (c *Container) initWeb() { } func (c *Container) initCache() { - cacheClient := redis.NewClient(&redis.Options{ + c.cacheClient = redis.NewClient(&redis.Options{ Addr: fmt.Sprintf("%s:%d", c.Config.Cache.Hostname, c.Config.Cache.Port), Password: c.Config.Cache.Password, }) - if _, err := cacheClient.Ping(context.Background()).Result(); err != nil { + if _, err := c.cacheClient.Ping(context.Background()).Result(); err != nil { panic(fmt.Sprintf("failed to connect to cache server: %v", err)) } - cacheStore := store.NewRedis(cacheClient, nil) + cacheStore := store.NewRedis(c.cacheClient, nil) c.Cache = cache.New(cacheStore) } diff --git a/services/services_test.go b/services/services_test.go index b048d06..98b08aa 100644 --- a/services/services_test.go +++ b/services/services_test.go @@ -48,5 +48,8 @@ func TestMain(m *testing.M) { // Run tests exitVal := m.Run() + if err := c.Shutdown(); err != nil { + panic(err) + } os.Exit(exitVal) }