MCPcopy Create free account
hub / github.com/davecheney/pub / process

Function process

workers/processor.go:12–27  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
12func 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
29func forEach[T any](a []T, fn func(T) error) error {
30 for _, v := range a {

Calls 2

forEachFunction · 0.70
ErrorMethod · 0.45

Tested by

no test coverage detected