From 7c0acf3f60d3032345a94bbe339b3af5f019a190 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 7 Apr 2019 15:20:21 -0700 Subject: [PATCH] Very basic state management for servers --- server/server.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index ee32a6c..9ff38f3 100644 --- a/server/server.go +++ b/server/server.go @@ -2,6 +2,7 @@ package server import ( "github.com/patrickmn/go-cache" + "github.com/pkg/errors" "github.com/pterodactyl/wings/config" "github.com/remeh/sizedwaitgroup" "go.uber.org/zap" @@ -13,6 +14,16 @@ import ( "time" ) +// Defines states that identify if the server is running or not. +const ( + StateOffline = "off" + StateStarting = "starting" + StateRunning = "running" + StateStopping = "stopping" +) + +type ProcessState string + // High level definition for a server instance being controlled by Wings. type Server struct { // The unique identifier for the server that should be used when referencing @@ -25,7 +36,7 @@ type Server struct { Suspended bool `json:"suspended"` // The power state of the server. - State int `json:"state"` + State ProcessState `json:"state"` // The command that should be used when booting up the server instance. Invocation string `json:"invocation"` @@ -219,6 +230,22 @@ func (s *Server) Cache() *cache.Cache { return s.cache } +// Sets the state of the server process. +func (s *Server) SetState(state ProcessState) error { + switch state { + case StateOffline: + case StateRunning: + case StateStarting: + case StateStopping: + s.State = state + break + default: + return errors.New("invalid state provided") + } + + return nil +} + // Determine if the server is bootable in it's current state or not. This will not // indicate why a server is not bootable, only if it is. func (s *Server) IsBootable() bool {