Correctly handle server responses and instantiation

This commit is contained in:
Dane Everitt 2020-10-31 11:13:40 -07:00
parent 41a67933eb
commit ca3becfb55
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 18 additions and 10 deletions

View File

@ -39,15 +39,21 @@ type InstallationScript struct {
}
type allServerResponse struct {
Data []json.RawMessage `json:"data"`
Data []RawServerData `json:"data"`
Meta Pagination `json:"meta"`
}
type RawServerData struct {
Uuid string `json:"uuid"`
Settings json.RawMessage `json:"settings"`
ProcessConfiguration json.RawMessage `json:"process_configuration"`
}
// Fetches all of the server configurations from the Panel API. This will initially load the
// first 50 servers, and then check the pagination response to determine if more pages should
// be loaded. If so, those requests are spun-up in additional routines and the final resulting
// slice of all servers will be returned.
func (r *Request) GetServers() ([]json.RawMessage, error) {
func (r *Request) GetServers() ([]RawServerData, error) {
resp, err := r.Get("/servers", D{"per_page": config.Get().RemoteQuery.BootServersPerPage})
if err != nil {
return nil, errors.WithStack(err)

View File

@ -45,25 +45,27 @@ func LoadDirectory() error {
log.WithField("total_configs", len(configs)).Info("processing servers returned by the API")
pool := workerpool.New(runtime.NumCPU())
for uuid, data := range configs {
uuid := uuid
log.Debugf("using %d workerpools to instantiate server instances", runtime.NumCPU())
for _, data := range configs {
data := data
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
// messaging in the output.
d := api.ServerConfigurationResponse{}
d := api.ServerConfigurationResponse{
Settings: data.Settings,
}
log.WithField("server", uuid).Info("creating new server object from API response")
if err := json.Unmarshal(data, &d); err != nil {
log.WithField("server", uuid).WithField("error", err).Error("failed to parse server configuration from API response, skipping...")
log.WithField("server", data.Uuid).Info("creating new server object from API response")
if err := json.Unmarshal(data.ProcessConfiguration, &d.ProcessConfiguration); err != nil {
log.WithField("server", data.Uuid).WithField("error", err).Error("failed to parse server configuration from API response, skipping...")
return
}
s, err := FromConfiguration(d)
if err != nil {
log.WithField("server", uuid).WithField("error", err).Error("failed to load server, skipping...")
log.WithField("server", data.Uuid).WithField("error", err).Error("failed to load server, skipping...")
return
}