Skip to content

Commit

Permalink
Fix issue with restarting app after SSH
Browse files Browse the repository at this point in the history
  • Loading branch information
robgonnella committed Dec 27, 2023
1 parent e227182 commit f1adaaf
Showing 1 changed file with 49 additions and 62 deletions.
111 changes: 49 additions & 62 deletions internal/ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type view struct {
eventManager event.Manager
eventUpdateChan chan event.Event
serverUpdateChan chan event.Event
errorListener chan event.Event
eventListenerIDs []int
prevFocusedName string
focusedName string
Expand Down Expand Up @@ -118,10 +119,16 @@ func (v *view) initialize(

v.serverUpdateChan = make(chan event.Event)
v.eventUpdateChan = make(chan event.Event)
v.errorListener = make(chan event.Event)
v.eventListenerIDs = []int{}

v.focusedName = "servers"

v.bindKeys()
v.registerEventListeners()
v.processBackgroundEventUpdates()
v.processErrorEvents()

go func() {
for _, o := range options {
o(v)
Expand Down Expand Up @@ -328,7 +335,7 @@ func (v *view) showFatalErrorModal(errMsg string) {
}

func (v *view) dismissFatalErrorModal() {
v.fullRestart()
v.restart()
}

// binds global key handlers
Expand Down Expand Up @@ -438,46 +445,18 @@ func (v *view) onSSH(ip string) {
ip,
)

restoreStdout()

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin

err := cmd.Run()

if err != nil {
v.partialRestart(
withShowError("failed to ssh to " + ip + ": " + err.Error()),
)
v.restart(withShowError("failed to ssh to " + ip + ": " + err.Error()))
return
}

v.partialRestart()
}

// handle incoming events
func (v *view) processBackgroundEventUpdates() {
go func() {
for {
select {
case evt, ok := <-v.eventUpdateChan:
if !ok {
return
}
v.app.QueueUpdateDraw(func() {
v.eventTable.UpdateTable(evt)
})
case evt, ok := <-v.serverUpdateChan:
if !ok {
return
}
v.app.QueueUpdateDraw(func() {
v.serverTable.UpdateTable(evt)
})
}
}
}()
v.restart()
}

// maps names to primitives for focusing
Expand Down Expand Up @@ -508,28 +487,8 @@ func (v *view) stop() {
go v.appCore.Stop()
}

// restarts the ui
func (v *view) partialRestart(options ...viewOption) {
v.stop()

allConfigs, err := v.appCore.GetConfigs()

if err != nil {
v.log.Fatal().Err(err).Msg("failed to retrieve configs")
}

maskStdout()

v.initialize(allConfigs, options...)

if err := v.run(); err != nil {
restoreStdout()
v.log.Fatal().Err(err).Msg("failed to restart view")
}
}

// restarts the entire application including re-instantiation of entire backend
func (v *view) fullRestart(options ...viewOption) {
func (v *view) restart(options ...viewOption) {
v.stop()

restoreStdout()
Expand Down Expand Up @@ -570,10 +529,7 @@ func (v *view) fullRestart(options ...viewOption) {
}
}

// sets up global key handlers, registers event listeners, sets up processing
// for channel updates, starts backend processes, and starts terminal ui
func (v *view) run() error {
v.bindKeys()
func (v *view) registerEventListeners() {
v.eventListenerIDs = append(
v.eventListenerIDs,
v.eventManager.RegisterListener(discovery.DiscoveryArpUpdateEvent, v.eventUpdateChan),
Expand All @@ -584,16 +540,41 @@ func (v *view) run() error {
v.eventManager.RegisterListener(discovery.DiscoveryArpUpdateEvent, v.serverUpdateChan),
v.eventManager.RegisterListener(discovery.DiscoverySynUpdateEvent, v.serverUpdateChan),
)
errorListener := make(chan event.Event)
v.eventListenerIDs = append(
v.eventListenerIDs,
v.eventManager.RegisterListener(event.ErrorEventType, errorListener),
v.eventManager.RegisterListener(event.FatalErrorEventType, errorListener),
v.eventManager.RegisterListener(event.ErrorEventType, v.errorListener),
v.eventManager.RegisterListener(event.FatalErrorEventType, v.errorListener),
)
v.processBackgroundEventUpdates()
v.appCore.StartDaemon()
}

// handle incoming server events
func (v *view) processBackgroundEventUpdates() {
go func() {
for evt := range errorListener {
for {
select {
case evt, ok := <-v.eventUpdateChan:
if !ok {
return
}
v.app.QueueUpdateDraw(func() {
v.eventTable.UpdateTable(evt)
})
case evt, ok := <-v.serverUpdateChan:
if !ok {
return
}
v.app.QueueUpdateDraw(func() {
v.serverTable.UpdateTable(evt)
})
}
}
}()
}

// handle incoming error events
func (v *view) processErrorEvents() {
go func() {
for evt := range v.errorListener {
switch evt.Type {
case event.FatalErrorEventType:
v.showFatalErrorModal(evt.Payload.(error).Error())
Expand All @@ -604,5 +585,11 @@ func (v *view) run() error {
}
}
}()
}

// sets up global key handlers, registers event listeners, sets up processing
// for channel updates, starts backend processes, and starts terminal ui
func (v *view) run() error {
v.appCore.StartDaemon()
return v.app.SetRoot(v.root, true).EnableMouse(true).Run()
}

0 comments on commit f1adaaf

Please sign in to comment.