-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
centralize registration of exit handlers
Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
- Loading branch information
Showing
4 changed files
with
75 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package util | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
var exitHandlers []func() | ||
|
||
// RegisterExitHandler appends a func Exit handler to the list of handlers. | ||
// The handlers will be invoked when vfkit receives a termination or interruption signal | ||
// | ||
// This method is useful when a caller wishes to execute a func before a shutdown. | ||
func RegisterExitHandler(handler func()) { | ||
exitHandlers = append(exitHandlers, handler) | ||
} | ||
|
||
// SetupExitSignalHandling sets up a signal channel to listen for termination or interruption signals. | ||
// When one of these signals is received, all the registered exit handlers will be invoked, just | ||
// before terminating the program. | ||
func SetupExitSignalHandling() { | ||
setupExitSignalHandling(true) | ||
} | ||
|
||
// setupExitSignalHandling sets up a signal channel to listen for termination or interruption signals. | ||
// When one of these signals is received, all the registered exit handlers will be invoked. | ||
// It is possible to prevent the program from exiting by setting the doExit param to false (used for testing) | ||
func setupExitSignalHandling(doExit bool) { | ||
sigChan := make(chan os.Signal, 2) | ||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) | ||
go func() { | ||
for sig := range sigChan { | ||
log.Printf("captured %v, calling exit handlers and exiting..", sig) | ||
for _, handler := range exitHandlers { | ||
handler() | ||
} | ||
if doExit { | ||
os.Exit(1) | ||
} | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import ( | ||
"syscall" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestExitHandlerCalled(t *testing.T) { | ||
setupExitSignalHandling(false) | ||
|
||
ch := make(chan struct{}) | ||
RegisterExitHandler(func() { | ||
close(ch) | ||
}) | ||
|
||
syscall.Kill(syscall.Getpid(), syscall.SIGINT) | ||
|
||
select { | ||
case <-ch: | ||
// exit handler was called | ||
case <-time.After(5 * time.Second): | ||
t.Errorf("Exit handler not called - timed out") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters