2020-08-11 04:38:42 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2020-11-08 21:52:20 +00:00
|
|
|
"emperror.dev/errors"
|
2020-08-11 04:38:42 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/pterodactyl/wings/environment"
|
|
|
|
)
|
|
|
|
|
2020-11-07 05:53:00 +00:00
|
|
|
func (e *Environment) State() string {
|
|
|
|
return e.st.Load()
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
// 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.
|
2020-11-07 05:53:00 +00:00
|
|
|
func (e *Environment) SetState(state string) {
|
2020-08-20 01:58:48 +00:00
|
|
|
if state != environment.ProcessOfflineState &&
|
|
|
|
state != environment.ProcessStartingState &&
|
|
|
|
state != environment.ProcessRunningState &&
|
|
|
|
state != environment.ProcessStoppingState {
|
2020-11-07 05:53:00 +00:00
|
|
|
panic(errors.New(fmt.Sprintf("invalid server state received: %s", state)))
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 19:08:40 +00:00
|
|
|
// Emit the event to any listeners that are currently registered.
|
2020-11-07 05:53:00 +00:00
|
|
|
if e.State() != state {
|
2020-08-11 04:38:42 +00:00
|
|
|
// If the state changed make sure we update the internal tracking to note that.
|
2020-11-07 05:53:00 +00:00
|
|
|
e.st.Store(state)
|
2020-11-07 05:14:29 +00:00
|
|
|
e.Events().Publish(environment.StateChangeEvent, state)
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
}
|