Skip to content

Commit

Permalink
Add fx.ShutdownError option
Browse files Browse the repository at this point in the history
  • Loading branch information
abramlab committed Dec 8, 2022
1 parent 5eb0e76 commit 02a2658
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
49 changes: 42 additions & 7 deletions shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package fx
import (
"context"
"time"

"go.uber.org/multierr"
)

// Shutdowner provides a method that can manually trigger the shutdown of the
Expand All @@ -34,17 +36,11 @@ type Shutdowner interface {
}

// ShutdownOption provides a way to configure properties of the shutdown
// process. Currently, no options have been implemented.
// process.
type ShutdownOption interface {
apply(*shutdowner)
}

type exitCodeOption int

func (code exitCodeOption) apply(s *shutdowner) {
s.exitCode = int(code)
}

var _ ShutdownOption = exitCodeOption(0)

// ExitCode is a [ShutdownOption] that may be passed to the Shutdown method of the
Expand All @@ -71,6 +67,45 @@ func ShutdownTimeout(timeout time.Duration) ShutdownOption {
return shutdownTimeoutOption(timeout)
}

type shutdownErrorOption []error

func (errs shutdownErrorOption) apply(s *shutdowner) {
s.app.err = multierr.Append(s.app.err, multierr.Combine(errs...))
}

// ShutdownError registers any number of errors with the application shutdown.
// If more than one error is given, the errors are combined into a
// single error. Similar to invocations, errors are applied in order.
//
// You can use these errors, for example, to decide what to do after the app shutdown.
//
// customErr := errors.New("something went wrong")
// app := fx.New(
// ...
// fx.Provide(func(s fx.Shutdowner, a A) B {
// s.Shutdown(fx.ShutdownError(customErr))
// }),
// ...
// )
// err := app.Start(context.Background())
// if err != nil {
// panic(err)
// }
// defer app.Stop(context.Background())
//
// if err := app.Err(); errors.Is(err, customErr) {
// // custom logic here
// }
func ShutdownError(errs ...error) ShutdownOption {
return shutdownErrorOption(errs)
}

type exitCodeOption int

func (code exitCodeOption) apply(s *shutdowner) {
s.exitCode = int(code)
}

type shutdowner struct {
app *App
exitCode int
Expand Down
23 changes: 23 additions & 0 deletions shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package fx_test

import (
"context"
"errors"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -128,6 +129,28 @@ func TestShutdown(t *testing.T) {

assert.NoError(t, s.Shutdown(fx.ExitCode(2), fx.ShutdownTimeout(time.Second)))
})

t.Run("with shutdown error", func(t *testing.T) {
t.Parallel()

var s fx.Shutdowner
app := fxtest.New(
t,
fx.Populate(&s),
)

done := app.Done()
wait := app.Wait()
defer app.RequireStart().RequireStop()

var expectedError = errors.New("shutdown error")

assert.NoError(t, s.Shutdown(fx.ShutdownError(expectedError)), "error in app shutdown")
assert.NotNil(t, <-done, "done channel did not receive signal")
assert.NotNil(t, <-wait, "wait channel did not receive signal")
assert.ErrorIs(t, app.Err(), expectedError,
"unexpected error, expected: %q, got: %q", expectedError, app.Err())
})
}

func TestDataRace(t *testing.T) {
Expand Down

0 comments on commit 02a2658

Please sign in to comment.