Skip to content

Commit

Permalink
fix: improve feed states notification
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Jun 15, 2024
1 parent f90ca07 commit e9e2d34
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ linters-settings:
# Whether to skip (*x).method() calls where x is a pointer receiver.
# Default: true
skipRecvDeref: false
disabled-checks:
- ifElseChain # too much false positives

gomnd:
# List of function patterns to exclude from analysis.
Expand Down
28 changes: 19 additions & 9 deletions app/sephirah/internal/biz/bizangela/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func NewPullFeedTopic( //nolint:gocognit // TODO
return libmq.NewTopic[modelyesod.PullFeed](
"PullFeed",
func(ctx context.Context, p *modelyesod.PullFeed) error {
if !a.supv.CheckFeedSource(p.Source) {
return nil
}

// Prepare for updating feed pull status
fc := new(modelyesod.FeedConfig)
fc.ID = p.InternalID
Expand All @@ -42,16 +38,24 @@ func NewPullFeedTopic( //nolint:gocognit // TODO
_ = a.repo.UpdateFeedPullStatus(ctx, fc)
if p.SystemNotify != nil {
un := *p.SystemNotify
if fc.LatestPullStatus == modelyesod.FeedConfigPullStatusSuccess {
if fc.LatestPullMessage == "" {
un.Notification.Level = modelnetzach.SystemNotificationLevelInfo
} else if fc.LatestPullStatus == modelyesod.FeedConfigPullStatusSuccess {
un.Notification.Level = modelnetzach.SystemNotificationLevelWarning
} else {
un.Notification.Level = modelnetzach.SystemNotificationLevelError
un.Notification.Content = fc.LatestPullMessage
}
un.Notification.Content = fc.LatestPullMessage
_ = systemNotify.PublishFallsLocalCall(ctx, un)
}
}()

// Check porter availability
if !a.supv.CheckFeedSource(p.Source) {
fc.LatestPullMessage = fmt.Sprintf("Pull %s feature not activate", p.Source)
return nil
}

// Pull feed and upsert
resp, err := a.porter.PullFeed(
a.supv.CallFeedSource(ctx, p.Source),
Expand Down Expand Up @@ -99,19 +103,25 @@ func NewPullFeedTopic( //nolint:gocognit // TODO
}
fc.LatestPullStatus = modelyesod.FeedConfigPullStatusSuccess

// Send to notify router
// Queue ParseFeedItemDigest and NotifyRouter
newItems := make([]*modelfeed.Item, 0, len(newItemGUIDs))
for _, item := range feed.Items {
if slices.Contains(newItemGUIDs, item.GUID) {
newItems = append(newItems, item)
_ = parse.Publish(ctx, modelangela.ParseFeedItemDigest{ID: item.ID})
err = parse.Publish(ctx, modelangela.ParseFeedItemDigest{ID: item.ID})
}
}
if err != nil {
fc.LatestPullMessage = fmt.Sprintf("Queue ParseFeedItemDigest failed: %s", err.Error())
}
if len(newItems) > 0 {
_ = notify.Publish(ctx, modelangela.NotifyRouter{
err = notify.Publish(ctx, modelangela.NotifyRouter{
FeedID: feed.ID,
Messages: newItems,
})
if err != nil {
fc.LatestPullMessage = fmt.Sprintf("Queue NotifyRouter failed: %s", err.Error())
}
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion app/sephirah/internal/biz/bizyesod/yesod.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (y *Yesod) PullFeeds(ctx context.Context) error {
owner.ID,
modelnetzach.SystemNotificationLevelOngoing,
fmt.Sprintf("Scheduled Server Task: Update Feed %s", c.Name),
"",
"Queued",
)
un.Notification.ID, err = y.searcher.NewID(ctx)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions app/sephirah/internal/data/angela.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"context"
"fmt"
"time"

"github.com/tuihub/librarian/app/sephirah/internal/biz/bizangela"
Expand Down Expand Up @@ -318,6 +319,10 @@ func (a *angelaRepo) UpsertSystemNotification(
userID model.InternalID,
notification *modelnetzach.SystemNotification,
) error {
n, err := a.data.db.SystemNotification.Get(ctx, notification.ID)
if err == nil && n != nil && len(n.Content) > 0 {
notification.Content = fmt.Sprintf("%s\n%s", n.Content, notification.Content)
}
q := a.data.db.SystemNotification.Create().
SetID(notification.ID).
SetType(converter.ToEntSystemNotificationType(notification.Type)).
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/libcache/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (k *Key[T]) GetWithFallBack(ctx context.Context, fallBackFunc fallBackFunc[
if err == nil {
return res, nil
}
if fallBackFunc != nil { //nolint:gocritic // no need
if fallBackFunc != nil {
res, err = fallBackFunc(ctx)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/lib/libcache/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (m *Map[K, V]) GetWithFallBack(ctx context.Context, key K,
if err == nil {
return res, nil
}
if fallBackFunc != nil { //nolint:gocritic // no need
if fallBackFunc != nil {
res, err = fallBackFunc(ctx, key)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/lib/libmq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ func poisonedTopicName(topic string) string {

func retryMiddleware() func(h message.HandlerFunc) message.HandlerFunc {
return middleware.Retry{
MaxRetries: 5, //nolint:gomnd //TODO
MaxRetries: 3, //nolint:gomnd //TODO
InitialInterval: time.Second,
MaxInterval: time.Minute,
Multiplier: 2, //nolint:gomnd //TODO
Multiplier: 4, //nolint:gomnd //TODO
MaxElapsedTime: 0,
RandomizationFactor: 0.2, //nolint:gomnd //TODO
OnRetryHook: nil,
Expand Down

0 comments on commit e9e2d34

Please sign in to comment.