package steprun import ( "encoding/json" "fmt" buildrun "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildrun" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" ) // **A holder takes every asker's kind** (design 47 H §752, H-A-41: every account may call every verb; the asker judgement // goes). An ask rides `mesh.seat..accept..`, the asker the asking seat's own name, and its kind stays in // its body; the holder keys the step under that asker (design 47 G §481). On main's subject, which names no asker, it takes // only a build and a run (design 47 G §543, A-184). // KindOf is the word the seat definition gives an ask's kind: run for a run or a compose, else the ask's own kind. func KindOf(ask sdkstep.StepAsk) string { if ask.Run != nil || ask.Compose != nil { return "run" } return ask.Kind } // askerRefusal is why an ask must not be taken from its subject: "" when it may be. Any named asker may ask any kind. func (p *Puller) askerRefusal(msg *Pulled, ask sdkstep.StepAsk) (why string) { if msg.Subject == "" { return "" // a source that gives no subject (a test's) } _, _, asker, err := sdkstep.ParseAskSubjectBy(msg.Subject) if err != nil { return fmt.Sprintf("the ask came on %s, which is no accept subject of this seat", msg.Subject) } kind := KindOf(ask) if asker == "" { // Main's subject takes a build, a run and, until it goes, the checker's check and recheck (design 47 G A-184, §543). if (ask.Build != nil && ask.Kind == sdkstep.AskDelivery) || kind == "run" || kind == sdkstep.AskCheck || kind == sdkstep.AskRecheck { return "" } return fmt.Sprintf("the kind %s is not taken on main's subject, which takes only a build, a run and the checker's check and recheck (asker: none named)", kind) } return "" } // refuseAsker ends an ask whose asker may not ask its kind: its step key says failed with the reason, naming the kind and the // asker, step-ended says failed, and the message is dropped. func (p *Puller) refuseAsker(msg *Pulled, ask sdkstep.StepAsk, why string) { key, err := sdkstep.AskStepKey(keyAsker(msg), p.Node, ask.ID) if err == nil { at := now().UTC() step := sdkstep.Step{ID: ask.ID, Tool: ask.Kind + " ask", Node: p.Node, State: sdkstep.StepFailed, Started: at, Ended: at, Reason: "error: " + why, Owner: buildrun.Owner, Run: int(msg.Delivered)} err = writeEnd(p.Put, key, step) } if err != nil { p.logf("the ask %s of the wrong asker cannot be answered on %s, so it is given back: %v", ask.ID, p.Node, err) _ = msg.Nak() return } p.logf("the ask %s is refused on %s: %s", ask.ID, p.Node, why) p.sayAskEnded(msg, ask, sdkstep.OutcomeFailed, why) _ = msg.Ack() // A-489: ended and written, so it leaves the work queue } // sayAskEnded says the end of an ask this holder ends without running it: its step-ended, an ask outside a pipeline as // sayEnded says it, a pipeline step as its own form (design 47 G §602), so the runner that asked hears it. func (p *Puller) sayAskEnded(msg *Pulled, ask sdkstep.StepAsk, outcome, said string) { if ask.Step == nil { p.sayEnded(msg, ask, outcome, said, nil, "") } else if end, err := sdkstep.EndOf(ask, p.Node, outcome); err == nil { // A pipeline step's end names its pipeline, step, commit and fingerprint (its own form, §602). end.Said, end.At, end.Asker = said, now().UTC(), askerOf(msg, ask) if body, err := sdkstep.EncodeStepEnded(end); err == nil { p.say("step-ended", json.RawMessage(body), "step-ended."+ask.ID) } } } // PriorRuns is Puller.Prior over a steps state's read (a module's ReadSteps.Get): the Run of the step a key holds. Nil for no // read (a test's), so the bus's delivery count stands. func PriorRuns(get func(key string) (json.RawMessage, error)) func(string) int { if get == nil { return nil } return func(key string) int { raw, err := get(key) if err != nil || raw == nil { return 0 } var s sdkstep.Step if json.Unmarshal(raw, &s) != nil { return 0 } return s.Run } }