Skip to content

Commit

Permalink
Move panic handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MDrakos committed Nov 5, 2024
1 parent bbfb2fa commit fe8713a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
5 changes: 5 additions & 0 deletions cmd/state-svc/internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"runtime/debug"
"sync"
"time"

Expand All @@ -15,6 +16,7 @@ import (
"github.com/ActiveState/cli/internal/httputil"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/poller"
"github.com/ActiveState/cli/internal/runbits/panics"
"github.com/ActiveState/cli/internal/strutils"
auth "github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/sysinfo"
Expand Down Expand Up @@ -43,6 +45,9 @@ func New(cfg *config.Instance, auth *auth.Auth) (*Messages, error) {
}

poll := poller.New(1*time.Hour, func() (interface{}, error) {
defer func() {
panics.LogAndPanic(recover(), debug.Stack())
}()
resp, err := fetch()
return resp, err
})
Expand Down
6 changes: 6 additions & 0 deletions cmd/state-svc/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Res

upchecker := updater.NewDefaultChecker(cfg, an)
pollUpdate := poller.New(1*time.Hour, func() (interface{}, error) {
defer func() {
panics.LogAndPanic(recover(), debug.Stack())
}()
logging.Debug("Poller checking for update info")
return upchecker.CheckFor(constants.ChannelName, "")
})
Expand All @@ -71,6 +74,9 @@ func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Res
}

pollAuth := poller.New(time.Duration(int64(time.Millisecond)*pollRate), func() (interface{}, error) {
defer func() {
panics.LogAndPanic(recover(), debug.Stack())
}()
if auth.SyncRequired() {
return nil, auth.Sync()
}
Expand Down
11 changes: 1 addition & 10 deletions internal/poller/poller.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package poller

import (
"runtime/debug"
"sync"
"time"

"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/runbits/errors"
"github.com/ActiveState/cli/internal/runbits/panics"
)

type Poller struct {
Expand All @@ -21,15 +19,8 @@ type Poller struct {
}

func New(interval time.Duration, pollFunc func() (interface{}, error)) *Poller {
wrappedFn := func() (interface{}, error) {
defer func() {
panics.LogAndPanic(recover(), debug.Stack())
}()
return pollFunc()
}

p := &Poller{
pollFunc: wrappedFn,
pollFunc: pollFunc,
done: make(chan struct{}),
}
go p.start(interval)
Expand Down

0 comments on commit fe8713a

Please sign in to comment.