utils: Take monitorShutdownSignal to take an exitFunc which would executed upon error. (#2378)

This hook approach allows program to keep running but being able to handle exiting
of the program in the dynamic way.

Fixes #2377
master
Harshavardhana 8 years ago committed by GitHub
parent b23605a2b5
commit 0188cd0b84
  1. 2
      main.go
  2. 13
      utils.go
  3. 20
      utils_test.go

@ -191,7 +191,7 @@ func main() {
// Initialize and monitor shutdown signal
shutdownSignal = make(chan bool, 1)
monitorShutdownSignal()
monitorShutdownSignal(os.Exit)
// Run the app - exit on error.
app.RunAndExitOnError()

@ -100,10 +100,13 @@ func registerObjectStorageShutdown(callback func() errCode) {
shutdownObjectStorageCallbacks = append(shutdownObjectStorageCallbacks, callback)
}
// Represents a type of an exit func which will be invoked during shutdown signal.
type onExitFunc func(code int)
// Start to monitor shutdownSignal to execute shutdown callbacks
func monitorShutdownSignal() {
func monitorShutdownSignal(onExitFn onExitFunc) {
go func() {
// Monitor processus signal
// Monitor signals.
trapCh := signalTrap(os.Interrupt, syscall.SIGTERM)
for {
select {
@ -115,7 +118,7 @@ func monitorShutdownSignal() {
for _, callback := range shutdownCallbacks {
exitCode := callback()
if exitCode != exitSuccess {
os.Exit(int(exitCode))
onExitFn(int(exitCode))
}
}
@ -123,11 +126,11 @@ func monitorShutdownSignal() {
for _, callback := range shutdownObjectStorageCallbacks {
exitCode := callback()
if exitCode != exitSuccess {
os.Exit(int(exitCode))
onExitFn(int(exitCode))
}
}
os.Exit(int(exitSuccess))
onExitFn(int(exitSuccess))
}
}
}()

@ -16,13 +16,10 @@
package main
import (
"testing"
)
import "testing"
// ShutdownCallback simulates a successful exit here, all registered
// shutdown callbacks need to return exitSuccess for this test to succeed
func TestShutdownCallback(t *testing.T) {
// ShutdownCallback simulates a successful and failure exit here.
func TestShutdownCallbackSuccess(t *testing.T) {
// Register two callbacks that return success
registerObjectStorageShutdown(func() errCode {
return exitSuccess
@ -33,10 +30,11 @@ func TestShutdownCallback(t *testing.T) {
shutdownSignal = make(chan bool, 1)
shutdownSignal <- true
// Start executing callbacks and quit if everything is fine
monitorShutdownSignal()
// Infinite loop here simulates an infinite running program
for {
// Start executing callbacks and exitFunc receives a success.
dummySuccess := func(code int) {
if code != int(exitSuccess) {
t.Fatalf("Expected %d, got %d instead.", code, exitSuccess)
}
}
monitorShutdownSignal(dummySuccess)
}

Loading…
Cancel
Save