Simplify Workers

This commit is contained in:
ferreo 2024-07-30 01:30:26 +01:00
parent 8f7657cf38
commit fd9e9418b2
2 changed files with 39 additions and 63 deletions

View File

@ -82,15 +82,7 @@ func StartPackageQueueWorker(ctx context.Context) {
}()
}
func StartQueueWorker(ctx context.Context) {
go processQueue(ctx)
}
func StartStatusWorker(ctx context.Context) {
go processStatus(ctx)
}
func processStatus(ctx context.Context) {
func processQueueAndStatus(ctx context.Context) {
for {
select {
case <-ctx.Done():
@ -98,74 +90,59 @@ func processStatus(ctx context.Context) {
default:
q := GetQueue()
itemsToRemove := make([]string, 0)
buildingFound := false
q.ForEach(func(k string, item domain.BuildQueueItem) bool {
if item.Status != domain.Building {
return true
}
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 {
item.Source.Packages.ForEach(func(k string, v domain.PackageInfo) bool {
v.Status = domain.Error
v.LastBuildStatus = domain.Error
item.Source.Packages.Set(k, v)
return true
})
packages.UpdateSourcePackage(item.Source)
itemsToRemove = append(itemsToRemove, k)
return true
updatePackageStatus(&item, domain.Error, domain.Error)
} else {
updatePackageStatus(&item, domain.Current, domain.Built)
}
item.Source.Packages.ForEach(func(k string, v domain.PackageInfo) bool {
v.Status = domain.Current
v.LastBuildStatus = domain.Built
v.Version = item.BuildVersion
v.NewVersion = ""
item.Source.Packages.Set(k, v)
return true
})
packages.UpdateSourcePackage(item.Source)
itemsToRemove = append(itemsToRemove, k)
return true
}
}
return true
})
for _, item := range itemsToRemove {
Remove(item)
}
time.Sleep(10 * time.Second)
}
}
}
func processQueue(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
q := GetQueue()
buildingFound := false
q.ForEach(func(k string, item domain.BuildQueueItem) bool {
if item.Status == domain.Building {
buildingFound = true
return false
}
return true
})
if buildingFound {
time.Sleep(30 * time.Second)
continue
}
if !buildingFound {
err := ProcessNext()
if err != nil {
slog.Error("unable to process queue: " + err.Error())
}
}
time.Sleep(30 * time.Second)
time.Sleep(10 * time.Second)
}
}
}
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.LastBuildStatus = domain.PackageStatus(buildStatus)
if status == domain.Current {
v.Version = item.BuildVersion
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) {

View File

@ -65,8 +65,7 @@ func runServer(ctx context.Context) error {
slog.Info("packages loaded in " + time.Since(start).String())
buildqueue.StartPackageQueueWorker(ctx)
buildqueue.StartQueueWorker(ctx)
buildqueue.StartStatusWorker(ctx)
buildqueue.StartQueueAndStatusWorker(ctx)
cfg := fiber.Config{
JSONEncoder: json.Marshal,