fix(templates): fill last task for boltdb

This commit is contained in:
fiftin 2024-07-20 01:13:01 +05:00
parent f63557d472
commit 6bfe7517f8
No known key found for this signature in database
GPG Key ID: 044381366A5D4731
2 changed files with 53 additions and 10 deletions

View File

@ -362,11 +362,9 @@ func apply(
return return
} }
if filter != nil { if filter != nil && !filter(obj) {
if !filter(obj) {
continue continue
} }
}
err = applier(obj) err = applier(obj)
if err != nil { if err != nil {
@ -405,7 +403,6 @@ func (d *BoltDb) count(bucketID int, props db.ObjectProps, params db.RetrieveQue
func unmarshalObjects(rawData enumerable, props db.ObjectProps, params db.RetrieveQueryParams, filter func(interface{}) bool, objects interface{}) (err error) { func unmarshalObjects(rawData enumerable, props db.ObjectProps, params db.RetrieveQueryParams, filter func(interface{}) bool, objects interface{}) (err error) {
objectsValue := reflect.ValueOf(objects).Elem() objectsValue := reflect.ValueOf(objects).Elem()
//objType := objectsValue.Type().Elem()
objectsValue.Set(reflect.MakeSlice(objectsValue.Type(), 0, 0)) objectsValue.Set(reflect.MakeSlice(objectsValue.Type(), 0, 0))
@ -450,6 +447,20 @@ func (d *BoltDb) getObjects(bucketID int, props db.ObjectProps, params db.Retrie
}) })
} }
func (d *BoltDb) apply(bucketID int, props db.ObjectProps, params db.RetrieveQueryParams, applier func(interface{}) error) error {
return d.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(makeBucketId(props, bucketID))
var c enumerable
if b == nil {
c = emptyEnumerable{}
} else {
c = b.Cursor()
}
return apply(c, props, params, nil, applier)
})
}
func (d *BoltDb) deleteObject(bucketID int, props db.ObjectProps, objectID objectID, tx *bbolt.Tx) error { func (d *BoltDb) deleteObject(bucketID int, props db.ObjectProps, objectID objectID, tx *bbolt.Tx) error {
for _, u := range []db.ObjectProps{db.TemplateProps, db.EnvironmentProps, db.InventoryProps, db.RepositoryProps} { for _, u := range []db.ObjectProps{db.TemplateProps, db.EnvironmentProps, db.InventoryProps, db.RepositoryProps} {
inUse, err := d.isObjectInUse(bucketID, props, objectID, u) inUse, err := d.isObjectInUse(bucketID, props, objectID, u)

View File

@ -1,6 +1,7 @@
package bolt package bolt
import ( import (
"errors"
"github.com/ansible-semaphore/semaphore/db" "github.com/ansible-semaphore/semaphore/db"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
@ -55,13 +56,44 @@ func (d *BoltDb) GetTemplates(projectID int, filter db.TemplateFilter, params db
return return
} }
var tasks []db.Task templatesMap := make(map[int]*db.Template)
err = d.getObjects(projectID, db.TaskProps, db.RetrieveQueryParams{}, func(i interface{}) bool { for i := 0; i < len(templates); i++ {
return true templatesMap[templates[i].ID] = &templates[i]
}, &tasks) }
//err = db.FillTemplates(d, templates) unfilledTemplateCount := len(templates)
var errEndOfTemplates = errors.New("no more templates to filling")
err = d.apply(projectID, db.TaskProps, db.RetrieveQueryParams{}, func(i interface{}) error {
task := i.(db.Task)
tpl, ok := templatesMap[task.TemplateID]
if !ok {
return nil
}
if tpl.LastTask != nil {
return nil
}
tpl.LastTask = &db.TaskWithTpl{
Task: task,
}
unfilledTemplateCount--
if unfilledTemplateCount <= 0 {
return errEndOfTemplates
}
return nil
})
if errors.Is(err, errEndOfTemplates) {
err = nil
}
return return
} }