Skip to content

Commit

Permalink
Merge pull request #615 from bbockelm/shutdown_on_err
Browse files Browse the repository at this point in the history
Fixes to the shutdown / error sequence
  • Loading branch information
jhiemstrawisc authored Jan 8, 2024
2 parents 802d0b2 + 5b8cc63 commit 9965d30
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
6 changes: 5 additions & 1 deletion cmd/director_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
)

func serveDirector(cmd *cobra.Command, args []string) error {
_, err := launchers.LaunchModules(cmd.Context(), config.DirectorType)
cancel, err := launchers.LaunchModules(cmd.Context(), config.DirectorType)
if err != nil {
cancel()
}

return err
}
6 changes: 5 additions & 1 deletion cmd/fed_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func fedServeStart(cmd *cobra.Command, args []string) error {
return errors.New("`pelican serve` does not support the cache module")
}

_, err := launchers.LaunchModules(cmd.Context(), modules)
cancel, err := launchers.LaunchModules(cmd.Context(), modules)
if err != nil {
cancel()
}

return err
}
5 changes: 4 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func handleCLI(args []string) error {
fmt.Println("Built By:", builtBy)
return nil
}
Execute()
err := Execute()
if err != nil {
os.Exit(1)
}
}
return nil
}
6 changes: 5 additions & 1 deletion cmd/origin_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
)

func serveOrigin(cmd *cobra.Command, args []string) error {
_, err := launchers.LaunchModules(cmd.Context(), config.OriginType)
cancel, err := launchers.LaunchModules(cmd.Context(), config.OriginType)
if err != nil {
cancel()
}

return err
}
6 changes: 5 additions & 1 deletion cmd/registry_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
)

func serveRegistry(cmd *cobra.Command, _ []string) error {
_, err := launchers.LaunchModules(cmd.Context(), config.RegistryType)
cancel, err := launchers.LaunchModules(cmd.Context(), config.RegistryType)
if err != nil {
cancel()
}

return err
}
8 changes: 2 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package main

import (
"context"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -78,7 +77,7 @@ func (i *uint16Value) Type() string {

func (i *uint16Value) String() string { return strconv.FormatUint(uint64(*i), 10) }

func Execute() {
func Execute() error {
egrp := errgroup.Group{}
defer func() {
err := egrp.Wait()
Expand All @@ -87,10 +86,7 @@ func Execute() {
}
}()
ctx := context.WithValue(context.Background(), config.EgrpKey, &egrp)
err := rootCmd.ExecuteContext(ctx)
if err != nil {
os.Exit(1)
}
return rootCmd.ExecuteContext(ctx)
}

func init() {
Expand Down
11 changes: 5 additions & 6 deletions launchers/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ func LaunchModules(ctx context.Context, modules config.ServerType) (context.Canc
return shutdownCancel, errors.Wrap(err, "Failure when configuring the server")
}

if param.Server_EnableUI.GetBool() {
// Set up necessary APIs to support Web UI, including auth and metrics
if err := web_ui.ConfigureServerWebAPI(ctx, engine, egrp); err != nil {
return shutdownCancel, err
}
// Set up necessary APIs to support Web UI, including auth and metrics
if err := web_ui.ConfigureServerWebAPI(ctx, engine, egrp); err != nil {
return shutdownCancel, err
}

if modules.IsEnabled(config.RegistryType) {
Expand Down Expand Up @@ -149,8 +147,9 @@ func LaunchModules(ctx context.Context, modules config.ServerType) (context.Canc
return nil
})

if err = server_utils.WaitUntilWorking(ctx, "GET", param.Server_ExternalWebUrl.GetString()+"/view", "Web UI", http.StatusOK); err != nil {
if err = server_utils.WaitUntilWorking(ctx, "GET", param.Server_ExternalWebUrl.GetString()+"/api/v1.0/servers", "Web UI", http.StatusOK); err != nil {
log.Errorln("Web engine startup appears to have failed:", err)
return shutdownCancel, err
}

if modules.IsEnabled(config.OriginType) {
Expand Down
18 changes: 11 additions & 7 deletions web_ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"crypto/tls"
"embed"
"fmt"
"github.com/pelicanplatform/pelican/config"
"math/rand"
"mime"
"net"
Expand All @@ -35,6 +34,8 @@ import (
"syscall"
"time"

"github.com/pelicanplatform/pelican/config"

"github.com/gin-gonic/gin"
"github.com/pelicanplatform/pelican/metrics"
"github.com/pelicanplatform/pelican/param"
Expand Down Expand Up @@ -247,18 +248,21 @@ func waitUntilLogin(ctx context.Context) error {
//
// You need to mount the static resources for UI in a separate function
func ConfigureServerWebAPI(ctx context.Context, engine *gin.Engine, egrp *errgroup.Group) error {
if err := configureAuthEndpoints(ctx, engine, egrp); err != nil {
return err
}
if err := configureCommonEndpoints(engine); err != nil {
return err
}
if err := configureWebResource(engine); err != nil {
return err
}
if err := configureMetrics(ctx, engine); err != nil {
return err
}
if param.Server_EnableUI.GetBool() {
if err := configureAuthEndpoints(ctx, engine, egrp); err != nil {
return err
}
if err := configureWebResource(engine); err != nil {
return err
}
}

// Redirect root to /view for web UI
engine.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/view/")
Expand Down

0 comments on commit 9965d30

Please sign in to comment.