brunel/buildqueue/queue.go

93 lines
1.8 KiB
Go
Raw Normal View History

2024-07-29 23:40:26 +02:00
package buildqueue
import (
"brunel/domain"
2024-07-30 00:59:30 +02:00
"strings"
2024-07-29 23:40:26 +02:00
"errors"
2024-07-30 00:49:30 +02:00
"github.com/alphadose/haxmap"
2024-07-29 23:40:26 +02:00
)
2024-07-30 00:49:30 +02:00
var queue = haxmap.New[string, domain.BuildQueueItem]()
2024-07-29 23:40:26 +02:00
func Add(buildItem domain.BuildQueueItem) error {
2024-07-30 00:59:30 +02:00
if strings.Contains(buildItem.BuildVersion, "+b") {
buildItem.BuildVersion = strings.Split(buildItem.BuildVersion, "+b")[0]
}
2024-07-29 23:40:26 +02:00
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")
}
2024-07-30 00:49:30 +02:00
queue.Del(name)
2024-07-29 23:40:26 +02:00
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
}
2024-07-30 00:49:30 +02:00
func GetQueue() *haxmap.Map[string, domain.BuildQueueItem] {
return queue
2024-07-29 23:40:26 +02:00
}
func GetCounts() domain.BuildQueueCount {
count := domain.BuildQueueCount{
Queued: 0,
Building: 0,
}
2024-07-30 00:49:30 +02:00
queue.ForEach(func(k string, v domain.BuildQueueItem) bool {
2024-07-29 23:40:26 +02:00
if v.Status == domain.Queued {
count.Queued++
} else if v.Status == domain.Building {
count.Building++
}
return true
})
return count
}
func ProcessNext() error {
2024-07-30 00:49:30 +02:00
2024-07-29 23:40:26 +02:00
var item *domain.BuildQueueItem
2024-07-30 00:49:30 +02:00
queue.ForEach(func(k string, v domain.BuildQueueItem) bool {
2024-07-29 23:40:26 +02:00
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
}