Skip to content

Commit

Permalink
feat: add callback funcs to InstallSignalHandler (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarschulte authored Aug 27, 2024
1 parent d72d1c0 commit 2402af8
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion go/action_kit_sdk/action_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ type ActionWithMetricQuery[T any] interface {
QueryMetrics(ctx context.Context, request action_kit_api.QueryMetricsRequestBody) (*action_kit_api.QueryMetricsResult, error)
}

type SignalHandlerCallBackFn func(os.Signal)

// InstallSignalHandler registers a signal handler that stops all active actions on SIGINT, SIGTERM and SIGUSR1.
func InstallSignalHandler() {
func InstallSignalHandler(callbacks ...SignalHandlerCallBackFn) {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
go func(signals <-chan os.Signal) {
Expand All @@ -78,6 +80,11 @@ func InstallSignalHandler() {
log.Debug().Str("signal", signalName).Msg("received signal - stopping all active actions")
StopAllActiveActions(fmt.Sprintf("received signal %s", signalName))

for _, callback := range callbacks {
log.Debug().Str("signal", signalName).Msg("calling signal handler callback")
callback(s)
}

switch s {
case syscall.SIGINT:
fmt.Println()
Expand Down

0 comments on commit 2402af8

Please sign in to comment.