brunel/buildqueue/queue.go
2024-07-29 23:49:30 +01:00

89 lines
1.7 KiB
Go

package buildqueue
import (
"brunel/domain"
"errors"
"github.com/alphadose/haxmap"
)
var queue = haxmap.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.Del(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() *haxmap.Map[string, domain.BuildQueueItem] {
return queue
}
func GetCounts() domain.BuildQueueCount {
count := domain.BuildQueueCount{
Queued: 0,
Building: 0,
}
queue.ForEach(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.ForEach(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
}