Skip to content

Commit

Permalink
Shutdown metrics server when listener exits (#3445)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-jokic authored Apr 16, 2024
1 parent 4ee49fe commit f965dfe
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
8 changes: 6 additions & 2 deletions cmd/ghalistener/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,19 @@ func (app *App) Run(ctx context.Context) error {
}

g, ctx := errgroup.WithContext(ctx)
metricsCtx, cancelMetrics := context.WithCancelCause(ctx)

g.Go(func() error {
app.logger.Info("Starting listener")
return app.listener.Listen(ctx, app.worker)
listnerErr := app.listener.Listen(ctx, app.worker)
cancelMetrics(fmt.Errorf("Listener exited: %w", listnerErr))
return listnerErr
})

if app.metrics != nil {
g.Go(func() error {
app.logger.Info("Starting metrics server")
return app.metrics.ListenAndServe(ctx)
return app.metrics.ListenAndServe(metricsCtx)
})
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ghalistener/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ func (l *Listener) Listen(ctx context.Context, handler Handler) error {
continue
}

// New context is created to avoid cancelation during message handling.
if err := l.handleMessage(context.Background(), handler, msg); err != nil {
// Remove cancellation from the context to avoid cancelling the message handling.
if err := l.handleMessage(context.WithoutCancel(ctx), handler, msg); err != nil {
return fmt.Errorf("failed to handle message: %w", err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ghalistener/listener/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ func TestListener_Listen(t *testing.T) {
).
Once()

// Ensure delete message is called with background context
client.On("DeleteMessage", context.Background(), mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
// Ensure delete message is called without cancel
client.On("DeleteMessage", context.WithoutCancel(ctx), mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()

config.Client = client

Expand Down
5 changes: 4 additions & 1 deletion cmd/ghalistener/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"strconv"
"time"

"github.com/actions/actions-runner-controller/github/actions"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -338,7 +339,9 @@ func (e *exporter) ListenAndServe(ctx context.Context) error {
e.logger.Info("starting metrics server", "addr", e.srv.Addr)
go func() {
<-ctx.Done()
e.logger.Info("stopping metrics server")
e.logger.Info("stopping metrics server", "err", ctx.Err())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
e.srv.Shutdown(ctx)
}()
return e.srv.ListenAndServe()
Expand Down

0 comments on commit f965dfe

Please sign in to comment.