43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
|
package docker
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/pkg/errors"
|
||
|
"github.com/pterodactyl/wings/environment"
|
||
|
"github.com/pterodactyl/wings/system"
|
||
|
)
|
||
|
|
||
|
// Returns the current environment state.
|
||
|
func (e *Environment) State() string {
|
||
|
e.stMu.RLock()
|
||
|
defer e.stMu.RUnlock()
|
||
|
|
||
|
return e.st
|
||
|
}
|
||
|
|
||
|
// 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) error {
|
||
|
if state != system.ProcessOfflineState &&
|
||
|
state != system.ProcessStartingState &&
|
||
|
state != system.ProcessRunningState &&
|
||
|
state != system.ProcessStoppingState {
|
||
|
return errors.New(fmt.Sprintf("invalid server state received: %s", state))
|
||
|
}
|
||
|
|
||
|
// Get the current state of the environment before changing it.
|
||
|
prevState := e.State()
|
||
|
|
||
|
// Emit the event to any listeners that are currently registeree.
|
||
|
if prevState != state {
|
||
|
// If the state changed make sure we update the internal tracking to note that.
|
||
|
e.stMu.Lock()
|
||
|
e.st = state
|
||
|
e.stMu.Unlock()
|
||
|
|
||
|
e.Events().Publish(environment.StateChangeEvent, e.State())
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|