Skip to content

Commit

Permalink
refactor: remove old handlers ShutdownOnSIGTERMxxxx
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Dec 30, 2023
1 parent 81b3d8f commit 6539758
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 46 deletions.
6 changes: 6 additions & 0 deletions docs/docs/service-lifecycle/shutdowner.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ do.MustShutdownNamed[T any](do.Injector, string)
do.MustShutdownNamedWithContext[T any](context.Context, do.Injector, string)
```

:::info

If no signal is passed to `injector.ShutdownOnSignals(...)`, both `syscall.SIGTERM` and `os.Interrupt` are handled by default.

:::

## Shutdowner interfaces

Your service can implement one of the following signatures:
Expand Down
2 changes: 1 addition & 1 deletion examples/nested-scope/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func main() {
driver.TakeASeat()
passenger.TakeASeat()

fmt.Println(injector.ShutdownOnSIGTERMOrInterrupt())
fmt.Println(injector.ShutdownOnSignals())
}
2 changes: 1 addition & 1 deletion examples/shutdownable/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func main() {
car := do.MustInvoke[*Car](injector)
car.Start()

_, err := injector.ShutdownOnSIGTERM()
_, err := injector.ShutdownOnSignals()
if err != nil {
log.Fatal(err.Error())
}
Expand Down
32 changes: 4 additions & 28 deletions root_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,44 +139,20 @@ func (s *RootScope) CloneWithOpts(opts *InjectorOpts) *RootScope {
return clone
}

// ShutdownOnSIGTERM listens for sigterm signal in order to graceful stop service.
// It will block until receiving a sigterm signal.
func (s *RootScope) ShutdownOnSIGTERM() (os.Signal, error) {
return s.ShutdownOnSignalsWithContext(context.Background(), syscall.SIGTERM)
}

// ShutdownOnSIGTERMWithContext listens for sigterm signal in order to graceful stop service.
// It will block until receiving a sigterm signal.
func (s *RootScope) ShutdownOnSIGTERMWithContext(ctx context.Context) (os.Signal, error) {
return s.ShutdownOnSignalsWithContext(ctx, syscall.SIGTERM)
}

// ShutdownOnSIGTERMOrInterrupt listens for sigterm or interrupt signal in order to graceful stop service.
// It will block until receiving a sigterm signal.
func (s *RootScope) ShutdownOnSIGTERMOrInterrupt() (os.Signal, error) {
return s.ShutdownOnSignalsWithContext(context.Background(), syscall.SIGTERM, os.Interrupt)
}

// ShutdownOnSIGTERMOrInterruptWithContext listens for sigterm or interrupt signal in order to graceful stop service.
// It will block until receiving a sigterm signal.
func (s *RootScope) ShutdownOnSIGTERMOrInterruptWithContext(ctx context.Context) (os.Signal, error) {
return s.ShutdownOnSignalsWithContext(ctx, syscall.SIGTERM, os.Interrupt)
}

// ShutdownOnSignals listens for signals defined in signals parameter in order to graceful stop service.
// It will block until receiving any of these signal.
// If no signal is provided in signals parameter, syscall.SIGTERM will be added as default signal.
// If no signal is provided in signals parameter, syscall.SIGTERM and os.Interrupt will be added as default signal.
func (s *RootScope) ShutdownOnSignals(signals ...os.Signal) (os.Signal, error) {
return s.ShutdownOnSignalsWithContext(context.Background(), signals...)
}

// ShutdownOnSignalsWithContext listens for signals defined in signals parameter in order to graceful stop service.
// It will block until receiving any of these signal.
// If no signal is provided in signals parameter, syscall.SIGTERM will be added as default signal.
// If no signal is provided in signals parameter, syscall.SIGTERM and os.Interrupt will be added as default signal.
func (s *RootScope) ShutdownOnSignalsWithContext(ctx context.Context, signals ...os.Signal) (os.Signal, error) {
// Make sure there is at least syscall.SIGTERM as a signal
// Make sure there is at least syscall.SIGTERM and os.Interrupt as a signal
if len(signals) < 1 {
signals = append(signals, syscall.SIGTERM)
signals = append(signals, syscall.SIGTERM, os.Interrupt)
}

ch := make(chan os.Signal, 5)
Expand Down
16 changes: 0 additions & 16 deletions root_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,6 @@ func TestRootScope_CloneWithOpts(t *testing.T) {
is.NotNil(clone.healthCheckPool)
}

func TestRootScope_ShutdownOnSIGTERMOrInterrupt(t *testing.T) {
// @TODO
}

func TestRootScope_ShutdownOnSIGTERMOrInterruptWithContext(t *testing.T) {
// @TODO
}

func TestRootScope_ShutdownOnSIGTERM(t *testing.T) {
// @TODO
}

func TestRootScope_ShutdownOnSIGTERMWithContext(t *testing.T) {
// @TODO
}

func TestRootScope_ShutdownOnSignals(t *testing.T) {
// @TODO
}
Expand Down

0 comments on commit 6539758

Please sign in to comment.