@@ -124,7 +124,7 @@ type WorkerRepository interface {
124124
125125 UpsertWorkerLabels (ctx context.Context , workerId uuid.UUID , opts []UpsertWorkerLabelOpts ) ([]* sqlcv1.WorkerLabel , error )
126126
127- DeleteOldWorkers (ctx context.Context , tenantId uuid.UUID , lastHeartbeatBefore time.Time ) (bool , error )
127+ CleanupOldWorkers (ctx context.Context , tenantId uuid.UUID , lastHeartbeatBefore time.Time ) (bool , error )
128128
129129 GetDispatcherIdsForWorkers (ctx context.Context , tenantId uuid.UUID , workerIds []uuid.UUID ) (map [uuid.UUID ]uuid.UUID , map [uuid.UUID ]struct {}, error )
130130
@@ -723,22 +723,30 @@ func (w *workerRepository) UpsertWorkerLabels(ctx context.Context, workerId uuid
723723 return affinities , nil
724724}
725725
726- func (w * workerRepository ) DeleteOldWorkers (ctx context.Context , tenantId uuid.UUID , lastHeartbeatBefore time.Time ) (bool , error ) {
727- hasMore , err := w .queries .DeleteOldWorkers (ctx , w .pool , sqlcv1.DeleteOldWorkersParams {
726+ func (w * workerRepository ) CleanupOldWorkers (ctx context.Context , tenantId uuid.UUID , lastHeartbeatBefore time.Time ) (bool , error ) {
727+ const timeout = 1000 * 60 * 3 // 3 minutes
728+ const batchSize int32 = 10000
729+
730+ tx , commit , rollback , err := sqlchelpers .PrepareTxWithStatementTimeout (ctx , w .pool , w .l , timeout )
731+ if err != nil {
732+ return false , fmt .Errorf ("error beginning transaction: %w" , err )
733+ }
734+ defer rollback ()
735+
736+ result , err := w .queries .CleanupOldWorkers (ctx , tx , sqlcv1.CleanupOldWorkersParams {
728737 Tenantid : tenantId ,
729738 Lastheartbeatbefore : sqlchelpers .TimestampFromTime (lastHeartbeatBefore ),
730- Limit : 20 ,
739+ Batchsize : batchSize ,
731740 })
732-
733741 if err != nil {
734- if errors .Is (err , pgx .ErrNoRows ) {
735- return false , nil
736- }
742+ return false , fmt .Errorf ("error cleaning up old workers: %w" , err )
743+ }
737744
738- return false , err
745+ if err := commit (ctx ); err != nil {
746+ return false , fmt .Errorf ("error committing transaction: %w" , err )
739747 }
740748
741- return hasMore , nil
749+ return result . RowsAffected () == int64 ( batchSize ) , nil
742750}
743751
744752func (w * workerRepository ) GetDispatcherIdsForWorkers (ctx context.Context , tenantId uuid.UUID , workerIds []uuid.UUID ) (map [uuid.UUID ]uuid.UUID , map [uuid.UUID ]struct {}, error ) {
0 commit comments