runner: gate stage handoff on settled verified session export
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
)
|
||||
|
||||
func completeSessionExport(e *protocol.SessionExport) bool {
|
||||
return e != nil && e.Status == "complete" && e.SessionID != "" && e.Messages > 0 && e.HTML != "" && e.HTMLBytes > 0 && !e.Truncated && e.Diagnostic == ""
|
||||
}
|
||||
|
||||
// resumeExportGates is also run when no job is due: a manual export repair or
|
||||
// a crash after persisting repair evidence may have made a handoff eligible.
|
||||
func (r *runner) resumeExportGates(ctx context.Context) error {
|
||||
ready, err := r.store.ReadySessionExportGates(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for id, sequence := range ready {
|
||||
r.mu.Lock()
|
||||
if _, active := r.active[id]; active {
|
||||
r.mu.Unlock()
|
||||
continue
|
||||
}
|
||||
d, err := r.store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
return err
|
||||
}
|
||||
if err := r.checkWorkspaceFreeExcept(d.Workspace, id); err != nil {
|
||||
r.mu.Unlock()
|
||||
continue
|
||||
}
|
||||
owner, err := r.store.RepairSessionOwner(ctx, id, sequence)
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
continue
|
||||
}
|
||||
job, err := r.store.SessionExportJob(ctx, id, sequence)
|
||||
if err != nil || job == nil || job.SessionID != owner.ID || job.Backend != owner.Backend || job.Directory != owner.Directory || job.Reference != owner.Reference {
|
||||
r.mu.Unlock()
|
||||
continue
|
||||
}
|
||||
var saved *protocol.RunStep
|
||||
for i := range d.Steps {
|
||||
if d.Steps[i].Sequence == sequence {
|
||||
saved = &d.Steps[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
verified := false
|
||||
if saved != nil {
|
||||
for _, e := range saved.SessionExportRepairs {
|
||||
if !completeSessionExport(e) || e.SessionID != owner.ID || e.Backend != owner.Backend || e.Directory != owner.Directory {
|
||||
continue
|
||||
}
|
||||
// New repairs stay private in the original visit. Historical
|
||||
// public repairs remain valid, but are never rewritten there.
|
||||
directory := r.turnPaths(id, sequence, saved.Stage).TurnDir
|
||||
if strings.HasPrefix(e.HTML, "public/") {
|
||||
directory = filepath.Join(r.artifactsRoot, strconv.FormatInt(id, 10))
|
||||
} else if filepath.Base(e.HTML) != e.HTML || !strings.HasPrefix(e.HTML, "session-repair-") {
|
||||
continue
|
||||
}
|
||||
if filepath.Clean(e.HTML) != e.HTML {
|
||||
continue
|
||||
}
|
||||
root, openErr := os.OpenRoot(directory)
|
||||
if openErr != nil {
|
||||
continue
|
||||
}
|
||||
info, statErr := root.Lstat(e.HTML)
|
||||
root.Close()
|
||||
if statErr == nil && info.Mode().IsRegular() && info.Size() == e.HTMLBytes && info.Size() > 0 && info.Size() <= stage.SessionExportFileLimit {
|
||||
verified = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !verified {
|
||||
r.mu.Unlock()
|
||||
continue
|
||||
}
|
||||
if err := r.store.ResumeSessionExportGate(ctx, id, sequence); err != nil {
|
||||
r.mu.Unlock()
|
||||
continue
|
||||
}
|
||||
step := *saved
|
||||
r.launch(id, d.Workspace, func(runCtx context.Context) { r.resume(runCtx, id, d, d.Workspace, step) })
|
||||
r.mu.Unlock()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user