Ocean/Shutdown/Shutdown.go

54 lines
1.6 KiB
Go
Raw Permalink Normal View History

package Shutdown
import (
"container/list"
"fmt"
"github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta"
"os"
)
2015-06-17 15:44:52 +00:00
// A type for shutdown handlers.
type ShutdownHandler interface {
Shutdown()
}
2015-06-17 15:44:52 +00:00
// Function to add new shutdown handlers.
func AddShutdownHandler(handler ShutdownHandler) {
shutdownHandlers.PushFront(handler)
}
2015-06-17 15:44:52 +00:00
// The thread which waits for the shutdown event.
func executeShutdown() {
2015-06-17 15:44:52 +00:00
// Wait for the shutdown event:
sig := <-shutdownSignal
stopAllRequests = true
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameSHUTDOWN, `The system was called to shutting down.`, sig.String(), `Call now all shutdown handlers.`)
2015-06-17 15:44:52 +00:00
// Execute all shutdown handlers:
for handler := shutdownHandlers.Front(); handler != nil; handler = handler.Next() {
safeCall(handler)
}
2015-06-17 15:44:52 +00:00
// Shutdown the logging system:
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameSHUTDOWN, `The system is shutting down now.`)
Log.Flush()
2015-06-17 15:44:52 +00:00
// Stop the whole server:
os.Exit(0)
}
2015-06-17 15:44:52 +00:00
// This function is a wrapper to call safely a shutdown handler.
func safeCall(handler *list.Element) {
defer func() {
if err := recover(); err != nil {
errObj := fmt.Errorf("%v", err)
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.SeverityNone, LM.ImpactNone, LM.MessageNameSHUTDOWN, `An error occurs for a shutdown handler.`, errObj.Error())
}
}()
h := handler.Value.(ShutdownHandler)
h.Shutdown()
}