Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
add destroyer and remove destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 31, 2019
1 parent e81b7e5 commit 0c29b92
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 45 deletions.
16 changes: 14 additions & 2 deletions EXPERIMENTAL/typicmd/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type application struct {

func (a application) Run(ctx *cli.Context) (err error) {
di := dig.New()
defer a.Destruct(di)
defer a.Close(di)
gracefulStop := make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
Expand All @@ -36,7 +36,7 @@ func (a application) Run(ctx *cli.Context) (err error) {
go func() {
<-gracefulStop
fmt.Println("\n\n\nGraceful Shutdown...")
a.Destruct(di)
a.Close(di)
}()
runner := a.Application.(typiobj.Runner)
return runner.Run(di)
Expand All @@ -50,3 +50,15 @@ func (a application) Provide() (constructors []interface{}) {
}
return
}

func (a application) Destroy() (destructors []interface{}) {
destructors = append(destructors, a.Modules.Destroy()...)
if destroyer, ok := a.Application.(typiobj.Destroyer); ok {
destructors = append(destructors, destroyer.Destroy()...)
}
return
}

func (a application) Close(c *dig.Container) (err error) {
return typiobj.Destroy(c, a)
}
17 changes: 8 additions & 9 deletions EXPERIMENTAL/typictx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package typictx
import (
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/slice"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
"go.uber.org/dig"
)

// Context of typical application
Expand Down Expand Up @@ -51,14 +50,14 @@ func (c *Context) Configurations() (cfgs []typiobj.Configuration) {
// }

// Destruct dependencies
func (c *Context) Destruct(container *dig.Container) (err error) {
if destructor, ok := c.Application.(typiobj.Destructor); ok {
if err = destructor.Destruct(container); err != nil {
return
}
}
return c.Modules.Destruct(container)
}
// func (c *Context) Destruct(container *dig.Container) (err error) {
// if destructor, ok := c.Application.(typiobj.Destructor); ok {
// if err = destructor.Destruct(container); err != nil {
// return
// }
// }
// return c.Modules.Destruct(container)
// }

// Preparing context
// TODO: rename back to validate as conflicting with life cycle phase
Expand Down
6 changes: 4 additions & 2 deletions EXPERIMENTAL/typiobj/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ func CliAction(p interface{}, fn interface{}) func(ctx *cli.Context) error {
return func(ctx *cli.Context) (err error) {
c := dig.New()
defer func() {
if destructor, ok := p.(Destructor); ok {
destructor.Destruct(c)
if destroyer, ok := p.(Destroyer); ok {
if err = Destroy(c, destroyer); err != nil {
return
}
}
}()
if provider, ok := p.(Provider); ok {
Expand Down
24 changes: 24 additions & 0 deletions EXPERIMENTAL/typiobj/destroyer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package typiobj

import "go.uber.org/dig"

// Destroyer responsible to destruct dependency
type Destroyer interface {
Destroy() []interface{}
}

// Destroy to execute destroyer
func Destroy(c *dig.Container, d Destroyer) (err error) {
for _, destructor := range d.Destroy() {
if err = c.Invoke(destructor); err != nil {
return
}
}
return
}

// IsDestroyer return true if object implementation of destructor
func IsDestroyer(obj interface{}) (ok bool) {
_, ok = obj.(Destroyer)
return
}
11 changes: 4 additions & 7 deletions EXPERIMENTAL/typiobj/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package typiobj
import (
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/slice"
"github.com/urfave/cli"
"go.uber.org/dig"
)

// Modules is list of module
Expand Down Expand Up @@ -39,13 +38,11 @@ func (m Modules) Provide() (constructors []interface{}) {
return
}

// Destruct dependency
func (m Modules) Destruct(c *dig.Container) (err error) {
// Destroy dependency
func (m Modules) Destroy() (destructors []interface{}) {
for _, module := range m {
if destructor, ok := module.(Destructor); ok {
if err = destructor.Destruct(c); err != nil {
return
}
if destroyer, ok := module.(Destroyer); ok {
destructors = append(destructors, destroyer.Destroy()...)
}
}
return
Expand Down
6 changes: 0 additions & 6 deletions EXPERIMENTAL/typiobj/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ func IsProvider(obj interface{}) (ok bool) {
return
}

// IsDestructor return true if object implementation of destructor
func IsDestructor(obj interface{}) (ok bool) {
_, ok = obj.(Destructor)
return
}

// IsCommandLiner return true if object implementation of CommandLiner
func IsCommandLiner(obj interface{}) (ok bool) {
_, ok = obj.(CommandLiner)
Expand Down
5 changes: 0 additions & 5 deletions EXPERIMENTAL/typiobj/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ type Provider interface {
Provide() []interface{}
}

// Destructor responsible to destruct dependency
type Destructor interface {
Destruct(c *dig.Container) error
}

// CommandLiner responsible to give command
type CommandLiner interface {
CommandLine() cli.Command
Expand Down
9 changes: 5 additions & 4 deletions pkg/typpostgres/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
"github.com/typical-go/typical-rest-server/pkg/utility/envkit"
"github.com/urfave/cli"
"go.uber.org/dig"
)

const (
Expand Down Expand Up @@ -67,9 +66,11 @@ func (p postgresModule) Provide() []interface{} {
}
}

// Destruct dependencies
func (p postgresModule) Destruct(c *dig.Container) (err error) {
return c.Invoke(p.closeConnection)
// Destroy dependencies
func (p postgresModule) Destroy() []interface{} {
return []interface{}{
p.closeConnection,
}
}

func (p postgresModule) loadConfig() (cfg *Config, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/typpostgres/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestModule(t *testing.T) {
m := typpostgres.Module()
require.True(t, typiobj.IsProvider(m))
require.True(t, typiobj.IsDestructor(m))
require.True(t, typiobj.IsDestroyer(m))
require.True(t, typiobj.IsCommandLiner(m))
require.True(t, typiobj.IsConfigurer(m))
}
9 changes: 5 additions & 4 deletions pkg/typredis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
"github.com/typical-go/typical-rest-server/pkg/utility/envkit"
"github.com/urfave/cli"
"go.uber.org/dig"
)

// Module of redis
Expand All @@ -37,9 +36,11 @@ func (r redisModule) Provide() []interface{} {
}
}

// Destruct dependencies
func (r redisModule) Destruct(c *dig.Container) (err error) {
return c.Invoke(c)
// Destroy dependencies
func (r redisModule) Destroy() []interface{} {
return []interface{}{
r.disconnect,
}
}

// CommandLine return command
Expand Down
2 changes: 1 addition & 1 deletion pkg/typredis/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestModule(t *testing.T) {
m := typredis.Module()
require.True(t, typiobj.IsProvider(m))
require.True(t, typiobj.IsDestructor(m))
require.True(t, typiobj.IsDestroyer(m))
require.True(t, typiobj.IsCommandLiner(m))
require.True(t, typiobj.IsConfigurer(m))
}
7 changes: 4 additions & 3 deletions pkg/typserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/kelseyhightower/envconfig"
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typiobj"
"go.uber.org/dig"

logrusmiddleware "github.com/bakatz/echo-logrusmiddleware"
"github.com/labstack/echo"
Expand Down Expand Up @@ -36,8 +35,10 @@ func (s serverModule) Provide() []interface{} {
}
}

func (s serverModule) Destruct(c *dig.Container) (err error) {
return c.Invoke(s.Shutdown)
func (s serverModule) Destroy() []interface{} {
return []interface{}{
s.Shutdown,
}
}

func (s serverModule) loadConfig() (cfg *Config, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/typserver/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (
func TestModule(t *testing.T) {
m := typserver.Module()
require.True(t, typiobj.IsProvider(m))
require.True(t, typiobj.IsDestructor(m))
require.True(t, typiobj.IsDestroyer(m))
require.True(t, typiobj.IsConfigurer(m))
}

0 comments on commit 0c29b92

Please sign in to comment.