brunel/buildqueue/worker.go

276 lines
6.2 KiB
Go

package buildqueue
import (
"brunel/config"
"brunel/domain"
"brunel/packages"
"context"
"fmt"
"log/slog"
"net/http"
"sort"
"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()
packs.ForEach(func(k string, v domain.SourcePackage) bool {
needsBuild := false
buildVersion := ""
buildAttempt := 0
v.Packages.ForEach(func(k string, v domain.PackageInfo) bool {
if v.Status == domain.Current {
return true
}
version := v.NewVersion
if version == "" {
version = v.Version
}
if v.Status == domain.Missing {
needsBuild = true
buildVersion = version
buildAttempt = v.BuildAttempts
return false
}
if v.Status == domain.Stale {
needsBuild = true
buildVersion = version
buildAttempt = v.BuildAttempts
return false
}
return true
})
if needsBuild {
if buildAttempt > 1 {
return true
}
typ := domain.BuildTypeLTO
if buildAttempt == 1 {
typ = domain.BuildTypeNormal
}
buildItem := domain.BuildQueueItem{
Source: v,
Status: domain.Queued,
Type: typ,
Patch: false,
Rebuild: false,
BuildNumber: buildAttempt,
BuildVersion: buildVersion,
}
err := Add(buildItem)
if err != nil {
slog.Info("unable to add package to queue: " + err.Error())
}
}
return true
})
time.Sleep(1 * time.Hour)
}
}
}()
}
func processQueueAndStatus(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
q := GetQueue()
itemsToRemove := make([]string, 0)
buildingFound := false
q.ForEach(func(k string, item domain.BuildQueueItem) bool {
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 {
if err != nil {
updatePackageStatus(&item, domain.Error, domain.Error)
} else {
updatePackageStatus(&item, domain.Current, domain.Built)
}
packages.UpdateSourcePackage(item.Source)
itemsToRemove = append(itemsToRemove, k)
}
}
return true
})
for _, item := range itemsToRemove {
Remove(item)
}
if !buildingFound {
err := ProcessNext()
if err != nil {
slog.Error("unable to process queue: " + err.Error())
}
}
time.Sleep(1500 * time.Millisecond)
}
}
}
func updatePackageStatus(item *domain.BuildQueueItem, status domain.PackageStatus, buildStatus domain.PackageStatus) {
item.Source.Packages.ForEach(func(k string, v domain.PackageInfo) bool {
v.Status = status
v.BuildAttempts++
v.LastBuildStatus = domain.PackageStatus(buildStatus)
if status == domain.Current {
v.Version = item.BuildVersion
v.BuildAttempts = 0
v.NewVersion = ""
}
item.Source.Packages.Set(k, v)
return true
})
}
func StartQueueAndStatusWorker(ctx context.Context) {
go processQueueAndStatus(ctx)
}
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
var builds []struct {
isMatch bool
status string
time time.Time
}
var f func(*html.Node) error
f = func(n *html.Node) error {
if n.Type == html.ElementNode && n.Data == "div" {
for _, a := range n.Attr {
if a.Key == "class" && a.Val == "flex-item tw-items-center" {
isMatch, status, buildTime := checkBuildBlock(n, buildName)
if isMatch {
builds = append(builds, struct {
isMatch bool
status string
time time.Time
}{isMatch, status, buildTime})
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if err := f(c); err != nil {
return err
}
}
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)
return false, nil
}
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
case "Waiting":
return false, nil // Build is waiting to start
default:
slog.Warn("Unknown build status", "status", mostRecentBuild.status)
return false, fmt.Errorf("unknown build status: %s", mostRecentBuild.status)
}
}
func checkBuildBlock(n *html.Node, buildName string) (bool, string, time.Time) {
var title string
var status string
var buildTime time.Time
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
}
}
}
}
case "div":
for _, a := range n.Attr {
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
}
}
}
}
}
}
case "relative-time":
for _, a := range n.Attr {
if a.Key == "datetime" {
buildTime, _ = time.Parse(time.RFC3339, a.Val)
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(n)
return title == buildName, status, buildTime
}