frank 38308d48f2
feat_: log on panic (#5849)
* feat_: log error and stacktrace when panic in goroutine

* test_: add test TestSafeGo

* chore_: rename logAndCall to call

* chore_: rename SafeGo to Go

* chore_: make lint-fix

* chore_: use t.Cleanup

* chore_: Revert "chore_: use t.Cleanup"

This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819.

* chore_: Revert "chore_: make lint-fix"

This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab.

* chore_: Revert "chore_: rename SafeGo to Go"

This reverts commit a6d73d6df583f313032d79aac62f66328039cb55.

* chore_: Revert "chore_: rename logAndCall to call"

This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc.

* chore_: Revert "test_: add test TestSafeGo"

This reverts commit a1fa91839f3960398980c6bf456e6462ec944819.

* chore_: Revert "feat_: log error and stacktrace when panic in goroutine"

This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a.

* feat_: log error and stacktrace when panic in goroutine

* chore_: make lint-fix

* chore_: rename logAndCall to call

* chore_: renaming LogOnPanic

* chore_: update rest goroutine function calls

* chore_: make lint-fix
2024-09-27 06:37:32 +08:00

102 lines
2.5 KiB
Go

package updates
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/hashicorp/go-version"
"go.uber.org/zap"
"github.com/ethereum/go-ethereum/log"
gocommon "github.com/status-im/status-go/common"
"github.com/status-im/status-go/services/ens"
"github.com/status-im/status-go/signal"
)
func NewAPI(ensService *ens.Service) *API {
return &API{
ensService: ensService,
httpClient: &http.Client{Timeout: time.Minute},
}
}
type API struct {
ensService *ens.Service
httpClient *http.Client
}
func (api *API) Check(ctx context.Context, chainID uint64, ens string, currentVersion string) {
go func() {
defer gocommon.LogOnPanic()
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
current, err := version.NewVersion(currentVersion)
if err != nil {
log.Error("invalid current version", "err", err)
return
}
uri, err := api.ensService.API().ResourceURL(ctx, chainID, ens)
if err != nil || uri.Host == "" {
log.Error("can't get obtain the updates content hash url", "ens", ens)
signal.SendUpdateAvailable(false, "", "")
return
}
url := uri.Scheme + "://" + uri.Host + uri.Path
versionURL := url + "VERSION"
response, err := api.httpClient.Get(versionURL)
if err != nil {
log.Error("can't get content", zap.String("any", versionURL))
signal.SendUpdateAvailable(false, "", "")
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
log.Error(fmt.Sprintf("version verification response status error: %v", response.StatusCode))
signal.SendUpdateAvailable(false, "", "")
return
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Error("version verification body err", "err", err)
signal.SendUpdateAvailable(false, "", "")
return
}
c := make(map[string]interface{})
err = json.Unmarshal(data, &c)
if err != nil {
log.Error("invalid json", "err", err)
signal.SendUpdateAvailable(false, "", "")
return
}
latestStr := ""
switch c["version"].(type) {
case string:
latestStr = c["version"].(string)
default:
log.Error("invalid latest version", "val", c["version"])
signal.SendUpdateAvailable(false, "", "")
return
}
latest, err := version.NewVersion(latestStr)
if err != nil {
log.Error("invalid latest version", "err", err)
signal.SendUpdateAvailable(false, "", "")
return
}
signal.SendUpdateAvailable(latest.GreaterThan(current), latestStr, url)
}()
}