process makes one pass through the objects matching the scope, calling fn for each one. If fn returns an error, the object is updated with the error and the process continues. If fn returns nil, the object is deleted.
(db *gorm.DB, scope func(*gorm.DB) *gorm.DB, fn func(*gorm.DB, T) error)
| 10 | // If fn returns an error, the object is updated with the error and the process continues. |
| 11 | // If fn returns nil, the object is deleted. |
| 12 | func process[T any](db *gorm.DB, scope func(*gorm.DB) *gorm.DB, fn func(*gorm.DB, T) error) error { |
| 13 | var requests []T |
| 14 | return db.Scopes(scope).FindInBatches(&requests, 100, func(db *gorm.DB, batch int) error { |
| 15 | return forEach(requests, func(request T) error { |
| 16 | start := time.Now() |
| 17 | if err := fn(db, request); err != nil { |
| 18 | return db.Model(request).Updates(map[string]interface{}{ |
| 19 | "attempts": gorm.Expr("attempts + 1"), |
| 20 | "last_attempt": start, |
| 21 | "last_result": err.Error(), |
| 22 | }).Error |
| 23 | } |
| 24 | return db.Delete(request).Error |
| 25 | }) |
| 26 | }).Error |
| 27 | } |
| 28 | |
| 29 | func forEach[T any](a []T, fn func(T) error) error { |
| 30 | for _, v := range a { |
no test coverage detected