Fix state handing logic
This commit is contained in:
parent
b73f78a832
commit
2bf293823f
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -14,41 +15,14 @@ const stateFileLocation = "data/.states.json"
|
||||||
|
|
||||||
var stateMutex sync.Mutex
|
var stateMutex sync.Mutex
|
||||||
|
|
||||||
// Checks if the state tracking file exists, if not it is generated.
|
|
||||||
func ensureStateFileExists() (bool, error) {
|
|
||||||
stateMutex.Lock()
|
|
||||||
defer stateMutex.Unlock()
|
|
||||||
|
|
||||||
if _, err := os.Stat(stateFileLocation); err != nil {
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return false, errors.WithStack(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the state of the servers.
|
// Returns the state of the servers.
|
||||||
func getServerStates() (map[string]string, error) {
|
func getServerStates() (map[string]string, error) {
|
||||||
// Check if the states file exists.
|
|
||||||
exists, err := ensureStateFileExists()
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.WithStack(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Request a lock after we check if the file exists.
|
// Request a lock after we check if the file exists.
|
||||||
stateMutex.Lock()
|
stateMutex.Lock()
|
||||||
defer stateMutex.Unlock()
|
defer stateMutex.Unlock()
|
||||||
|
|
||||||
// Return an empty map if the file does not exist.
|
|
||||||
if !exists {
|
|
||||||
return map[string]string{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open the states file.
|
// Open the states file.
|
||||||
f, err := os.Open(stateFileLocation)
|
f, err := os.OpenFile(stateFileLocation, os.O_RDONLY|os.O_CREATE, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.WithStack(err)
|
return nil, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
@ -56,7 +30,7 @@ func getServerStates() (map[string]string, error) {
|
||||||
|
|
||||||
// Convert the json object to a map.
|
// Convert the json object to a map.
|
||||||
states := map[string]string{}
|
states := map[string]string{}
|
||||||
if err := json.NewDecoder(f).Decode(&states); err != nil {
|
if err := json.NewDecoder(f).Decode(&states); err != nil && err != io.EOF {
|
||||||
return nil, errors.WithStack(err)
|
return nil, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user