fix: clear scheduler cache when deleting accounts

This commit is contained in:
gaoren002 2026-05-11 14:22:13 +00:00
parent 771e0ca973
commit 60f6602b81
2 changed files with 36 additions and 0 deletions

View File

@ -450,6 +450,7 @@ func (r *accountRepository) Delete(ctx context.Context, id int64) error {
return err
}
}
r.deleteSchedulerAccountSnapshot(ctx, id)
if err := enqueueSchedulerOutbox(ctx, r.sql, service.SchedulerOutboxEventAccountChanged, &id, nil, buildSchedulerGroupPayload(groupIDs)); err != nil {
logger.LegacyPrintf("repository.account", "[SchedulerOutbox] enqueue account delete failed: account=%d err=%v", id, err)
}
@ -749,6 +750,15 @@ func (r *accountRepository) syncSchedulerAccountSnapshot(ctx context.Context, ac
}
}
func (r *accountRepository) deleteSchedulerAccountSnapshot(ctx context.Context, accountID int64) {
if r == nil || r.schedulerCache == nil || accountID <= 0 {
return
}
if err := r.schedulerCache.DeleteAccount(ctx, accountID); err != nil {
logger.LegacyPrintf("repository.account", "[Scheduler] delete account snapshot failed: id=%d err=%v", accountID, err)
}
}
func (r *accountRepository) syncSchedulerAccountSnapshots(ctx context.Context, accountIDs []int64) {
if r == nil || r.schedulerCache == nil || len(accountIDs) == 0 {
return

View File

@ -23,6 +23,7 @@ type AccountRepoSuite struct {
type schedulerCacheRecorder struct {
setAccounts []*service.Account
deleteIDs []int64
accounts map[int64]*service.Account
}
@ -53,6 +54,10 @@ func (s *schedulerCacheRecorder) SetAccount(ctx context.Context, account *servic
}
func (s *schedulerCacheRecorder) DeleteAccount(ctx context.Context, accountID int64) error {
s.deleteIDs = append(s.deleteIDs, accountID)
if s.accounts != nil {
delete(s.accounts, accountID)
}
return nil
}
@ -185,6 +190,27 @@ func (s *AccountRepoSuite) TestDelete() {
s.Require().Error(err, "expected error after delete")
}
func (s *AccountRepoSuite) TestDelete_RemovesSchedulerAccountSnapshot() {
account := mustCreateAccount(s.T(), s.client, &service.Account{Name: "to-delete-cache"})
cacheRecorder := &schedulerCacheRecorder{
accounts: map[int64]*service.Account{
account.ID: {
ID: account.ID,
Name: account.Name,
Status: service.StatusActive,
Schedulable: true,
},
},
}
s.repo.schedulerCache = cacheRecorder
err := s.repo.Delete(s.ctx, account.ID)
s.Require().NoError(err, "Delete")
s.Require().Equal([]int64{account.ID}, cacheRecorder.deleteIDs)
s.Require().NotContains(cacheRecorder.accounts, account.ID)
}
func (s *AccountRepoSuite) TestDelete_WithGroupBindings() {
group := mustCreateGroup(s.T(), s.client, &service.Group{Name: "g-del"})
account := mustCreateAccount(s.T(), s.client, &service.Account{Name: "acc-del"})