Skip to content

Commit

Permalink
Update shutdown code for ProcessContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed May 3, 2022
1 parent 61820ab commit caf1df8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
34 changes: 15 additions & 19 deletions container/ProcessContainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,33 +156,19 @@ func (c *ProcessContainer) printHelp() {
}

func (c *ProcessContainer) captureErrors(correlationId string) {
c.Logger().Info(correlationId, "Try recover...")
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
msg := cconv.StringConverter.ToString(r)
err = errors.New(msg)
}
c.Close(correlationId)
c.Logger().Fatal(correlationId, err, "Process is terminated")
os.Exit(1)
}
}

func (c *ProcessContainer) captureExit(correlationId string) {
c.Logger().Info(correlationId, "Press Control-C to stop the microservice...")

ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
select {
case <-ch:
c.Close(correlationId)
c.Logger().Info(correlationId, "Goodbye!")
os.Exit(0)
}
}()
}

// Runs the container by instantiating and running components inside the container.
// It reads the container configuration, creates, configures, references and opens components. On process exit it closes, unreferences and destroys components to gracefully shutdown.
// Parameters:
Expand All @@ -206,16 +192,26 @@ func (c *ProcessContainer) Run(args []string) {
return
}

c.Logger().Info(correlationId, "Press Control-C to stop the microservice...")

defer c.captureErrors(correlationId)
c.captureExit(correlationId)

err = c.Open(correlationId)
if err != nil {
c.Close(correlationId)
c.Logger().Fatal(correlationId, err, "Process is terminated")
os.Exit(1)
return
}

ch := make(chan bool)
<-ch
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGABRT)

select {
case <-ch:
c.Close(correlationId)
c.Logger().Info(correlationId, "Goodbye!")
os.Exit(0)
}

}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ go 1.16
require (
github.com/pip-services3-go/pip-services3-commons-go v1.1.0
github.com/pip-services3-go/pip-services3-components-go v1.2.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.7.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)

0 comments on commit caf1df8

Please sign in to comment.