-
Notifications
You must be signed in to change notification settings - Fork 14
/
rerunobjecthooks.go
48 lines (42 loc) · 1.16 KB
/
rerunobjecthooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"context"
"github.com/davecheney/pub/models"
"gorm.io/gorm"
)
type RerunObjectHooksCmd struct {
Domain string `required:"" help:"domain name of the instance to backfill from."`
}
func (c *RerunObjectHooksCmd) Run(ctx *Context) error {
db, err := gorm.Open(ctx.Dialector, &ctx.Config)
if err != nil {
return err
}
instance, err := models.NewInstances(db).FindByDomain(c.Domain)
if err != nil {
return err
}
var objs []*models.Object
db = db.WithContext(
context.WithValue(db.Statement.Context, "instance", instance),
)
return db.FindInBatches(
&objs, 100, func(tx *gorm.DB, batch int) error {
// var wg sync.WaitGroup
// wg.Add(len(objs))
for _, obj := range objs {
// go func(obj *models.Object) {
// db.Transaction(func(tx *gorm.DB) error {
// defer wg.Done()
// ctx.Logger.Info("rerunning object hooks", "id", obj.ID, "type", obj.Type, "uri", obj.URI)
if err := obj.AfterSave(tx); err != nil {
ctx.Logger.Error("failed to run object hooks", "id", obj.ID, "type", obj.Type, "uri", obj.URI, "error", err)
}
// return nil
// })
// }(obj)
}
// wg.Wait()
return nil
}).Error
}