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

Provide ConfigureOriginAPI a cancel context that can be cancelled on shutdown #431

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions cmd/origin_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func serveOrigin( /*cmd*/ *cobra.Command /*args*/, []string) error {
config.CleanupTempResources()
}()

wg.Add(1)
err := xrootd.SetUpMonitoring(shutdownCtx, &wg)
if err != nil {
return err
}
wg.Add(1) // Add to wg afterward to ensure no error causes deadlock

err = checkDefaults(true, nil)
if err != nil {
Expand All @@ -142,9 +142,10 @@ func serveOrigin( /*cmd*/ *cobra.Command /*args*/, []string) error {
}

// Set up the APIs unrelated to UI, which only contains director-based health test reporting endpoint for now
if err = origin_ui.ConfigureOriginAPI(engine); err != nil {
if err = origin_ui.ConfigureOriginAPI(engine, shutdownCtx, &wg); err != nil {
return err
}
wg.Add(1)
if err = origin_ui.RegisterNamespaceWithRetry(); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions metrics/xrootd_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func ConfigureMonitoring(ctx context.Context, wg *sync.WaitGroup) (int, error) {
sessions.Stop()
userids.Stop()
transfers.Stop()
log.Infoln("Gracefully stopping metrics cache auto eviction...")
}()

go func() {
Expand Down
15 changes: 5 additions & 10 deletions origin_ui/origin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
package origin_ui

import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -42,7 +40,6 @@ var (
// Duration to wait before timeout
// TODO: Do we want to make this a configurable value?
directorTimeoutDuration = 30 * time.Second
exitSignals = make(chan os.Signal, 1)
exitLoop = make(chan struct{})
)

Expand Down Expand Up @@ -140,7 +137,7 @@ func directorTestResponse(ctx *gin.Context) {
}

// Configure API endpoints for origin that are not tied to UI
func ConfigureOriginAPI(router *gin.Engine) error {
func ConfigureOriginAPI(router *gin.Engine, ctx context.Context, wg *sync.WaitGroup) error {
if router == nil {
return errors.New("Origin configuration passed a nil pointer")
}
Expand All @@ -149,21 +146,19 @@ func ConfigureOriginAPI(router *gin.Engine) error {
// start the timer for the director test report timeout
resetDirectorTimeoutTimer()

// When program exits
signal.Notify(exitSignals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

go func() {
// Gracefully stop the timer at the exit of the program
<-exitSignals
defer wg.Done()
<-ctx.Done()
timerMutex.Lock()
defer timerMutex.Unlock()
log.Infoln("Gracefully stopping the director-health test timeout timer...")
// Terminate the infinite loop to reset the timer
close(exitLoop)
if directorTimeoutTimer != nil {
directorTimeoutTimer.Stop()
directorTimeoutTimer = nil
}
log.Infoln("Gracefully stopping the director-health test timeout timer...")
}()

group := router.Group("/api/v1.0/origin-api")
Expand Down
Loading