From 93c71a6ab7665f4b457f6ed2a0747fec0a94e92b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 27 May 2019 17:12:51 -0700 Subject: [PATCH] Correct handling of state and console events --- server/environment_docker.go | 14 +++++++------- server/events.go | 2 +- server/server.go | 17 ++++++----------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/server/environment_docker.go b/server/environment_docker.go index f197b54..2e924d9 100644 --- a/server/environment_docker.go +++ b/server/environment_docker.go @@ -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 }() diff --git a/server/events.go b/server/events.go index f73ad73..1a0c9e4 100644 --- a/server/events.go +++ b/server/events.go @@ -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" ) diff --git a/server/server.go b/server/server.go index 29874b3..0eabedb 100644 --- a/server/server.go +++ b/server/server.go @@ -201,7 +201,7 @@ func FromConfiguration(data []byte, cfg *config.SystemConfiguration) (*Server, e s.Cache = cache.New(time.Minute*10, time.Minute*15) s.Filesystem = &Filesystem{ Configuration: cfg, - Server: s, + Server: s, } return s, nil @@ -236,23 +236,18 @@ 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 // then crashed. return nil -} \ No newline at end of file +}