Code cleanup unrelated to changes for backup restoration

This commit is contained in:
Dane Everitt 2021-01-18 21:27:00 -08:00
parent 63dac51692
commit 5021ea6a86
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 32 additions and 34 deletions

View File

@ -2,6 +2,11 @@ package docker
import ( import (
"context" "context"
"fmt"
"io"
"sync"
"emperror.dev/errors"
"github.com/apex/log" "github.com/apex/log"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
@ -9,8 +14,6 @@ import (
"github.com/pterodactyl/wings/environment" "github.com/pterodactyl/wings/environment"
"github.com/pterodactyl/wings/events" "github.com/pterodactyl/wings/events"
"github.com/pterodactyl/wings/system" "github.com/pterodactyl/wings/system"
"io"
"sync"
) )
type Metadata struct { type Metadata struct {
@ -187,3 +190,26 @@ func (e *Environment) SetImage(i string) {
e.meta.Image = i e.meta.Image = i
} }
func (e *Environment) State() string {
return e.st.Load()
}
// SetState sets the state of the environment. This emits an event that server's
// can hook into to take their own actions and track their own state based on
// the environment.
func (e *Environment) SetState(state string) {
if state != environment.ProcessOfflineState &&
state != environment.ProcessStartingState &&
state != environment.ProcessRunningState &&
state != environment.ProcessStoppingState {
panic(errors.New(fmt.Sprintf("invalid server state received: %s", state)))
}
// Emit the event to any listeners that are currently registered.
if e.State() != state {
// If the state changed make sure we update the internal tracking to note that.
e.st.Store(state)
e.Events().Publish(environment.StateChangeEvent, state)
}
}

View File

@ -164,9 +164,10 @@ func (e *Environment) Stop() error {
return nil return nil
} }
// Attempts to gracefully stop a server using the defined stop command. If the server // WaitForStop attempts to gracefully stop a server using the defined stop
// does not stop after seconds have passed, an error will be returned, or the instance // command. If the server does not stop after seconds have passed, an error will
// will be terminated forcefully depending on the value of the second argument. // be returned, or the instance will be terminated forcefully depending on the
// value of the second argument.
func (e *Environment) WaitForStop(seconds uint, terminate bool) error { func (e *Environment) WaitForStop(seconds uint, terminate bool) error {
if err := e.Stop(); err != nil { if err := e.Stop(); err != nil {
return err return err

View File

@ -1,29 +0,0 @@
package docker
import (
"emperror.dev/errors"
"fmt"
"github.com/pterodactyl/wings/environment"
)
func (e *Environment) State() string {
return e.st.Load()
}
// Sets the state of the environment. This emits an event that server's can hook into to
// take their own actions and track their own state based on the environment.
func (e *Environment) SetState(state string) {
if state != environment.ProcessOfflineState &&
state != environment.ProcessStartingState &&
state != environment.ProcessRunningState &&
state != environment.ProcessStoppingState {
panic(errors.New(fmt.Sprintf("invalid server state received: %s", state)))
}
// Emit the event to any listeners that are currently registered.
if e.State() != state {
// If the state changed make sure we update the internal tracking to note that.
e.st.Store(state)
e.Events().Publish(environment.StateChangeEvent, state)
}
}