forked from gitea/gitea
refactor: make db iterate context aware (#27710)
the iteration will run until finished atm. this changes it by checking if if the context got canceled before each run of a loop sequence is executed [View this pull with now whitespace](https://github.com/go-gitea/gitea/pull/27710/files?diff=unified&w=1)
This commit is contained in:
parent
510d07506e
commit
b2f828db5e
|
@ -17,6 +17,10 @@ func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx contex
|
||||||
batchSize := setting.Database.IterateBufferSize
|
batchSize := setting.Database.IterateBufferSize
|
||||||
sess := GetEngine(ctx)
|
sess := GetEngine(ctx)
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
default:
|
||||||
beans := make([]*Bean, 0, batchSize)
|
beans := make([]*Bean, 0, batchSize)
|
||||||
if cond != nil {
|
if cond != nil {
|
||||||
sess = sess.Where(cond)
|
sess = sess.Where(cond)
|
||||||
|
@ -35,4 +39,5 @@ func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx contex
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,10 @@ func deleteOrphanedRepos(ctx context.Context) (int64, error) {
|
||||||
adminUser := &user_model.User{IsAdmin: true}
|
adminUser := &user_model.User{IsAdmin: true}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return deleted, ctx.Err()
|
||||||
|
default:
|
||||||
var ids []int64
|
var ids []int64
|
||||||
if err := e.Table("`repository`").
|
if err := e.Table("`repository`").
|
||||||
Join("LEFT", "`user`", "repository.owner_id=user.id").
|
Join("LEFT", "`user`", "repository.owner_id=user.id").
|
||||||
|
@ -57,6 +61,7 @@ func deleteOrphanedRepos(ctx context.Context) (int64, error) {
|
||||||
deleted++
|
deleted++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
Loading…
Reference in New Issue