Improve server state logical handling; allow setting state directly on the environment
This commit is contained in:
@@ -3,7 +3,9 @@ package server
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"github.com/pterodactyl/wings/system"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Defines the current resource usage for a given server instance. If a server is offline you
|
||||
@@ -16,7 +18,7 @@ type ResourceUsage struct {
|
||||
environment.Stats
|
||||
|
||||
// The current server status.
|
||||
State string `json:"state" default:"offline"`
|
||||
State system.AtomicString `json:"state"`
|
||||
|
||||
// The current disk space being used by the server. This value is not guaranteed to be accurate
|
||||
// at all times. It is "manually" set whenever server.Proc() is called. This is kind of just a
|
||||
@@ -24,16 +26,16 @@ type ResourceUsage struct {
|
||||
Disk int64 `json:"disk_bytes"`
|
||||
}
|
||||
|
||||
// Alias the resource usage so that we don't infinitely recurse when marshaling the struct.
|
||||
type IResourceUsage ResourceUsage
|
||||
|
||||
// Custom marshaler to ensure that the object is locked when we're converting it to JSON in
|
||||
// order to avoid race conditions.
|
||||
func (ru *ResourceUsage) MarshalJSON() ([]byte, error) {
|
||||
ru.mu.Lock()
|
||||
defer ru.mu.Unlock()
|
||||
|
||||
return json.Marshal(IResourceUsage(*ru))
|
||||
// Alias the resource usage so that we don't infinitely recurse when marshaling the struct.
|
||||
type alias ResourceUsage
|
||||
|
||||
return json.Marshal(alias(*ru))
|
||||
}
|
||||
|
||||
// Returns the resource usage stats for the server instance. If the server is not running, only the
|
||||
@@ -42,10 +44,10 @@ func (ru *ResourceUsage) MarshalJSON() ([]byte, error) {
|
||||
//
|
||||
// When a process is stopped all of the stats are zeroed out except for the disk.
|
||||
func (s *Server) Proc() *ResourceUsage {
|
||||
s.resources.SetDisk(s.Filesystem().CachedUsage())
|
||||
// Store the updated disk usage when requesting process usage.
|
||||
atomic.StoreInt64(&s.resources.Disk, s.Filesystem().CachedUsage())
|
||||
|
||||
// Get a read lock on the resources at this point. Don't do this before setting
|
||||
// the disk, otherwise you'll cause a deadlock.
|
||||
// Acquire a lock before attempting to return the value of resources.
|
||||
s.resources.mu.RLock()
|
||||
defer s.resources.mu.RUnlock()
|
||||
|
||||
@@ -57,24 +59,3 @@ func (s *Server) emitProcUsage() {
|
||||
s.Log().WithField("error", err).Warn("error while emitting server resource usage to listeners")
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the servers current state.
|
||||
func (ru *ResourceUsage) getInternalState() string {
|
||||
ru.mu.RLock()
|
||||
defer ru.mu.RUnlock()
|
||||
|
||||
return ru.State
|
||||
}
|
||||
|
||||
// Sets the new state for the server.
|
||||
func (ru *ResourceUsage) setInternalState(state string) {
|
||||
ru.mu.Lock()
|
||||
ru.State = state
|
||||
ru.mu.Unlock()
|
||||
}
|
||||
|
||||
func (ru *ResourceUsage) SetDisk(i int64) {
|
||||
ru.mu.Lock()
|
||||
ru.Disk = i
|
||||
ru.mu.Unlock()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user