Correct handling of state and console events

This commit is contained in:
Dane Everitt 2019-05-27 17:12:51 -07:00
parent 7b1ebd790d
commit 93c71a6ab7
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 14 additions and 19 deletions

View File

@ -120,7 +120,7 @@ func (d *DockerEnvironment) Start() error {
// No reason to try starting a container that is already running.
if c.State.Running {
d.Server.Emit(StatusEvent, ProcessRunningState)
d.Server.SetState(ProcessRunningState)
if !d.attached {
return d.Attach()
}
@ -134,18 +134,18 @@ func (d *DockerEnvironment) Start() error {
return err
}
d.Server.Emit(StatusEvent, ProcessStartingState)
d.Server.SetState(ProcessStartingState)
// Reset the permissions on files for the server before actually trying
// to start it.
if err := d.Server.Filesystem.Chown("/"); err != nil {
d.Server.Emit(StatusEvent, ProcessOfflineState)
d.Server.SetState(ProcessOfflineState)
return err
}
opts := types.ContainerStartOptions{}
if err := d.Client.ContainerStart(context.Background(), d.Server.Uuid, opts); err != nil {
d.Server.Emit(StatusEvent, ProcessOfflineState)
d.Server.SetState(ProcessOfflineState)
return err
}
@ -158,7 +158,7 @@ func (d *DockerEnvironment) Start() error {
func (d *DockerEnvironment) Stop() error {
t := time.Second * 10
d.Server.Emit(StatusEvent, ProcessStoppingState)
d.Server.SetState(ProcessStoppingState)
return d.Client.ContainerStop(context.Background(), d.Server.Uuid, &t)
}
@ -175,7 +175,7 @@ func (d *DockerEnvironment) Terminate(signal os.Signal) error {
return nil
}
d.Server.Emit(StatusEvent, ProcessStoppingState)
d.Server.SetState(ProcessStoppingState)
return d.Client.ContainerKill(ctx, d.Server.Uuid, "SIGKILL")
}
@ -211,7 +211,7 @@ func (d *DockerEnvironment) Attach() error {
go func() {
defer d.stream.Close()
defer func() {
d.Server.Emit(StatusEvent, ProcessOfflineState)
d.Server.SetState(ProcessOfflineState)
d.attached = false
}()

View File

@ -6,7 +6,7 @@ type EventListenerFunction *func(string)
// Defines all of the possible output events for a server.
const (
ConsoleOutputEvent = "console"
ConsoleOutputEvent = "console output"
StatusEvent = "status"
)

View File

@ -236,19 +236,14 @@ const (
// Sets the state of the server internally. This function handles crash detection as
// well as reporting to event listeners for the server.
func (s *Server) SetState(state string) error {
switch state {
case ProcessOfflineState:
case ProcessStartingState:
case ProcessRunningState:
case ProcessStoppingState:
s.State = state
break
default:
if state != ProcessOfflineState && state != ProcessStartingState && state != ProcessRunningState && state != ProcessStoppingState {
return errors.New(fmt.Sprintf("invalid server state received: %s", state))
}
s.State = state
// Emit the event to any listeners that are currently registered.
s.Emit(StatusEvent, state)
s.Emit(StatusEvent, s.State)
// @todo handle a crash event here. Need to port the logic from the Nodejs daemon
// into this daemon. I believe its basically just if state != stopping && newState = stopped