Merge pull request #2375 from gaoren002/fix/account-delete-scheduler-cache
fix: clear scheduler cache when deleting accounts
This commit is contained in:
commit
a60a349ecf
@ -454,6 +454,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)
|
||||
}
|
||||
@ -754,6 +755,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
|
||||
|
||||
@ -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"})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user