replace servers.servers singleton with server.Manager
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -15,26 +16,21 @@ import (
|
||||
"github.com/pterodactyl/wings/config"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"github.com/pterodactyl/wings/environment/docker"
|
||||
"github.com/pterodactyl/wings/panelapi"
|
||||
"github.com/pterodactyl/wings/server/filesystem"
|
||||
)
|
||||
|
||||
var servers = NewCollection(nil)
|
||||
|
||||
func GetServers() *Collection {
|
||||
return servers
|
||||
}
|
||||
|
||||
// Iterates over a given directory and loads all of the servers listed before returning
|
||||
// them to the calling function.
|
||||
func LoadDirectory() error {
|
||||
if len(servers.items) != 0 {
|
||||
func (m *manager) Initialize(serversPerPage int) error {
|
||||
if len(m.servers.items) != 0 {
|
||||
return errors.New("cannot call LoadDirectory with a non-nil collection")
|
||||
}
|
||||
|
||||
log.Info("fetching list of servers from API")
|
||||
configs, err := api.New().GetServers()
|
||||
assignedServers, err := m.panelClient.GetServers(context.TODO(), serversPerPage)
|
||||
if err != nil {
|
||||
if !api.IsRequestError(err) {
|
||||
if !panelapi.IsRequestError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -42,13 +38,11 @@ func LoadDirectory() error {
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
log.WithField("total_configs", len(configs)).Info("processing servers returned by the API")
|
||||
log.WithField("total_configs", len(assignedServers)).Info("processing servers returned by the API")
|
||||
|
||||
pool := workerpool.New(runtime.NumCPU())
|
||||
log.Debugf("using %d workerpools to instantiate server instances", runtime.NumCPU())
|
||||
for _, data := range configs {
|
||||
data := data
|
||||
|
||||
for _, data := range assignedServers {
|
||||
pool.Submit(func() {
|
||||
// Parse the json.RawMessage into an expected struct value. We do this here so that a single broken
|
||||
// server does not cause the entire boot process to hang, and allows us to show more useful error
|
||||
@@ -69,7 +63,7 @@ func LoadDirectory() error {
|
||||
return
|
||||
}
|
||||
|
||||
servers.Add(s)
|
||||
m.Add(s)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
46
server/manager.go
Normal file
46
server/manager.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/pterodactyl/wings/panelapi"
|
||||
)
|
||||
|
||||
type Manager interface {
|
||||
// Initialize fetches all servers assigned to this node from the API.
|
||||
Initialize(serversPerPage int) error
|
||||
GetAll() []*Server
|
||||
Get(uuid string) *Server
|
||||
Add(s *Server)
|
||||
Remove(s *Server)
|
||||
}
|
||||
|
||||
type manager struct {
|
||||
servers Collection
|
||||
|
||||
panelClient panelapi.Client
|
||||
}
|
||||
|
||||
// NewManager creates a new server manager.
|
||||
func NewManager(panelClient panelapi.Client) Manager {
|
||||
return &manager{panelClient: panelClient}
|
||||
}
|
||||
|
||||
func (m *manager) GetAll() []*Server {
|
||||
return m.servers.items
|
||||
}
|
||||
|
||||
func (m *manager) Get(uuid string) *Server {
|
||||
return m.servers.Find(func(s *Server) bool {
|
||||
return s.Id() == uuid
|
||||
})
|
||||
}
|
||||
|
||||
func (m *manager) Add(s *Server) {
|
||||
s.manager = m
|
||||
m.servers.Add(s)
|
||||
}
|
||||
|
||||
func (m *manager) Remove(s *Server) {
|
||||
m.servers.Remove(func(sf *Server) bool {
|
||||
return sf.Id() == s.Id()
|
||||
})
|
||||
}
|
||||
@@ -27,6 +27,9 @@ type Server struct {
|
||||
ctx context.Context
|
||||
ctxCancel *context.CancelFunc
|
||||
|
||||
// manager holds a reference to the manager responsible for the server
|
||||
manager *manager
|
||||
|
||||
emitterLock sync.Mutex
|
||||
powerLock *semaphore.Weighted
|
||||
throttleOnce sync.Once
|
||||
|
||||
@@ -36,10 +36,10 @@ func CachedServerStates() (map[string]string, error) {
|
||||
}
|
||||
|
||||
// saveServerStates .
|
||||
func saveServerStates() error {
|
||||
func (m *manager) saveServerStates() error {
|
||||
// Get the states of all servers on the daemon.
|
||||
states := map[string]string{}
|
||||
for _, s := range GetServers().All() {
|
||||
for _, s := range m.GetAll() {
|
||||
states[s.Id()] = s.Environment.State()
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ func (s *Server) OnStateChange() {
|
||||
// We also get the benefit of server status changes always propagating corrected configurations
|
||||
// to the disk should we forget to do it elsewhere.
|
||||
go func() {
|
||||
if err := saveServerStates(); err != nil {
|
||||
if err := s.manager.saveServerStates(); err != nil {
|
||||
s.Log().WithField("error", err).Warn("failed to write server states to disk")
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user