Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure exit with non-zero code when error occurs #358

Merged
merged 4 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 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,13 @@ func Run(
handlers,
)

if !success {
// This only occurs when a bootstrap handler has fail.
// The handler will have logged an error, so not need to log a message here.
cancel()
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}

defer deferred()

// wait for go routines to stop executing.
Expand Down
22 changes: 16 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")
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
_ = server.Shutdown(context.Background())
lc.Info("Web server shut down")
}()
Expand All @@ -139,10 +140,19 @@ 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 != http.ErrServerClosed {
// Other errors occur during bootstrapping, like port bind fails, are considered fatal
lc.Errorf("Web server failed: %v", err)

// Allow any long-running go functions that may have started to stop before exiting
cancel := container.CancelFuncFrom(dic.Get)
cancel() // this will caused the service to stop
cancel()

// Wait for all long-running go functions to stop before exiting.
wg.Done() // Must do this to account for this go func's wg.Add above otherwise wait will block indefinitely
wg.Wait()
os.Exit(1)
} else {
lc.Info("Web server stopped")
}
Expand Down