Skip to content

Commit

Permalink
fix: Ensure exit with non-zero code when error occurs
Browse files Browse the repository at this point in the history
fixes #318

Signed-off-by: Leonard Goodell <[email protected]>
  • Loading branch information
Leonard Goodell committed Jul 14, 2022
1 parent 1361f04 commit e354950
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
7 changes: 6 additions & 1 deletion bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func Run(
useSecretProvider bool,
handlers []interfaces.BootstrapHandler) {

wg, deferred, _ := RunAndReturnWaitGroup(
wg, deferred, success := RunAndReturnWaitGroup(
ctx,
cancel,
commonFlags,
Expand All @@ -195,6 +195,11 @@ func Run(
handlers,
)

if !success {
cancel()
os.Exit(1)
}

defer deferred()

// wait for go routines to stop executing.
Expand Down
18 changes: 12 additions & 6 deletions bootstrap/handlers/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
commonDTO "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
"net/http"
"os"
"strconv"
"sync"
"time"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
commonDTO "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"

"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
Expand Down Expand Up @@ -123,7 +125,6 @@ func (b *HttpServer) BootstrapHandler(
defer wg.Done()

<-ctx.Done()
lc.Info("Web server shutting down")
_ = server.Shutdown(context.Background())
lc.Info("Web server shut down")
}()
Expand All @@ -139,10 +140,15 @@ func (b *HttpServer) BootstrapHandler(

b.isRunning = true
err := server.ListenAndServe()
if err != nil {
// "Server closed" error occurs when Shutdown above is called in the Done processing, so it can be ignored
if err != nil && err.Error() != "http: Server closed" {
// Other errors occur during bootstrapping, like port bind fails, are considered fatal
lc.Errorf("Web server failed: %v", err)
cancel := container.CancelFuncFrom(dic.Get)
cancel() // this will caused the service to stop
cancel() // this will clean up any long-running go functions that may have started
// Give time for clean up to occur before exiting.
time.Sleep(1 * time.Millisecond)
os.Exit(1)
} else {
lc.Info("Web server stopped")
}
Expand Down

0 comments on commit e354950

Please sign in to comment.