refactor(app): add destination error events

This commit is contained in:
Rob Watson 2025-04-23 20:32:38 +02:00
parent b8550f050b
commit f4021a2886
3 changed files with 62 additions and 33 deletions

View File

@ -220,8 +220,8 @@ func (a *App) handleCommand(
URL: c.URL, URL: c.URL,
}) })
if err := a.configService.SetConfig(newCfg); err != nil { if err := a.configService.SetConfig(newCfg); err != nil {
a.logger.Error("Config update failed", "err", err) a.logger.Error("Add destination failed", "err", err)
ui.ConfigUpdateFailed(err) a.eventBus.Send(event.AddDestinationFailedEvent{Err: err})
break break
} }
a.cfg = newCfg a.cfg = newCfg
@ -234,8 +234,8 @@ func (a *App) handleCommand(
return dest.URL == c.URL return dest.URL == c.URL
}) })
if err := a.configService.SetConfig(newCfg); err != nil { if err := a.configService.SetConfig(newCfg); err != nil {
a.logger.Error("Config update failed", "err", err) a.logger.Error("Remove destination failed", "err", err)
ui.ConfigUpdateFailed(err) a.eventBus.Send(event.RemoveDestinationFailedEvent{Err: err})
break break
} }
a.cfg = newCfg a.cfg = newCfg

View File

