Skip to content

refactor: make db iterate context aware #27710

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

Merged
merged 3 commits into from
Oct 21, 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
5 changes: 5 additions & 0 deletions models/db/iterate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx contex
batchSize := setting.Database.IterateBufferSize
sess := GetEngine(ctx)
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
beans := make([]*Bean, 0, batchSize)
if cond != nil {
sess = sess.Where(cond)
Expand All @@ -36,3 +40,4 @@ func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx contex
}
}
}
}
5 changes: 5 additions & 0 deletions modules/doctor/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func deleteOrphanedRepos(ctx context.Context) (int64, error) {
adminUser := &user_model.User{IsAdmin: true}

for {
select {
case <-ctx.Done():
return deleted, ctx.Err()
default:
var ids []int64
if err := e.Table("`repository`").
Join("LEFT", "`user`", "repository.owner_id=user.id").
Expand All @@ -58,6 +62,7 @@ func deleteOrphanedRepos(ctx context.Context) (int64, error) {
}
}
}
}

func init() {
Register(&Check{
Expand Down