2024-07-29 23:40:26 +02:00
|
|
|
package buildqueue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"brunel/config"
|
|
|
|
"brunel/domain"
|
2024-07-30 16:41:35 +02:00
|
|
|
"brunel/helpers"
|
2024-07-29 23:40:26 +02:00
|
|
|
"brunel/packages"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
2024-07-30 02:24:18 +02:00
|
|
|
"sort"
|
2024-07-29 23:40:26 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/html"
|
|
|
|
)
|
|
|
|
|
|
|
|
func StartPackageQueueWorker(ctx context.Context) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
err := packages.ProcessPackages()
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("unable to process packages: " + err.Error())
|
|
|
|
}
|
|
|
|
packs := packages.GetPackages()
|
2024-07-30 02:24:18 +02:00
|
|
|
packs.ForEach(func(k string, v domain.SourcePackage) bool {
|
2024-08-04 12:51:54 +02:00
|
|
|
needsBuild := v.Status == domain.Missing || v.Status == domain.Stale || v.Status == domain.Error
|
|
|
|
buildVersion := v.NewVersion
|
|
|
|
if buildVersion == "" {
|
|
|
|
buildVersion = v.Version
|
|
|
|
}
|
2024-07-29 23:40:26 +02:00
|
|
|
if needsBuild {
|
2024-07-30 16:41:35 +02:00
|
|
|
state, err := helpers.DBInst.GetBuildState(v.Name)
|
|
|
|
if err != nil {
|
|
|
|
state = domain.BuildState{
|
|
|
|
BuildNumber: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if state.BuildNumber > 1 {
|
2024-07-30 09:20:32 +02:00
|
|
|
return true
|
|
|
|
}
|
2024-07-30 01:07:21 +02:00
|
|
|
typ := domain.BuildTypeLTO
|
2024-07-30 16:41:35 +02:00
|
|
|
if state.BuildNumber == 1 {
|
2024-07-30 01:07:21 +02:00
|
|
|
typ = domain.BuildTypeNormal
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
buildItem := domain.BuildQueueItem{
|
|
|
|
Source: v,
|
|
|
|
Status: domain.Queued,
|
|
|
|
Type: typ,
|
|
|
|
Patch: false,
|
|
|
|
Rebuild: false,
|
2024-07-30 16:41:35 +02:00
|
|
|
BuildNumber: state.BuildNumber,
|
2024-07-29 23:40:26 +02:00
|
|
|
BuildVersion: buildVersion,
|
|
|
|
}
|
2024-07-30 16:41:35 +02:00
|
|
|
err = Add(buildItem)
|
2024-07-29 23:40:26 +02:00
|
|
|
if err != nil {
|
|
|
|
slog.Info("unable to add package to queue: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
time.Sleep(1 * time.Hour)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-07-30 02:30:26 +02:00
|
|
|
func processQueueAndStatus(ctx context.Context) {
|
2024-07-29 23:40:26 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2024-07-30 00:49:30 +02:00
|
|
|
q := GetQueue()
|
2024-07-29 23:40:26 +02:00
|
|
|
itemsToRemove := make([]string, 0)
|
2024-07-30 02:30:26 +02:00
|
|
|
buildingFound := false
|
|
|
|
|
2024-07-30 00:49:30 +02:00
|
|
|
q.ForEach(func(k string, item domain.BuildQueueItem) bool {
|
2024-07-30 02:30:26 +02:00
|
|
|
if item.Status == domain.Building {
|
|
|
|
buildingFound = true
|
|
|
|
complete, err := CheckIfBuildComplete(ctx, item)
|
|
|
|
if err != nil && !complete {
|
|
|
|
slog.Error("unable to check if build is complete: " + err.Error())
|
|
|
|
}
|
|
|
|
if complete {
|
2024-07-30 16:41:35 +02:00
|
|
|
|
2024-07-30 02:30:26 +02:00
|
|
|
if err != nil {
|
2024-07-30 16:41:35 +02:00
|
|
|
updateBuildState(item, domain.Error)
|
2024-08-04 12:51:54 +02:00
|
|
|
item.Source.Status = domain.Error
|
|
|
|
item.Source.LastBuildStatus = domain.Error
|
|
|
|
item.Source.BuildAttempts++
|
2024-07-30 16:41:35 +02:00
|
|
|
updatePackageStatus(&item, domain.Error)
|
2024-07-30 02:30:26 +02:00
|
|
|
} else {
|
2024-07-30 16:41:35 +02:00
|
|
|
updateBuildState(item, domain.Built)
|
2024-08-04 12:51:54 +02:00
|
|
|
item.Source.Status = domain.Built
|
|
|
|
item.Source.LastBuildStatus = domain.Built
|
|
|
|
item.Source.BuildAttempts = 0
|
|
|
|
item.Source.Version = item.BuildVersion
|
2024-07-30 16:41:35 +02:00
|
|
|
updatePackageStatus(&item, domain.Current)
|
2024-07-30 02:30:26 +02:00
|
|
|
}
|
2024-07-29 23:40:26 +02:00
|
|
|
packages.UpdateSourcePackage(item.Source)
|
|
|
|
itemsToRemove = append(itemsToRemove, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2024-07-30 02:30:26 +02:00
|
|
|
|
2024-07-29 23:40:26 +02:00
|
|
|
for _, item := range itemsToRemove {
|
|
|
|
Remove(item)
|
|
|
|
}
|
2024-07-30 02:30:26 +02:00
|
|
|
|
|
|
|
if !buildingFound {
|
|
|
|
err := ProcessNext()
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("unable to process queue: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:41:35 +02:00
|
|
|
time.Sleep(10 * time.Second)
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:41:35 +02:00
|
|
|
func updatePackageStatus(item *domain.BuildQueueItem, status domain.PackageStatus) {
|
2024-07-30 02:30:26 +02:00
|
|
|
item.Source.Packages.ForEach(func(k string, v domain.PackageInfo) bool {
|
|
|
|
v.Status = status
|
|
|
|
if status == domain.Current {
|
|
|
|
v.Version = item.BuildVersion
|
|
|
|
v.NewVersion = ""
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
2024-07-30 02:30:26 +02:00
|
|
|
item.Source.Packages.Set(k, v)
|
|
|
|
return true
|
|
|
|
})
|
2024-07-30 16:41:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateBuildState(item domain.BuildQueueItem, buildStatus domain.PackageStatus) {
|
|
|
|
state, err := helpers.DBInst.GetBuildState(item.Source.Name)
|
|
|
|
if err != nil {
|
|
|
|
state = domain.BuildState{
|
|
|
|
Name: item.Source.Name,
|
|
|
|
Status: string(domain.Queued),
|
|
|
|
BuildVersion: item.BuildVersion,
|
|
|
|
BuildNumber: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.Status = string(buildStatus)
|
|
|
|
state.BuildVersion = item.BuildVersion
|
|
|
|
state.BuildNumber++
|
|
|
|
helpers.DBInst.UpdateBuildState(state)
|
2024-07-30 02:30:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func StartQueueAndStatusWorker(ctx context.Context) {
|
|
|
|
go processQueueAndStatus(ctx)
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckIfBuildComplete(ctx context.Context, item domain.BuildQueueItem) (bool, error) {
|
|
|
|
resp, err := http.Get(config.Configs.ActionsUrl)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
doc, err := html.Parse(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buildName := item.Source.Name + "=" + item.BuildVersion
|
2024-07-30 02:24:18 +02:00
|
|
|
var builds []struct {
|
|
|
|
isMatch bool
|
|
|
|
status string
|
|
|
|
time time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var f func(*html.Node) error
|
|
|
|
f = func(n *html.Node) error {
|
2024-07-29 23:40:26 +02:00
|
|
|
if n.Type == html.ElementNode && n.Data == "div" {
|
|
|
|
for _, a := range n.Attr {
|
2024-07-30 02:24:18 +02:00
|
|
|
if a.Key == "class" && a.Val == "flex-item tw-items-center" {
|
|
|
|
isMatch, status, buildTime := checkBuildBlock(n, buildName)
|
2024-07-29 23:40:26 +02:00
|
|
|
if isMatch {
|
2024-07-30 02:24:18 +02:00
|
|
|
builds = append(builds, struct {
|
|
|
|
isMatch bool
|
|
|
|
status string
|
|
|
|
time time.Time
|
|
|
|
}{isMatch, status, buildTime})
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
2024-07-30 02:24:18 +02:00
|
|
|
if err := f(c); err != nil {
|
|
|
|
return err
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-30 02:24:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f(doc); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort builds by time, most recent first
|
|
|
|
sort.Slice(builds, func(i, j int) bool {
|
|
|
|
return builds[i].time.After(builds[j].time)
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(builds) == 0 {
|
|
|
|
slog.Info("No matching builds found", "buildName", buildName)
|
2024-07-29 23:40:26 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-07-30 02:24:18 +02:00
|
|
|
mostRecentBuild := builds[0]
|
|
|
|
|
|
|
|
switch mostRecentBuild.status {
|
|
|
|
case "Success":
|
|
|
|
return true, nil
|
|
|
|
case "Failure":
|
|
|
|
return true, fmt.Errorf("build failed")
|
|
|
|
case "Running":
|
|
|
|
return false, nil // Build is still in progress
|
|
|
|
case "Queued":
|
|
|
|
return false, nil // Build is still in progress
|
2024-07-30 03:02:55 +02:00
|
|
|
case "Waiting":
|
|
|
|
return false, nil // Build is waiting to start
|
2024-07-30 02:24:18 +02:00
|
|
|
default:
|
|
|
|
slog.Warn("Unknown build status", "status", mostRecentBuild.status)
|
|
|
|
return false, fmt.Errorf("unknown build status: %s", mostRecentBuild.status)
|
|
|
|
}
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
|
2024-07-30 02:24:18 +02:00
|
|
|
func checkBuildBlock(n *html.Node, buildName string) (bool, string, time.Time) {
|
2024-07-29 23:40:26 +02:00
|
|
|
var title string
|
|
|
|
var status string
|
2024-07-30 02:24:18 +02:00
|
|
|
var buildTime time.Time
|
2024-07-29 23:40:26 +02:00
|
|
|
|
|
|
|
var f func(*html.Node)
|
|
|
|
f = func(n *html.Node) {
|
|
|
|
if n.Type == html.ElementNode {
|
|
|
|
switch n.Data {
|
|
|
|
case "a":
|
|
|
|
for _, a := range n.Attr {
|
|
|
|
if a.Key == "class" && a.Val == "flex-item-title" {
|
|
|
|
for _, attr := range n.Attr {
|
|
|
|
if attr.Key == "title" {
|
|
|
|
title = attr.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 02:24:18 +02:00
|
|
|
case "div":
|
2024-07-29 23:40:26 +02:00
|
|
|
for _, a := range n.Attr {
|
2024-07-30 02:24:18 +02:00
|
|
|
if a.Key == "class" && a.Val == "flex-item-leading" {
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
if c.Type == html.ElementNode && c.Data == "span" {
|
|
|
|
for _, attr := range c.Attr {
|
|
|
|
if attr.Key == "data-tooltip-content" {
|
|
|
|
status = attr.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 02:24:18 +02:00
|
|
|
case "relative-time":
|
|
|
|
for _, a := range n.Attr {
|
|
|
|
if a.Key == "datetime" {
|
|
|
|
buildTime, _ = time.Parse(time.RFC3339, a.Val)
|
|
|
|
}
|
|
|
|
}
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
f(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f(n)
|
|
|
|
|
2024-07-30 02:24:18 +02:00
|
|
|
return title == buildName, status, buildTime
|
2024-07-29 23:40:26 +02:00
|
|
|
}
|