brunel/buildqueue/queue.go

102 lines
2.1 KiB
Go
Raw Normal View History

2024-07-29 23:40:26 +02:00
package buildqueue
import (
"brunel/domain"
2024-08-10 15:06:31 +02:00
"fmt"
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
2024-08-10 15:06:31 +02:00
func getQueueKey(name string, buildType domain.BuildType) string {
return fmt.Sprintf("%s_%s", name, buildType)
}
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
}
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-08-10 15:06:31 +02:00
key := getQueueKey(buildItem.Source.Name, buildItem.Type)
if _, ok := queue.Get(key); ok {
2024-07-29 23:40:26 +02:00
return errors.New("package already in queue")
}
2024-08-10 15:06:31 +02:00
queue.Set(key, buildItem)
2024-07-29 23:40:26 +02:00
return nil
}
2024-08-10 15:06:31 +02:00
func Get(name string, buildType domain.BuildType) (domain.BuildQueueItem, bool) {
key := getQueueKey(name, buildType)
item, ok := queue.Get(key)
2024-07-29 23:40:26 +02:00
return item, ok
}
2024-08-10 15:06:31 +02:00
func Remove(name string, buildType domain.BuildType) error {
key := getQueueKey(name, buildType)
_, ok := queue.Get(key)
2024-07-29 23:40:26 +02:00
if !ok {
return errors.New("package not in queue")
}
2024-08-10 15:06:31 +02:00
queue.Del(key)
2024-07-29 23:40:26 +02:00
return nil
}
func Update(buildItem domain.BuildQueueItem) error {
2024-08-10 15:06:31 +02:00
key := getQueueKey(buildItem.Source.Name, buildItem.Type)
item, ok := queue.Get(key)
2024-07-29 23:40:26 +02:00
if !ok {
return errors.New("package not in queue")
}
if item.Status == domain.Building {
return errors.New("package is already building")
}
2024-08-10 15:06:31 +02:00
queue.Set(key, buildItem)
2024-07-29 23:40:26 +02:00
return nil
}
func ProcessNext() error {
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 {
2024-07-30 02:58:10 +02:00
return nil
2024-07-29 23:40:26 +02:00
}
2024-08-02 22:07:39 +02:00
2024-07-29 23:40:26 +02:00
err := UpdateBuildFile(*item)
if err != nil {
return err
}
item.Status = domain.Building
err = Update(*item)
return err
}