Make the signal handler work properly
This commit is contained in:
parent
8a1cf0456a
commit
baf73f33e7
22
main.go
22
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/handlers"
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/handlers"
|
||||||
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
||||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,20 +22,21 @@ func main() {
|
||||||
// Merge old state if present
|
// Merge old state if present
|
||||||
util.MergeOldState(&spaceApiResponse)
|
util.MergeOldState(&spaceApiResponse)
|
||||||
|
|
||||||
|
// Register signal handler
|
||||||
|
sc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
go func(ch chan os.Signal, resp *types.SpaceAPIResponseV14) {
|
||||||
|
<-ch
|
||||||
|
log.Println("Saving state and shutting down...")
|
||||||
|
util.SaveCurrentState(*resp)
|
||||||
|
os.Exit(0)
|
||||||
|
}(sc, &spaceApiResponse)
|
||||||
|
|
||||||
// Register HTTP handlers
|
// Register HTTP handlers
|
||||||
http.HandleFunc("/", handlers.Root(&spaceApiResponse))
|
http.HandleFunc("/", handlers.Root(&spaceApiResponse))
|
||||||
http.HandleFunc("/state/open", handlers.StateOpen(config.BAUsername, config.BAPassword, &spaceApiResponse))
|
http.HandleFunc("/state/open", handlers.StateOpen(config.BAUsername, config.BAPassword, &spaceApiResponse))
|
||||||
|
|
||||||
// Start webserver
|
// Start webserver
|
||||||
log.Println("Starting HTTP server...")
|
log.Println("Starting HTTP server...")
|
||||||
go log.Fatalln(http.ListenAndServe(":8080", nil))
|
log.Fatalln(http.ListenAndServe(":8080", nil))
|
||||||
|
|
||||||
// Wait for exit signal
|
|
||||||
sc := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
<-sc
|
|
||||||
|
|
||||||
// Save state for next run
|
|
||||||
log.Println("Saving state and shutting down...")
|
|
||||||
util.SaveCurrentState(spaceApiResponse)
|
|
||||||
}
|
}
|
||||||
|
|
13
util/util.go
13
util/util.go
|
@ -47,12 +47,6 @@ func MergeOldState(response *types.SpaceAPIResponseV14) {
|
||||||
oldState []byte
|
oldState []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create state directory if not present
|
|
||||||
err = os.MkdirAll(path.Dir(savedStateJSONPath), 0750)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if state.json is present
|
// Check if state.json is present
|
||||||
_, err = os.Stat(savedStateJSONPath)
|
_, err = os.Stat(savedStateJSONPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -84,6 +78,13 @@ removeOld:
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveCurrentState(response types.SpaceAPIResponseV14) {
|
func SaveCurrentState(response types.SpaceAPIResponseV14) {
|
||||||
|
// Create state directory if not present
|
||||||
|
err := os.MkdirAll(path.Dir(savedStateJSONPath), 0750)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open persistent state file for reading
|
||||||
file, err := os.OpenFile(savedStateJSONPath, os.O_RDWR|os.O_CREATE, 0644)
|
file, err := os.OpenFile(savedStateJSONPath, os.O_RDWR|os.O_CREATE, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err)
|
log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err)
|
||||||
|
|
Loading…
Reference in a new issue