brunel/buildqueue/queue.go

87 lines
1.7 KiB
Go
Raw Normal View History

2024-07-29 23:40:26 +02:00
package buildqueue
import (
"brunel/domain"
"brunel/fastmap"
"errors"
)
var queue = fastmap.New[string, domain.BuildQueueItem]()
func Add(buildItem domain.BuildQueueItem) error {
if _, ok := queue.Get(buildItem.Source.Name); ok {
return errors.New("package already in queue")
}
queue.Set(buildItem.Source.Name, buildItem)
return nil
}
func Get(name string) (domain.BuildQueueItem, bool) {
item, ok := queue.Get(name)
return item, ok
}
func Remove(name string) error {
_, ok := queue.Get(name)
if !ok {
return errors.New("package not in queue")
}
queue.Delete(name)
return nil
}
func Update(buildItem domain.BuildQueueItem) error {
item, ok := queue.Get(buildItem.Source.Name)
if !ok {
return errors.New("package not in queue")
}
if item.Status == domain.Building {
return errors.New("package is already building")
}
queue.Set(buildItem.Source.Name, buildItem)
return nil
}
func GetQueue() *fastmap.Fastmap[string, domain.BuildQueueItem] {
return queue
}
func GetCounts() domain.BuildQueueCount {
count := domain.BuildQueueCount{
Queued: 0,
Building: 0,
}
queue.Iter(func(k string, v domain.BuildQueueItem) bool {
if v.Status == domain.Queued {
count.Queued++
} else if v.Status == domain.Building {
count.Building++
}
return true
})
return count
}
func ProcessNext() error {
var item *domain.BuildQueueItem
queue.Iter(func(k string, v domain.BuildQueueItem) bool {
if v.Status == domain.Queued {
item = &v
return false
}
return true
})
if item == nil {
return errors.New("no packages in queue")
}
err := UpdateBuildFile(*item)
if err != nil {
return err
}
item.Status = domain.Building
err = Update(*item)
return err
}