Skip to content

Commit

Permalink
Merge pull request #426 from overmindtech/signalhandling
Browse files Browse the repository at this point in the history
Improved signal handling
  • Loading branch information
DavidS-ovm authored Jun 21, 2024
2 parents bf175d8 + 2160413 commit 92c566a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 23 deletions.
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ func Execute() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

// Create a goroutine to watch for cancellation signals
// Create a goroutine to watch for cancellation signals and aborting the
// running command. Note that bubbletea converts ^C to a Quit message,
// so we also need to handle that, but we still need to deal with the
// regular signals.
go func() {
select {
case <-sigs:
log.Info("Received signal, shutting down")
cancel()
case <-ctx.Done():
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/tea_ensuretoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ func getOauthToken(ctx context.Context, oi OvermindInstance, requiredScopes []st
os.Exit(1)
}

if m.token == nil {
fmt.Println("Error running program: no token received")
os.Exit(1)
}
tok, err := josejwt.ParseSigned(m.token.AccessToken, []jose.SignatureAlgorithm{jose.RS256})
if err != nil {
fmt.Println("Error running program: received invalid token:", err)
Expand Down
19 changes: 0 additions & 19 deletions cmd/tea_taskmodel.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
package cmd

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

// waitForCancellation returns a tea.Cmd that will wait for SIGINT and SIGTERM and run the provided cancel on receipt.
func waitForCancellation(ctx context.Context, cancel context.CancelFunc) tea.Cmd {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

return func() tea.Msg {
select {
case <-sigs:
cancel()
case <-ctx.Done():
}
return tea.Quit
}
}

type taskStatus int

const (
Expand Down
2 changes: 0 additions & 2 deletions cmd/tea_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (m *cmdModel) Init() tea.Cmd {
// don't init sources on test-fake runs
// m.tasks["02_config"] = NewInitialiseSourcesModel()
return tea.Batch(
waitForCancellation(m.ctx, m.cancel),
m.tasks["00_oi"].Init(),
m.tasks["01_token"].Init(),
// m.tasks["02_config"].Init(),
Expand All @@ -95,7 +94,6 @@ func (m *cmdModel) Init() tea.Cmd {
m.tasks["02_config"] = NewInitialiseSourcesModel(m.width)

return tea.Batch(
waitForCancellation(m.ctx, m.cancel),
m.tasks["00_oi"].Init(),
m.tasks["01_token"].Init(),
m.tasks["02_config"].Init(),
Expand Down
2 changes: 1 addition & 1 deletion tracing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func InitTracer(opts ...otlptracehttp.Option) error {
// setup recovery for an unexpected panic in this function
defer sentry.Flush(2 * time.Second)
defer sentry.Recover()
log.Info("sentry configured")
log.Debug("sentry configured")
}

client := otlptracehttp.NewClient(opts...)
Expand Down

0 comments on commit 92c566a

Please sign in to comment.