Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should always requeue #105

Merged
merged 4 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/bots/telegram/handlers/recap/toggle_recap.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (h *EnableRecapCommandHandler) Handle(c *tgbot.Context) (tgbot.Response, er
return nil, tgbot.NewExceptionError(err).WithMessage("聊天记录回顾功能开启失败,请稍后再试!").WithReply(c.Update.Message)
}

err = h.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(c.Update.Message.Chat.ID)
err = h.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(c.Update.Message.Chat.ID, telegram.ChatType(c.Update.Message.Chat.Type), c.Update.Message.Chat.Title)
if err != nil {
return nil, tgbot.NewExceptionError(err).WithMessage("聊天记录回顾功能开启失败,请稍后再试!").WithReply(c.Update.Message)
}
Expand Down
21 changes: 12 additions & 9 deletions internal/models/tgchats/feature_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (m *Model) HasChatHistoriesRecapEnabled(chatID int64, chatType telegram.Cha
return featureFlags.FeatureChatHistoriesRecap, nil
}

func (m *Model) ListChatHistoriesRecapEnabledChats() ([]int64, error) {
func (m *Model) ListChatHistoriesRecapEnabledChats() ([]*ent.TelegramChatFeatureFlags, error) {
featureFlagsChats, err := m.ent.TelegramChatFeatureFlags.
Query().
Where(
Expand All @@ -144,28 +144,26 @@ func (m *Model) ListChatHistoriesRecapEnabledChats() ([]int64, error) {
return nil, err
}

return lo.Map(featureFlagsChats, func(featureFlags *ent.TelegramChatFeatureFlags, _ int) int64 {
return featureFlags.ChatID
}), nil
return featureFlagsChats, nil
}

func (m *Model) QueueSendChatHistoriesRecapTask() {
chatIDs, err := m.ListChatHistoriesRecapEnabledChats()
chats, err := m.ListChatHistoriesRecapEnabledChats()
if err != nil {
m.logger.Errorf("failed to list chat histories recap enabled chats: %v", err)
return
}

for _, chatID := range chatIDs {
err = m.QueueOneSendChatHistoriesRecapTaskForChatID(chatID)
for _, chat := range chats {
err = m.QueueOneSendChatHistoriesRecapTaskForChatID(chat.ChatID, telegram.ChatType(chat.ChatType), chat.ChatTitle)
if err != nil {
m.logger.Errorf("failed to queue send chat histories recap task: %v", err)
continue
}
}
}

func (m *Model) QueueOneSendChatHistoriesRecapTaskForChatID(chatID int64) error {
func (m *Model) QueueOneSendChatHistoriesRecapTaskForChatID(chatID int64, chatType telegram.ChatType, chatTitle string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

Expand Down Expand Up @@ -194,7 +192,12 @@ func (m *Model) QueueOneSendChatHistoriesRecapTaskForChatID(chatID int64) error

for _, schedule := range scheduleSets {
m.logger.Infof("scheduled one send chat histories recap task for %d at %s", chatID, schedule)
return m.digger.BuryUtil(ctx, timecapsules.AutoRecapCapsule{ChatID: chatID}, schedule.UnixMilli())

return m.digger.BuryUtil(ctx, timecapsules.AutoRecapCapsule{
ChatID: chatID,
ChatType: chatType,
ChatTitle: chatTitle,
}, schedule.UnixMilli())
}

return nil
Expand Down
8 changes: 5 additions & 3 deletions internal/models/tgchats/feature_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"testing"

"github.com/nekomeowww/insights-bot/ent"
"github.com/nekomeowww/insights-bot/ent/telegramchatfeatureflags"
"github.com/nekomeowww/insights-bot/pkg/types/telegram"
"github.com/nekomeowww/insights-bot/pkg/utils"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -85,8 +87,8 @@ func TestListChatHistoriesRecapEnabledChats(t *testing.T) {
assert.NoError(err)
}()

chatIDs, err := model.ListChatHistoriesRecapEnabledChats()
chats, err := model.ListChatHistoriesRecapEnabledChats()
require.NoError(err)
require.Len(chatIDs, 3)
assert.ElementsMatch([]int64{chatID1, chatID2, chatID3}, chatIDs)
require.Len(chats, 3)
assert.ElementsMatch([]int64{chatID1, chatID2, chatID3}, lo.Map(chats, func(item *ent.TelegramChatFeatureFlags, _ int) int64 { return item.ChatID }))
}
38 changes: 33 additions & 5 deletions internal/services/autorecap/autorecap.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,39 @@ func (m *AutoRecapService) sendChatHistoriesRecap(
digger *timecapsule.TimeCapsuleDigger[timecapsules.AutoRecapCapsule],
capsule *timecapsule.TimeCapsule[timecapsules.AutoRecapCapsule],
) {
var enabled bool

_, err := lo.Attempt(10, func(index int) error {
var err error
enabled, err = m.tgchats.HasChatHistoriesRecapEnabled(capsule.Payload.ChatID, capsule.Payload.ChatType, capsule.Payload.ChatTitle)
if err != nil {
m.logger.Errorf("failed to check chat histories recap enabled: %v", err)
return err
}

return nil
})
if err != nil {
// requeue if failed
err = m.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(capsule.Payload.ChatID, capsule.Payload.ChatType, capsule.Payload.ChatTitle)
if err != nil {
m.logger.Errorf("failed to queue one send chat histories recap task for chat %d: %v", capsule.Payload.ChatID, err)
}

m.logger.Errorf("failed to check chat histories recap enabled: %v", err)

return
}
if !enabled {
return
}

// always requeue
err = m.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(capsule.Payload.ChatID, capsule.Payload.ChatType, capsule.Payload.ChatTitle)
if err != nil {
m.logger.Errorf("failed to queue one send chat histories recap task for chat %d: %v", capsule.Payload.ChatID, err)
}

m.logger.Infof("generating chat histories recap for chat %d", capsule.Payload.ChatID)

histories, err := m.chathistories.FindLastSixHourChatHistories(capsule.Payload.ChatID)
Expand Down Expand Up @@ -118,9 +151,4 @@ func (m *AutoRecapService) sendChatHistoriesRecap(
m.logger.Errorf("failed to send chat histories recap: %v", err)
}
}

err = m.tgchats.QueueOneSendChatHistoriesRecapTaskForChatID(capsule.Payload.ChatID)
if err != nil {
m.logger.Errorf("failed to queue one send chat histories recap task for chat %d: %v", capsule.Payload.ChatID, err)
}
}
6 changes: 5 additions & 1 deletion pkg/types/timecapsules/auto_recap.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package timecapsules

import "github.com/nekomeowww/insights-bot/pkg/types/telegram"

type AutoRecapCapsule struct {
ChatID int64 `json:"chat_id"`
ChatID int64 `json:"chat_id"`
ChatType telegram.ChatType `json:"chat_type"`
ChatTitle string `json:"chat_title"`
}