Skip to content

Commit 98859cd

Browse files
committed
fix: Add subgroup notfound
1 parent 0b05074 commit 98859cd

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

internal/services/aggregate_group_service.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,27 @@ func (s *AggregateGroupService) AddSubGroups(ctx context.Context, groupID uint,
208208
}
209209

210210
// Add new sub groups
211-
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
211+
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
212212
for _, newSg := range result.SubGroups {
213213
newSg.GroupID = groupID
214214
if err := tx.Create(&newSg).Error; err != nil {
215215
return app_errors.ParseDBError(err)
216216
}
217217
}
218218

219-
// 触发缓存更新
220-
if err := s.groupManager.Invalidate(); err != nil {
221-
logrus.WithContext(ctx).WithError(err).Error("failed to invalidate group cache after adding sub groups")
222-
}
223-
224219
return nil
225220
})
221+
222+
if err != nil {
223+
return err
224+
}
225+
226+
// 触发缓存更新
227+
if err := s.groupManager.Invalidate(); err != nil {
228+
logrus.WithContext(ctx).WithError(err).Error("failed to invalidate group cache after adding sub groups")
229+
}
230+
231+
return nil
226232
}
227233

228234
// UpdateSubGroupWeight updates the weight of a specific sub group

internal/services/subgroup_manager.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ func (m *SubGroupManager) SelectSubGroup(group *models.Group) (string, error) {
5858

5959
// RebuildSelectors rebuild all selectors based on the incoming group
6060
func (m *SubGroupManager) RebuildSelectors(groups map[string]*models.Group) {
61-
m.mu.Lock()
62-
m.selectors = make(map[uint]*selector)
63-
m.mu.Unlock()
61+
newSelectors := make(map[uint]*selector)
6462

65-
rebuildCount := 0
6663
for _, group := range groups {
6764
if group.GroupType == "aggregate" && len(group.SubGroups) > 0 {
68-
if sel := m.getSelector(group); sel != nil {
69-
rebuildCount++
65+
if sel := m.createSelector(group); sel != nil {
66+
newSelectors[group.ID] = sel
7067
}
7168
}
7269
}
7370

74-
logrus.WithField("new_count", rebuildCount).Debug("Rebuilt selectors for aggregate groups")
71+
m.mu.Lock()
72+
m.selectors = newSelectors
73+
m.mu.Unlock()
74+
75+
logrus.WithField("new_count", len(newSelectors)).Debug("Rebuilt selectors for aggregate groups")
7576
}
7677

7778
// getSelector retrieves or creates a selector for the aggregate group

0 commit comments

Comments
 (0)