Move server tracking in program into global memory state
This commit is contained in:
39
server/collection.go
Normal file
39
server/collection.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package server
|
||||
|
||||
type Collection struct {
|
||||
items []*Server
|
||||
}
|
||||
|
||||
// Return all of the items in the collection.
|
||||
func (c *Collection) All() []*Server {
|
||||
return c.items
|
||||
}
|
||||
|
||||
// Adds an item to the collection store.
|
||||
func (c *Collection) Add(s *Server) {
|
||||
c.items = append(c.items, s)
|
||||
}
|
||||
|
||||
// Returns only those items matching the filter criteria.
|
||||
func (c *Collection) Filter(filter func (*Server) bool) []*Server {
|
||||
r := make([]*Server, 0)
|
||||
for _, v := range c.items {
|
||||
if filter(v) {
|
||||
r = append(r, v)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// Returns a single element from the collection matching the filter. If nothing is
|
||||
// found a nil result is returned.
|
||||
func (c *Collection) Find(filter func (*Server) bool) *Server {
|
||||
for _, v := range c.items {
|
||||
if filter(v) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -18,6 +18,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var servers *Collection
|
||||
|
||||
func GetServers() *Collection {
|
||||
return servers
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -136,7 +142,7 @@ type Allocations struct {
|
||||
|
||||
// Iterates over a given directory and loads all of the servers listed before returning
|
||||
// them to the calling function.
|
||||
func LoadDirectory(dir string, cfg *config.SystemConfiguration) ([]*Server, error) {
|
||||
func LoadDirectory(dir string, cfg *config.SystemConfiguration) error {
|
||||
// We could theoretically use a standard wait group here, however doing
|
||||
// that introduces the potential to crash the program due to too many
|
||||
// open files. This wouldn't happen on a small setup, but once the daemon is
|
||||
@@ -149,10 +155,10 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) ([]*Server, erro
|
||||
|
||||
f, err := ioutil.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
var servers []*Server
|
||||
servers = new(Collection)
|
||||
|
||||
for _, file := range f {
|
||||
if !strings.HasSuffix(file.Name(), ".yml") || file.IsDir() {
|
||||
@@ -177,7 +183,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) ([]*Server, erro
|
||||
return
|
||||
}
|
||||
|
||||
servers = append(servers, s)
|
||||
servers.Add(s)
|
||||
}(file)
|
||||
}
|
||||
|
||||
@@ -185,7 +191,7 @@ func LoadDirectory(dir string, cfg *config.SystemConfiguration) ([]*Server, erro
|
||||
// before continuing.
|
||||
wg.Wait()
|
||||
|
||||
return servers, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initializes the default required internal struct components for a Server.
|
||||
|
||||
Reference in New Issue
Block a user