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 2 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
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()
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
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")
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,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 != http.ErrServerClosed {
// 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)
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
} else {
lc.Info("Web server stopped")
}
Expand Down