Skip to content

Commit

Permalink
Handle start-up permissions error
Browse files Browse the repository at this point in the history
  • Loading branch information
robgonnella committed Sep 30, 2023
1 parent 57cc8db commit fa23d9f
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ sudo go run main.go
sudo go run main.go --debug
```

- clear database file and log file
- clear config file and log file

```bash
./build/ops clear
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ will fail to start.
sudo ops
```

- clear database file and log file
- clear config file and log file

```bash
sudo ops clear
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

/**
* Command to remove database and log files
* Command to remove config and log files
*/
func clear() *cobra.Command {
cmd := &cobra.Command{
Use: "clear",
Short: "Clears database and log files",
Short: "Clears config and log files",
RunE: func(cmd *cobra.Command, args []string) error {
log := logger.New()

Expand Down
17 changes: 11 additions & 6 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/robgonnella/ops/internal/logger"
)

// EventListener represents a registered listener for database events
// EventListener represents a registered listener for events
type EventListener struct {
id int
channel chan *event.Event
Expand Down Expand Up @@ -80,7 +80,7 @@ func (c *Core) NetworkInfo() network.NetworkInfo {
return *c.networkInfo
}

// CreateConfig creates a new config in the database
// CreateConfig creates a new config
func (c *Core) CreateConfig(conf config.Config) error {
_, err := c.configService.Create(&conf)

Expand All @@ -91,7 +91,7 @@ func (c *Core) CreateConfig(conf config.Config) error {
return nil
}

// UpdateConfig updates an existing config in the database
// UpdateConfig updates an existing config
func (c *Core) UpdateConfig(conf config.Config) error {
updated, err := c.configService.Update(&conf)

Expand Down Expand Up @@ -128,15 +128,20 @@ func (c *Core) GetConfigs() ([]*config.Config, error) {
}

// StartDaemon starts the network monitoring processes in a goroutine
func (c *Core) StartDaemon() {
func (c *Core) StartDaemon(errorReporter chan error) {
go func() {
if err := c.Monitor(); err != nil {
c.errorChan <- err
go func() {
c.errorChan <- err
}()
go func() {
errorReporter <- err
}()
}
}()
}

// RegisterEventListener registers a channel as a listener for database events
// RegisterEventListener registers a channel as a listener for events
func (c *Core) RegisterEventListener(channel chan *event.Event) int {
c.mux.Lock()
defer c.mux.Unlock()
Expand Down
7 changes: 6 additions & 1 deletion internal/core/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ func (c *Core) Monitor() error {

// Start network scanner
go func() {
c.discovery.MonitorNetwork()
if err := c.discovery.MonitorNetwork(); err != nil {
// bail with error
c.errorChan <- err
return
}

// bail if network monitoring stops
done <- true
}()
Expand Down
2 changes: 1 addition & 1 deletion internal/discovery/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ type Scanner interface {

// Service interface for monitoring a network
type Service interface {
MonitorNetwork()
MonitorNetwork() error
Stop()
}
10 changes: 5 additions & 5 deletions internal/discovery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func NewScannerService(
}

// MonitorNetwork polls the network to discover and track devices
func (s *ScannerService) MonitorNetwork() {
func (s *ScannerService) MonitorNetwork() error {
s.log.Info().Msg("Starting network discovery")

// blocking call that continuously scans the network on an interval
s.pollNetwork()
return s.pollNetwork()
}

// Stop stop network discover. Once called this service will be useless.
Expand All @@ -72,7 +72,7 @@ func (s *ScannerService) Stop() {

// private
// make polling calls to scanner.Scan()
func (s *ScannerService) pollNetwork() {
func (s *ScannerService) pollNetwork() error {
ticker := time.NewTicker(time.Second * 30)

// start first scan
Expand All @@ -88,7 +88,7 @@ func (s *ScannerService) pollNetwork() {
case <-s.ctx.Done():
s.log.Info().Msg("Network polling stopped")
ticker.Stop()
return
return s.ctx.Err()
case r := <-s.resultChan:
switch r.Type {
case scanner.ARPResult:
Expand Down Expand Up @@ -126,7 +126,7 @@ func (s *ScannerService) pollNetwork() {
}
case err := <-s.errorChan:
s.log.Error().Err(err).Msg("discovery service encountered an error")
return
return err
case <-ticker.C:
// always scan in goroutine to prevent blocking result channel
go func() {
Expand Down
2 changes: 1 addition & 1 deletion internal/exception/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package exception

import "errors"

// ErrRecordNotFound custom database error for failure to find record
// ErrRecordNotFound custom error for failure to find record
var ErrRecordNotFound = errors.New("record not found")
4 changes: 1 addition & 3 deletions internal/ui/component/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func (t *ServerTable) Primitive() tview.Primitive {
return t.table
}

// UpdateTable updates the table with the incoming list of servers from
// the database. We expect these servers to always be sorted so the ordering
// should remain relatively consistent.
// UpdateTable updates the table with the incoming server from the event
func (t *ServerTable) UpdateTable(evt *event.Event) {
payload, ok := evt.Payload.(*discovery.DiscoveryResult)

Expand Down
4 changes: 1 addition & 3 deletions internal/ui/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ func (u *UI) Launch(debug bool) error {
)

if err != nil {
log.Error().Err(err).Msg("")
log.Info().Msg("disabling logs")
logger.SetGlobalLevel(zerolog.Disabled)
log.Fatal().Err(err).Msg("failed to open log file")
} else {
logger.SetGlobalLogFile(file)
logger.SetWithCaller()
Expand Down
37 changes: 34 additions & 3 deletions internal/ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,31 @@ func (v *view) dismissErrorModal() {
v.app.SetRoot(v.root, true)
}

func (v *view) showFatalErrorModal(err error) {
dismiss := func() {
v.dismissFatalErrorModal(err)
}

buttons := []component.ModalButton{
{
Label: "Exit",
OnClick: dismiss,
},
}

errorModal := component.NewModal(
err.Error(),
buttons,
)

v.app.SetRoot(errorModal.Primitive(), false)
}

func (v *view) dismissFatalErrorModal(err error) {
v.app.SetRoot(v.root, true)
v.stop()
}

// binds global key handlers
func (v *view) bindKeys() {
v.app.SetInputCapture(func(evt *tcell.EventKey) *tcell.EventKey {
Expand Down Expand Up @@ -338,7 +363,7 @@ func (v *view) focus(name string) {

if err != nil {
v.log.Error().Err(err).Msg("")
v.showErrorModal("Failed to retrieve configurations from database")
v.showErrorModal("Failed to retrieve configurations")
return
}

Expand Down Expand Up @@ -415,7 +440,7 @@ func (v *view) onSSH(ip string) {
v.restart()
}

// handle incoming database events
// handle incoming events
func (v *view) processBackgroundEventUpdates() {
go func() {
for {
Expand Down Expand Up @@ -521,6 +546,12 @@ func (v *view) run() error {
v.appCore.RegisterEventListener(v.serverUpdateChan),
)
v.processBackgroundEventUpdates()
v.appCore.StartDaemon()
errorChan := make(chan error)
v.appCore.StartDaemon(errorChan)
go func() {
err := <-errorChan
v.showFatalErrorModal(err)
v.app.Draw()
}()
return v.app.SetRoot(v.root, true).EnableMouse(true).Run()
}

0 comments on commit fa23d9f

Please sign in to comment.