Add queue details UI for admins (#1632)

# Changes
- Adds an admin view to see the whole work-queue of the server. 
- The admin can also pause / resume the queue. 
- The view is reloading data every 5 seconds automatically.
- The task model from queue got removed in favor of the one from models.
This commit is contained in:
Anbraten
2023-03-20 04:50:56 +01:00
committed by GitHub
parent 4d5c59556e
commit 2337f1854a
19 changed files with 432 additions and 265 deletions

View File

@@ -3,7 +3,6 @@ package queue
import (
"context"
"errors"
"fmt"
"strings"
"github.com/woodpecker-ci/woodpecker/server/model"
@@ -17,87 +16,11 @@ var (
ErrNotFound = errors.New("queue: task not found")
)
// Task defines a unit of work in the queue.
type Task struct {
// ID identifies this task.
ID string `json:"id,omitempty"`
// Data is the actual data in the entry.
Data []byte `json:"data"`
// Labels represents the key-value pairs the entry is labeled with.
Labels map[string]string `json:"labels,omitempty"`
// Task IDs this task depend
Dependencies []string
// Dependency's exit status
DepStatus map[string]string
// RunOn failure or success
RunOn []string
}
// ShouldRun tells if a task should be run or skipped, based on dependencies
func (t *Task) ShouldRun() bool {
if runsOnFailure(t.RunOn) && runsOnSuccess(t.RunOn) {
return true
}
if !runsOnFailure(t.RunOn) && runsOnSuccess(t.RunOn) {
for _, status := range t.DepStatus {
if StatusSuccess != status {
return false
}
}
return true
}
if runsOnFailure(t.RunOn) && !runsOnSuccess(t.RunOn) {
for _, status := range t.DepStatus {
if StatusSuccess == status {
return false
}
}
return true
}
return false
}
func (t *Task) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s (%s) - %s", t.ID, t.Dependencies, t.DepStatus))
return sb.String()
}
func runsOnFailure(runsOn []string) bool {
for _, status := range runsOn {
if status == "failure" {
return true
}
}
return false
}
func runsOnSuccess(runsOn []string) bool {
if len(runsOn) == 0 {
return true
}
for _, status := range runsOn {
if status == "success" {
return true
}
}
return false
}
// InfoT provides runtime information.
type InfoT struct {
Pending []*Task `json:"pending"`
WaitingOnDeps []*Task `json:"waiting_on_deps"`
Running []*Task `json:"running"`
Pending []*model.Task `json:"pending"`
WaitingOnDeps []*model.Task `json:"waiting_on_deps"`
Running []*model.Task `json:"running"`
Stats struct {
Workers int `json:"worker_count"`
Pending int `json:"pending_count"`
@@ -105,7 +28,7 @@ type InfoT struct {
Running int `json:"running_count"`
Complete int `json:"completed_count"`
} `json:"stats"`
Paused bool
Paused bool `json:"paused"`
}
func (t *InfoT) String() string {
@@ -128,19 +51,19 @@ func (t *InfoT) String() string {
// Filter filters tasks in the queue. If the Filter returns false,
// the Task is skipped and not returned to the subscriber.
type FilterFn func(*Task) bool
type FilterFn func(*model.Task) bool
// Queue defines a task queue for scheduling tasks among
// a pool of workers.
type Queue interface {
// Push pushes a task to the tail of this queue.
Push(c context.Context, task *Task) error
Push(c context.Context, task *model.Task) error
// PushAtOnce pushes a task to the tail of this queue.
PushAtOnce(c context.Context, tasks []*Task) error
PushAtOnce(c context.Context, tasks []*model.Task) error
// Poll retrieves and removes a task head of this queue.
Poll(c context.Context, f FilterFn) (*Task, error)
Poll(c context.Context, f FilterFn) (*model.Task, error)
// Extend extends the deadline for a task.
Extend(c context.Context, id string) error