Merge pull request 'Pipeline runner tools: runner_cancel, the runner's cancel is reachable as a tool' (#233) from fix/pipeline-runner-ends-stale into main
This commit was merged in pull request #233.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// CancelTool is runner_cancel: the runner's seat verb cancel, reachable as a tool (design 47 G §482). It lists the pipelines
|
||||
// through the bundle's reader (the one the verb list uses), picks the running ones named, or the running ones of a branch
|
||||
// whose source is below a bound, and asks the seat verb cancel for each over the bus; the verb records the hand act and says
|
||||
// pipeline-ended. A pipeline that is not running is never asked.
|
||||
func CancelTool(list func(seat.ListInput) (seat.ListAnswer, error), ask func(key string, body any) (json.RawMessage, error)) stdio.Tool {
|
||||
return stdio.Tool{Name: Prefix + "_cancel",
|
||||
Description: "Cancel running pipelines by the seat verb cancel: those named, or those of a branch (default main) whose source is below below_source; exactly one of the two. It names why, and dry_run lists what it would cancel. A pipeline that is not running is refused, never asked.",
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{
|
||||
"pipelines": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
|
||||
"below_source": map[string]any{"type": "integer"},
|
||||
"branch": map[string]any{"type": "string"},
|
||||
"why": map[string]any{"type": "string"},
|
||||
"dry_run": map[string]any{"type": "boolean"},
|
||||
}, "required": []string{"why"}},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
in, err := as[CancelInput](args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return in.run(list, ask)
|
||||
}}
|
||||
}
|
||||
|
||||
// CancelInput is what runner_cancel takes.
|
||||
type CancelInput struct {
|
||||
Pipelines []string `json:"pipelines,omitempty"`
|
||||
BelowSource *int64 `json:"below_source,omitempty"`
|
||||
Branch string `json:"branch,omitempty"`
|
||||
Why string `json:"why"`
|
||||
DryRun bool `json:"dry_run,omitempty"`
|
||||
}
|
||||
|
||||
// CancelResult is one pipeline's end of runner_cancel: cancelled, would-cancel (dry run) or refused whole.
|
||||
type CancelResult struct {
|
||||
Pipeline string `json:"pipeline"`
|
||||
Result string `json:"result"`
|
||||
Refusal string `json:"refusal,omitempty"`
|
||||
}
|
||||
|
||||
func (in CancelInput) run(list func(seat.ListInput) (seat.ListAnswer, error), ask func(string, any) (json.RawMessage, error)) (any, error) {
|
||||
if strings.TrimSpace(in.Why) == "" {
|
||||
return nil, errors.New("cancel names why")
|
||||
}
|
||||
if (len(in.Pipelines) > 0) == (in.BelowSource != nil) {
|
||||
return nil, errors.New("cancel takes exactly one of pipelines and below_source")
|
||||
}
|
||||
branch := in.Branch
|
||||
if branch == "" {
|
||||
branch = "main"
|
||||
}
|
||||
listed := seat.ListInput{}
|
||||
if in.BelowSource != nil {
|
||||
listed.Branch = branch
|
||||
}
|
||||
all, err := list(listed)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the pipelines are not read: %w", err)
|
||||
}
|
||||
byKey := map[string]seat.Pipeline{}
|
||||
for _, p := range all.Pipelines {
|
||||
byKey[p.Pipeline] = p
|
||||
}
|
||||
var results []CancelResult
|
||||
var picked []string
|
||||
if in.BelowSource != nil {
|
||||
for _, p := range all.Pipelines {
|
||||
if p.Outcome == seat.PipelineOutcomeRunning && p.Source > 0 && p.Source < *in.BelowSource {
|
||||
picked = append(picked, p.Pipeline)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, name := range in.Pipelines {
|
||||
p, ok := byKey[name]
|
||||
switch {
|
||||
case !ok:
|
||||
results = append(results, CancelResult{Pipeline: name, Result: "refused", Refusal: "no such pipeline"})
|
||||
case p.Outcome != seat.PipelineOutcomeRunning:
|
||||
results = append(results, CancelResult{Pipeline: name, Result: "refused", Refusal: "the pipeline is " + p.Outcome + ", not running"})
|
||||
default:
|
||||
picked = append(picked, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
counts := map[string]int{}
|
||||
for _, name := range picked {
|
||||
if in.DryRun {
|
||||
results = append(results, CancelResult{Pipeline: name, Result: "would-cancel"})
|
||||
} else if _, err := ask("seat:"+seat.Seat+"."+seat.VerbCancel, seat.CancelInput{Pipeline: name, Why: in.Why}); err != nil {
|
||||
results = append(results, CancelResult{Pipeline: name, Result: "refused", Refusal: err.Error()})
|
||||
} else {
|
||||
results = append(results, CancelResult{Pipeline: name, Result: "cancelled"})
|
||||
}
|
||||
}
|
||||
for _, r := range results {
|
||||
counts[r.Result]++
|
||||
}
|
||||
return map[string]any{"results": results, "counts": counts}, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
||||
)
|
||||
|
||||
// runner_cancel picks the running pipelines named or of the branch below a source, asks the seat verb cancel for each
|
||||
// and never asks for one not running (design 47 G §482).
|
||||
func TestCancelTool(t *testing.T) {
|
||||
pl := func(key, branch, outcome string, source int64) seat.Pipeline {
|
||||
return seat.Pipeline{Pipeline: key, Branch: branch, Outcome: outcome, Source: source}
|
||||
}
|
||||
all := []seat.Pipeline{pl("main.1.a", "main", "running", 1), pl("main.2.b", "main", "running", 5),
|
||||
pl("main.1.c", "main", "passed", 1), pl("x.1.d", "x", "running", 1)}
|
||||
list := func(in seat.ListInput) (seat.ListAnswer, error) {
|
||||
var out []seat.Pipeline
|
||||
for _, p := range all {
|
||||
if in.Branch == "" || p.Branch == in.Branch {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return seat.ListAnswer{Pipelines: out}, nil
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name, args, asked, counts, err string
|
||||
}{
|
||||
{"named", `{"pipelines":["main.1.a","main.1.c","nope"],"why":"w"}`, "main.1.a", `"cancelled":1`, ""},
|
||||
{"below", `{"below_source":5,"why":"w"}`, "main.1.a", `"cancelled":1`, ""},
|
||||
{"below other branch", `{"below_source":5,"branch":"x","why":"w"}`, "x.1.d", `"cancelled":1`, ""},
|
||||
{"dry run", `{"below_source":9,"why":"w","dry_run":true}`, "", `"would-cancel":2`, ""},
|
||||
{"refusal whole", `{"pipelines":["main.2.b"],"why":"boom"}`, "main.2.b", `"refused":1`, ""},
|
||||
{"no why", `{"below_source":5}`, "", "", "why"},
|
||||
{"both", `{"below_source":5,"pipelines":["main.1.a"],"why":"w"}`, "", "", "exactly one"},
|
||||
{"neither", `{"why":"w"}`, "", "", "exactly one"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
var asked []string
|
||||
ask := func(key string, body any) (json.RawMessage, error) {
|
||||
in := body.(seat.CancelInput)
|
||||
asked = append(asked, in.Pipeline)
|
||||
if key != "seat:mesh-pipeline-runner.cancel" {
|
||||
t.Errorf("key %q", key)
|
||||
}
|
||||
if in.Why == "boom" {
|
||||
return nil, errors.New("the runner refuses")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
var args map[string]any
|
||||
_ = json.Unmarshal([]byte(c.args), &args)
|
||||
got, err := CancelTool(list, ask).Run(args)
|
||||
if c.err != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), c.err) || len(asked) > 0 {
|
||||
t.Fatalf("err %v, asked %v; want %q", err, asked, c.err)
|
||||
}
|
||||
return
|
||||
}
|
||||
raw, _ := json.Marshal(got)
|
||||
if err != nil || strings.Join(asked, " ") != c.asked || !strings.Contains(string(raw), c.counts) {
|
||||
t.Fatalf("%s, %v, asked %v; want %q and %s", raw, err, asked, c.asked, c.counts)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -151,6 +151,7 @@ func main() {
|
||||
tools := append(Tools(r), FirstViewTools(os.Getenv(stdio.ServedAs), stdio.State(views.StatusState), meshhandactsclient.Recorder{Ask: stdio.Ask}, time.Now)...)
|
||||
tools = append(tools, UnloggedTools(stdio.State(seat.StatePipelines), stdio.Log(seat.StateStepLog), meshhandactsclient.Recorder{Ask: stdio.Ask},
|
||||
func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-pipeline-tools] "+f+"\n", a...) })...)
|
||||
tools = append(tools, CancelTool(r.List, stdio.Ask))
|
||||
tools = append(tools, HealthTool(r.Pipelines, kept{stdio.State(pipeline.StateConsumers)}, time.Now))
|
||||
if err := stdio.Serve("", tools); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[mesh-delivery-pipeline-tools] %v\n", err)
|
||||
|
||||
@@ -31,13 +31,14 @@ func TestTools(t *testing.T) {
|
||||
for _, tl := range FirstViewTools("the-runner", stdio.State(views.StatusState), nil, nil) {
|
||||
names = append(names, tl.Name)
|
||||
}
|
||||
names = append(names, CancelTool(nil, nil).Name)
|
||||
// RN25 (§468, §76; A-164): the health check is the bundle's runner_health.
|
||||
names = append(names, HealthTool(none{}, none{}, nil).Name)
|
||||
// §606, §549: the unlogged entries are listed and appended by hand.
|
||||
for _, tl := range UnloggedTools(stdio.State("pipelines"), stdio.Log("step-log"), nil, nil) {
|
||||
names = append(names, tl.Name)
|
||||
}
|
||||
want := "mesh-pipeline-runner.show mesh-pipeline-runner.list mesh-pipeline-runner.stands mesh-pipeline-runner.why-carried mesh-pipeline-runner.kept runner_first_views runner_first_view_again runner_health runner_unlogged runner_unlogged_append"
|
||||
want := "mesh-pipeline-runner.show mesh-pipeline-runner.list mesh-pipeline-runner.stands mesh-pipeline-runner.why-carried mesh-pipeline-runner.kept runner_first_views runner_first_view_again runner_cancel runner_health runner_unlogged runner_unlogged_append"
|
||||
if got := strings.Join(names, " "); got != want {
|
||||
t.Errorf("tools %q, want %q", got, want)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user