@ -5,11 +5,13 @@ import "git.netflux.io/rob/octoplex/internal/domain"
type Name string type Name string
const ( const (
EventNameAppStateChanged Name = "app_state_changed" EventNameAppStateChanged Name = "app_state_changed"
EventNameDestinationAdded Name = "destination_added" EventNameDestinationAdded Name = "destination_added"
EventNameDestinationRemoved Name = "destination_removed" EventNameAddDestinationFailed Name = "add_destination_failed"
EventNameMediaServerStarted Name = "media_server_started" EventNameDestinationRemoved Name = "destination_removed"
EventNameFatalErrorOccurred Name = "fatal_error_occurred" EventNameRemoveDestinationFailed Name = "remove_destination_failed"
EventNameFatalErrorOccurred Name = "fatal_error_occurred"
EventNameMediaServerStarted Name = "media_server_started"
) )
// Event represents something which happened in the appllication. // Event represents something which happened in the appllication.
@ -35,6 +37,15 @@ func (e DestinationAddedEvent) name() Name {
return EventNameDestinationAdded return EventNameDestinationAdded
} }
// AddDestinationFailedEvent is emitted when a destination fails to be added.
type AddDestinationFailedEvent struct {
Err error
}
func (e AddDestinationFailedEvent) name() Name {
return EventNameAddDestinationFailed
}
// DestinationRemovedEvent is emitted when a destination is successfully // DestinationRemovedEvent is emitted when a destination is successfully
// removed. // removed.
type DestinationRemovedEvent struct { type DestinationRemovedEvent struct {
@ -45,14 +56,14 @@ func (e DestinationRemovedEvent) name() Name {
return EventNameDestinationRemoved return EventNameDestinationRemoved
} }
// MediaServerStartedEvent is emitted when the mediaserver component starts successfully. // RemoveDestinationFailedEvent is emitted when a destination fails to be
type MediaServerStartedEvent struct { // removed.
RTMPURL string type RemoveDestinationFailedEvent struct {
RTMPSURL string Err error
} }
func (e MediaServerStartedEvent) name() Name { func (e RemoveDestinationFailedEvent) name() Name {
return "media_server_started" return EventNameRemoveDestinationFailed
} }
// FatalErrorOccurredEvent is emitted when a fatal application // FatalErrorOccurredEvent is emitted when a fatal application
@ -64,3 +75,13 @@ type FatalErrorOccurredEvent struct {
func (e FatalErrorOccurredEvent) name() Name { func (e FatalErrorOccurredEvent) name() Name {
return "fatal_error_occurred" return "fatal_error_occurred"
} }
// MediaServerStartedEvent is emitted when the mediaserver component starts successfully.
type MediaServerStartedEvent struct {
RTMPURL string
RTMPSURL string
}
func (e MediaServerStartedEvent) name() Name {
return "media_server_started"
}

View File

@ -281,7 +281,9 @@ func (ui *UI) run(ctx context.Context) {
appStateChangedC := ui.eventBus.Register(event.EventNameAppStateChanged) appStateChangedC := ui.eventBus.Register(event.EventNameAppStateChanged)
destinationAddedC := ui.eventBus.Register(event.EventNameDestinationAdded) destinationAddedC := ui.eventBus.Register(event.EventNameDestinationAdded)
addDestinationFailedC := ui.eventBus.Register(event.EventNameAddDestinationFailed)
destinationRemovedC := ui.eventBus.Register(event.EventNameDestinationRemoved) destinationRemovedC := ui.eventBus.Register(event.EventNameDestinationRemoved)
removeDestinationFailedC := ui.eventBus.Register(event.EventNameRemoveDestinationFailed)
mediaServerStartedC := ui.eventBus.Register(event.EventNameMediaServerStarted) mediaServerStartedC := ui.eventBus.Register(event.EventNameMediaServerStarted)
fatalErrorOccurredC := ui.eventBus.Register(event.EventNameFatalErrorOccurred) fatalErrorOccurredC := ui.eventBus.Register(event.EventNameFatalErrorOccurred)
@ -306,10 +308,18 @@ func (ui *UI) run(ctx context.Context) {
ui.app.QueueUpdateDraw(func() { ui.app.QueueUpdateDraw(func() {
ui.handleDestinationAdded(evt.(event.DestinationAddedEvent)) ui.handleDestinationAdded(evt.(event.DestinationAddedEvent))
}) })
case evt := <-addDestinationFailedC:
ui.app.QueueUpdateDraw(func() {
ui.handleDestinationEventError(evt.(event.AddDestinationFailedEvent).Err)
})
case evt := <-destinationRemovedC: case evt := <-destinationRemovedC:
ui.app.QueueUpdateDraw(func() { ui.app.QueueUpdateDraw(func() {
ui.handleDestinationRemoved(evt.(event.DestinationRemovedEvent)) ui.handleDestinationRemoved(evt.(event.DestinationRemovedEvent))
}) })
case evt := <-removeDestinationFailedC:
ui.app.QueueUpdateDraw(func() {
ui.handleDestinationEventError(evt.(event.RemoveDestinationFailedEvent).Err)
})
case evt := <-mediaServerStartedC: case evt := <-mediaServerStartedC:
ui.app.QueueUpdateDraw(func() { ui.app.QueueUpdateDraw(func() {
ui.handleMediaServerStarted(evt.(event.MediaServerStartedEvent)) ui.handleMediaServerStarted(evt.(event.MediaServerStartedEvent))
@ -866,24 +876,6 @@ func (ui *UI) Close() {
ui.app.Stop() ui.app.Stop()
} }
func (ui *UI) ConfigUpdateFailed(err error) {
ui.app.QueueUpdateDraw(func() {
ui.showModal(
pageNameConfigUpdateFailed,
"Configuration update failed:\n\n"+err.Error(),
[]string{"Ok"},
false,
func(int, string) {
pageName, frontPage := ui.pages.GetFrontPage()
if pageName != pageNameAddDestination {
ui.logger.Warn("Unexpected page when configuration form closed", "page", pageName)
}
ui.app.SetFocus(frontPage)
},
)
})
}
func (ui *UI) addDestination() { func (ui *UI) addDestination() {
const ( const (
inputLen = 60 inputLen = 60
@ -981,6 +973,22 @@ func (ui *UI) handleDestinationRemoved(event.DestinationRemovedEvent) {
ui.selectPreviousDestination() ui.selectPreviousDestination()
} }
func (ui *UI) handleDestinationEventError(err error) {
ui.showModal(
pageNameConfigUpdateFailed,
"Configuration update failed:\n\n"+err.Error(),
[]string{"Ok"},
false,
func(int, string) {
pageName, frontPage := ui.pages.GetFrontPage()
if pageName != pageNameAddDestination {
ui.logger.Warn("Unexpected page when configuration form closed", "page", pageName)
}
ui.app.SetFocus(frontPage)
},
)
}
func (ui *UI) closeAddDestinationForm() { func (ui *UI) closeAddDestinationForm() {
var hasDestinations bool var hasDestinations bool
ui.mu.Lock() ui.mu.Lock()