mirror of
https://github.com/pgsty/minio.git
synced 2026-07-19 12:10:24 +03:00
Add a generic registerShutdown function for graceful exit (#2344)
* Add a generic registerShutdown function for graceful exit * Add shutdown callback test case
This commit is contained in:
committed by
Harshavardhana
parent
62c0612eac
commit
d28fb5fe23
@@ -20,7 +20,9 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// xmlDecoder provide decoded value in xml.
|
||||
@@ -73,3 +75,60 @@ func contains(stringList []string, element string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// shutdownSignal - is the channel that receives any boolean when
|
||||
// we want broadcast the start of shutdown
|
||||
var shutdownSignal chan bool
|
||||
|
||||
// shutdownCallbacks - is the list of function callbacks executed one by one
|
||||
// when a shutdown starts. A callback returns 0 for success and 1 for failure.
|
||||
// Failure is considered an emergency error that needs an immediate exit
|
||||
var shutdownCallbacks []func() errCode
|
||||
|
||||
// shutdownObjectStorageCallbacks - contains the list of function callbacks that
|
||||
// need to be invoked when a shutdown starts. These callbacks will be called before
|
||||
// the general callback shutdowns
|
||||
var shutdownObjectStorageCallbacks []func() errCode
|
||||
|
||||
// Register callback functions that need to be called when process terminates.
|
||||
func registerShutdown(callback func() errCode) {
|
||||
shutdownCallbacks = append(shutdownCallbacks, callback)
|
||||
}
|
||||
|
||||
// Register object storagecallback functions that need to be called when process terminates.
|
||||
func registerObjectStorageShutdown(callback func() errCode) {
|
||||
shutdownObjectStorageCallbacks = append(shutdownObjectStorageCallbacks, callback)
|
||||
}
|
||||
|
||||
// Start to monitor shutdownSignal to execute shutdown callbacks
|
||||
func monitorShutdownSignal() {
|
||||
go func() {
|
||||
// Monitor processus signal
|
||||
trapCh := signalTrap(os.Interrupt, syscall.SIGTERM)
|
||||
for {
|
||||
select {
|
||||
case <-trapCh:
|
||||
// Start a graceful shutdown call
|
||||
shutdownSignal <- true
|
||||
case <-shutdownSignal:
|
||||
// Call all callbacks and exit for emergency
|
||||
for _, callback := range shutdownCallbacks {
|
||||
exitCode := callback()
|
||||
if exitCode != exitSuccess {
|
||||
os.Exit(int(exitCode))
|
||||
}
|
||||
|
||||
}
|
||||
// Call all object storage shutdown callbacks and exit for emergency
|
||||
for _, callback := range shutdownObjectStorageCallbacks {
|
||||
exitCode := callback()
|
||||
if exitCode != exitSuccess {
|
||||
os.Exit(int(exitCode))
|
||||
}
|
||||
|
||||
}
|
||||
os.Exit(int(exitSuccess))
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user