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

View File

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

View File

@ -201,7 +201,7 @@ func FromConfiguration(data []byte, cfg *config.SystemConfiguration) (*Server, e
s.Cache = cache.New(time.Minute*10, time.Minute*15) s.Cache = cache.New(time.Minute*10, time.Minute*15)
s.Filesystem = &Filesystem{ s.Filesystem = &Filesystem{
Configuration: cfg, Configuration: cfg,
Server: s, Server: s,
} }
return s, nil return s, nil
@ -236,23 +236,18 @@ const (
// Sets the state of the server internally. This function handles crash detection as // Sets the state of the server internally. This function handles crash detection as
// well as reporting to event listeners for the server. // well as reporting to event listeners for the server.
func (s *Server) SetState(state string) error { func (s *Server) SetState(state string) error {
switch state { if state != ProcessOfflineState && state != ProcessStartingState && state != ProcessRunningState && state != ProcessStoppingState {
case ProcessOfflineState:
case ProcessStartingState:
case ProcessRunningState:
case ProcessStoppingState:
s.State = state
break
default:
return errors.New(fmt.Sprintf("invalid server state received: %s", state)) return errors.New(fmt.Sprintf("invalid server state received: %s", state))
} }
s.State = state
// Emit the event to any listeners that are currently registered. // 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 // @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 // into this daemon. I believe its basically just if state != stopping && newState = stopped
// then crashed. // then crashed.
return nil return nil
} }