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

140 lines
4.1 KiB
Go

package community
import (
"database/sql"
"encoding/json"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
gocommon "github.com/status-im/status-go/common"
"github.com/status-im/status-go/server"
"github.com/status-im/status-go/services/wallet/thirdparty"
"github.com/status-im/status-go/services/wallet/walletevent"
)
// These events are used to notify the UI of state changes
const (
EventCommmunityDataUpdated walletevent.EventType = "wallet-community-data-updated"
)
type Manager struct {
db *DataDB
communityInfoProvider thirdparty.CommunityInfoProvider
mediaServer *server.MediaServer
feed *event.Feed
}
type Data struct {
ID string `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
Image string `json:"image,omitempty"`
}
func NewManager(db *sql.DB, mediaServer *server.MediaServer, feed *event.Feed) *Manager {
return &Manager{
db: NewDataDB(db),
mediaServer: mediaServer,
feed: feed,
}
}
// Used to break circular dependency, call once as soon as possible after initialization
func (cm *Manager) SetCommunityInfoProvider(communityInfoProvider thirdparty.CommunityInfoProvider) {
cm.communityInfoProvider = communityInfoProvider
}
func (cm *Manager) GetCommunityInfo(id string) (*thirdparty.CommunityInfo, *InfoState, error) {
communityInfo, state, err := cm.db.GetCommunityInfo(id)
if err != nil {
return nil, nil, err
}
if cm.mediaServer != nil && communityInfo != nil && len(communityInfo.CommunityImagePayload) > 0 {
communityInfo.CommunityImage = cm.GetCommunityImageURL(id)
}
return communityInfo, state, err
}
func (cm *Manager) GetCommunityID(tokenURI string) string {
return cm.communityInfoProvider.GetCommunityID(tokenURI)
}
func (cm *Manager) FillCollectiblesMetadata(communityID string, cs []*thirdparty.FullCollectibleData) (bool, error) {
return cm.communityInfoProvider.FillCollectiblesMetadata(communityID, cs)
}
func (cm *Manager) setCommunityInfo(id string, c *thirdparty.CommunityInfo) (err error) {
return cm.db.SetCommunityInfo(id, c)
}
func (cm *Manager) fetchCommunityInfo(communityID string, fetcher func() (*thirdparty.CommunityInfo, error)) (*thirdparty.CommunityInfo, error) {
communityInfo, err := fetcher()
if err != nil {
dbErr := cm.setCommunityInfo(communityID, nil)
if dbErr != nil {
log.Error("SetCommunityInfo failed", "communityID", communityID, "err", dbErr)
}
return nil, err
}
err = cm.setCommunityInfo(communityID, communityInfo)
return communityInfo, err
}
func (cm *Manager) FetchCommunityInfo(communityID string) (*thirdparty.CommunityInfo, error) {
return cm.fetchCommunityInfo(communityID, func() (*thirdparty.CommunityInfo, error) {
return cm.communityInfoProvider.FetchCommunityInfo(communityID)
})
}
func (cm *Manager) FetchCommunityMetadataAsync(communityID string) {
go func() {
defer gocommon.LogOnPanic()
communityInfo, err := cm.FetchCommunityMetadata(communityID)
if err != nil {
log.Error("FetchCommunityInfo failed", "communityID", communityID, "err", err)
}
cm.signalUpdatedCommunityMetadata(communityID, communityInfo)
}()
}
func (cm *Manager) FetchCommunityMetadata(communityID string) (*thirdparty.CommunityInfo, error) {
communityInfo, err := cm.FetchCommunityInfo(communityID)
if err != nil {
return nil, err
}
_ = cm.setCommunityInfo(communityID, communityInfo)
return communityInfo, err
}
func (cm *Manager) GetCommunityImageURL(communityID string) string {
if cm.mediaServer != nil {
return cm.mediaServer.MakeWalletCommunityImagesURL(communityID)
}
return ""
}
func (cm *Manager) signalUpdatedCommunityMetadata(communityID string, communityInfo *thirdparty.CommunityInfo) {
if communityInfo == nil {
return
}
data := Data{
ID: communityID,
Name: communityInfo.CommunityName,
Color: communityInfo.CommunityColor,
Image: cm.GetCommunityImageURL(communityID),
}
payload, err := json.Marshal(data)
if err != nil {
log.Error("Error marshaling response: %v", err)
return
}
event := walletevent.Event{
Type: EventCommmunityDataUpdated,
Message: string(payload),
}
cm.feed.Send(event)
}