vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Retire the queued runs of an event a newer one replaced
8ca8bbe04848a350f514eb956f70951b701bcd9c · Keenan Nemetz · 10d ago · parent 3031e357
Retire the queued runs of an event a newer one replaced Opening a change request and then updating it queued two full sets of the workflow's jobs for the same base commit. Neither superseded the other: the occurrence is what makes event runs distinct, and CancelSuperseded only looks at pushes. On a single-slot runner the newer event waited behind the older event's identical work, and every edit added another set. A run now records the subject of the event that queued it: a change-request ID, an issue ID, or an entity and comment pair. When an event's batch is queued, the queued runs of an older event for the same repository, trigger, and subject are canceled. The batch's own occurrences are kept, so an edited and an assigned action from one push do not cancel each other. Only queued rows are touched: a handler that has started finishes, because it is already acting on the event it received. Schedules and dispatches have no subject and are unchanged. The query needs no ref: the subject is a globally unique entity ID, so a base branch that moved does not keep the older set alive, and another change request's queue on the same branch is untouched. Verified with go test ./server/, the store tests, and vole workflow run verify.
ci/runs/runs.go +53−6
| @@ -118,7 +118,12 @@ type Run struct { | |||
| 118 | 118 | TriggerAction string | |
| 119 | 119 | EventPayload string | |
| 120 | 120 | Occurrence string | |
| 121 | Steps []Step | ||
| 121 | // Subject is the entity whose event queued the run: a change-request ID, an | ||
| 122 | // issue ID, or an entity and comment pair. It is empty for a push, schedule, or | ||
| 123 | // manual dispatch. A newer event for the same subject supersedes the queued runs | ||
| 124 | // of the older one, so the subject is stored rather than parsed out of Occurrence. | ||
| 125 | Subject string | ||
| 126 | Steps []Step | ||
| 122 | 127 | // StepsSHA is the digest of the pinned steps, recorded when the run is queued. | |
| 123 | 128 | // It is what a runner's attestation covers, so "what ran" is the steps and not | |
| 124 | 129 | // merely the workflow file they were read from. | |
| @@ -210,7 +215,7 @@ type Run struct { | |||
| 210 | 215 | ||
| 211 | 216 | // runColumns is the column list scanRun reads, in order. HasLog is an EXISTS rather | |
| 212 | 217 | // than a stored column so a list of runs stays one query as logs come and go. | |
| 213 | const runColumns = `id, repo, ref, branch, sha, workflow, workflow_sha, workflow_name, workflow_description, job, job_name, job_description, trigger_kind, event_action, event_payload, occurrence, steps, steps_sha, | ||
| 218 | const runColumns = `id, repo, ref, branch, sha, workflow, workflow_sha, workflow_name, workflow_description, job, job_name, job_description, trigger_kind, event_action, event_payload, occurrence, subject, steps, steps_sha, | ||
| 214 | 219 | artifacts, release_artifacts, pages_path, tag, tag_annotated, secrets, services, status, attempts, lease_owner, lease_until, created, started, finished, exit_code, os, arch, image, image_file, image_definition, image_sha, runtime, network, timeout, bot, env, cancel_requested, cancel_actor, failure, failure_message, | |
| 215 | 220 | EXISTS (SELECT 1 FROM run_logs WHERE run_logs.run_id = runs.id)` | |
| 216 | 221 | ||
| @@ -377,6 +382,7 @@ CREATE TABLE IF NOT EXISTS runs ( | |||
| 377 | 382 | event_action TEXT NOT NULL DEFAULT '', | |
| 378 | 383 | event_payload TEXT NOT NULL DEFAULT '', | |
| 379 | 384 | occurrence TEXT NOT NULL DEFAULT '', | |
| 385 | subject TEXT NOT NULL DEFAULT '', | ||
| 380 | 386 | steps TEXT NOT NULL, | |
| 381 | 387 | status TEXT NOT NULL, | |
| 382 | 388 | attempts INTEGER NOT NULL DEFAULT 0, | |
| @@ -611,6 +617,7 @@ CREATE TABLE IF NOT EXISTS auto_merges ( | |||
| 611 | 617 | `ALTER TABLE runs ADD COLUMN event_action TEXT NOT NULL DEFAULT ''`, | |
| 612 | 618 | `ALTER TABLE runs ADD COLUMN event_payload TEXT NOT NULL DEFAULT ''`, | |
| 613 | 619 | `ALTER TABLE runs ADD COLUMN occurrence TEXT NOT NULL DEFAULT ''`, | |
| 620 | `ALTER TABLE runs ADD COLUMN subject TEXT NOT NULL DEFAULT ''`, | ||
| 614 | 621 | `ALTER TABLE runner_sessions ADD COLUMN labels TEXT NOT NULL DEFAULT '[]'`, | |
| 615 | 622 | `ALTER TABLE run_attestations ADD COLUMN commit_id TEXT NOT NULL DEFAULT ''`, | |
| 616 | 623 | `ALTER TABLE runs ADD COLUMN failure TEXT NOT NULL DEFAULT ''`, | |
| @@ -624,6 +631,13 @@ CREATE TABLE IF NOT EXISTS auto_merges ( | |||
| 624 | 631 | return nil, err | |
| 625 | 632 | } | |
| 626 | 633 | } | |
| 634 | // The queue retires an older event's queued runs by subject, so the lookup that | ||
| 635 | // finds them should not scan every run for the repository. | ||
| 636 | if _, err := db.Exec(`CREATE INDEX IF NOT EXISTS runs_subject | ||
| 637 | ON runs(repo, trigger_kind, subject, status)`); err != nil { | ||
| 638 | db.Close() | ||
| 639 | return nil, err | ||
| 640 | } | ||
| 627 | 641 | // The unique key is what makes ingestion idempotent. A push has one occurrence | |
| 628 | 642 | // (the empty string); an event trigger names its occurrence so a later issue | |
| 629 | 643 | // edit or schedule slot can run the same commit again. | |
| @@ -745,13 +759,13 @@ func (s *Store) Enqueue(r Run, labels []string) (bool, error) { | |||
| 745 | 759 | res, err := tx.Exec(`INSERT OR IGNORE INTO runs | |
| 746 | 760 | (id, repo, ref, branch, tag, tag_annotated, sha, workflow, workflow_sha, | |
| 747 | 761 | workflow_name, workflow_description, job, job_name, job_description, | |
| 748 | trigger_kind, event_action, event_payload, occurrence, steps, steps_sha, | ||
| 762 | trigger_kind, event_action, event_payload, occurrence, subject, steps, steps_sha, | ||
| 749 | 763 | image, image_file, image_definition, image_sha, | |
| 750 | 764 | artifacts, release_artifacts, pages_path, secrets, services, env, network, timeout, bot, status, created) | |
| 751 | VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, | ||
| 765 | VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, | ||
| 752 | 766 | r.ID, r.Repo, r.Ref, r.Branch, r.Tag, boolInt(r.TagAnnotated), r.SHA, r.Workflow, | |
| 753 | 767 | r.WorkflowSHA, r.WorkflowName, r.WorkflowDescription, r.Job, r.JobName, r.JobDescription, | |
| 754 | r.Trigger, r.TriggerAction, r.EventPayload, r.Occurrence, | ||
| 768 | r.Trigger, r.TriggerAction, r.EventPayload, r.Occurrence, r.Subject, | ||
| 755 | 769 | string(steps), r.StepsSHA, | |
| 756 | 770 | r.Image, r.ImageFile, r.ImageDefinition, r.ImageSHA, | |
| 757 | 771 | string(arts), string(rel), r.PagesPath, string(secrets), string(services), string(env), r.Network, r.Timeout, r.Bot, r.Status, r.Created.Unix()) | |
| @@ -930,6 +944,39 @@ func (s *Store) CancelSuperseded(repo, ref, keep string, now time.Time) (int, er | |||
| 930 | 944 | return n, nil | |
| 931 | 945 | } | |
| 932 | 946 | ||
| 947 | // CancelSupersededEvents retires the queued runs of an older event for the same | ||
| 948 | // subject: a change request, an issue, or one comment. Only waiting rows are touched; | ||
| 949 | // a handler that is already running finishes, because it is already acting on the | ||
| 950 | // event it received. keep names the occurrences the caller is queueing now, so the | ||
| 951 | // actions of one event (edited and assigned, say) do not cancel each other. The | ||
| 952 | // subject is a globally unique entity id, so the query needs no ref: a base branch | ||
| 953 | // that moved must not keep the older queued set alive. | ||
| 954 | func (s *Store) CancelSupersededEvents(repo, trigger, subject string, keep []string, now time.Time) (int, error) { | ||
| 955 | if repo == "" || trigger == "" || subject == "" || len(keep) == 0 { | ||
| 956 | return 0, nil | ||
| 957 | } | ||
| 958 | placeholders := strings.TrimSuffix(strings.Repeat("?,", len(keep)), ",") | ||
| 959 | args := make([]any, 0, 6+len(keep)) | ||
| 960 | args = append(args, StatusCanceled, now.Unix(), repo, trigger, subject, StatusQueued) | ||
| 961 | for _, occurrence := range keep { | ||
| 962 | args = append(args, occurrence) | ||
| 963 | } | ||
| 964 | res, err := s.db.Exec(`UPDATE runs SET status = ?, finished = ?, lease_owner = '', lease_until = 0 | ||
| 965 | WHERE repo = ? AND trigger_kind = ? AND subject = ? AND status = ? | ||
| 966 | AND occurrence NOT IN (`+placeholders+`)`, args...) | ||
| 967 | if err != nil { | ||
| 968 | return 0, err | ||
| 969 | } | ||
| 970 | n, err := res.RowsAffected() | ||
| 971 | if err != nil { | ||
| 972 | return 0, err | ||
| 973 | } | ||
| 974 | if n > 0 { | ||
| 975 | s.notify() | ||
| 976 | } | ||
| 977 | return int(n), nil | ||
| 978 | } | ||
| 979 | |||
| 933 | 980 | // Session is one connected runner as the queue sees it: who it authenticated as, | |
| 934 | 981 | // what it is building (if anything), and when it was last heard from. Presence is a | |
| 935 | 982 | // derived fact about live channels, so a row that stops being refreshed is a runner | |
| @@ -2253,7 +2300,7 @@ func scanRun(row scannable) (*Run, error) { | |||
| 2253 | 2300 | var env, secrets, services string | |
| 2254 | 2301 | if err := row.Scan(&r.ID, &r.Repo, &r.Ref, &r.Branch, &r.SHA, &r.Workflow, &r.WorkflowSHA, | |
| 2255 | 2302 | &r.WorkflowName, &r.WorkflowDescription, &r.Job, &r.JobName, &r.JobDescription, | |
| 2256 | &r.Trigger, &r.TriggerAction, &r.EventPayload, &r.Occurrence, | ||
| 2303 | &r.Trigger, &r.TriggerAction, &r.EventPayload, &r.Occurrence, &r.Subject, | ||
| 2257 | 2304 | &steps, &r.StepsSHA, &artifacts, &releaseArtifacts, &r.PagesPath, &r.Tag, &tagAnnotated, | |
| 2258 | 2305 | &secrets, &services, &r.Status, &r.Attempts, &r.LeaseOwner, &leaseUntil, | |
| 2259 | 2306 | &created, &started, &finished, &r.ExitCode, &r.OS, &r.Arch, | |