oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "encoding/json"
6
7 "amadan.net/oficina/equipo/internal/equipo/team"
8 "gorm.io/gorm"
9)
10
11type runnerAdapter struct {
12 store *Store
13 operation team.Operation
14}
15
16func (s *Store) RunnerAdapter(op team.Operation) team.Adapter {
17 return runnerAdapter{store: s, operation: op}
18}
19func (a runnerAdapter) Classify(op team.Operation) (team.ReplayClass, error) {
20 if op != a.operation {
21 return "", team.ErrUnknownKind
22 }
23 switch op {
24 case team.OpForgePublish:
25 return team.ReplayReconcilable, nil
26 case team.OpProviderCall:
27 return team.ReplayUnsafe, nil
28 }
29 return "", team.ErrUnknownKind
30}
31func (a runnerAdapter) Execute(context.Context, team.Dispatch) (team.Receipt, error) {
32 return team.Receipt{}, team.ErrState
33}
34func (a runnerAdapter) Reconcile(ctx context.Context, d team.Dispatch) (team.ReconcileResult, error) {
35 if _, err := a.Classify(d.Operation); err != nil {
36 return team.ReconcileResult{}, err
37 }
38 var action executionActions
39 // The action's stored payload and attempt bind every lookup, including after
40 // cancellation. Raw runner observations are never receipts.
41 if err := a.store.db.G.WithContext(ctx).First(&action, "id=?", d.Action).Error; err != nil {
42 return team.ReconcileResult{}, storeError(err)
43 }
44 if action.Key != d.Key || action.Operation != string(d.Operation) || action.PayloadHash != d.Hash || string(action.Payload) != string(d.Payload) {
45 return team.ReconcileResult{}, ErrConflict
46 }
47 var rows []runnerObservation
48 err := a.store.db.G.WithContext(ctx).Where("operation=? AND assignment_id IN (SELECT id FROM runner_assignments WHERE job_id=? AND attempt_id=? AND fence=?)", "verified_receipt", action.JobID, action.AttemptID, action.Fence).Order("at, request_id").Find(&rows).Error
49 if err != nil {
50 return team.ReconcileResult{}, storeError(err)
51 }
52 var found *team.Receipt
53 for _, row := range rows {
54 var q RunnerRequest
55 if json.Unmarshal([]byte(row.Payload), &q) != nil {
56 return team.ReconcileResult{}, ErrUnavailable
57 }
58 if q.ActionID != d.Action {
59 continue
60 }
61 var assignment runnerAssignment
62 if err := a.store.db.G.WithContext(ctx).First(&assignment, "id=?", row.AssignmentID).Error; err != nil {
63 return team.ReconcileResult{}, storeError(err)
64 }
65 if !binding(assignment, q) || q.Scope.Job != team.JobID(action.JobID) || q.Handle.Attempt != team.AttemptID(action.AttemptID) || q.Handle.Fence != action.Fence || q.Receipt == nil {
66 return team.ReconcileResult{}, ErrConflict
67 }
68 if d.Operation == team.OpForgePublish {
69 if err := team.ValidatePublicationReceipt(d.Payload, *q.Receipt); err != nil {
70 return team.ReconcileResult{}, err
71 }
72 }
73 if found != nil {
74 before, _ := json.Marshal(found)
75 after, _ := json.Marshal(q.Receipt)
76 if string(before) != string(after) {
77 return team.ReconcileResult{}, team.ErrReceiptConflict
78 }
79 }
80 found = q.Receipt
81 }
82 return team.ReconcileResult{Receipt: found, Definitive: found != nil}, nil
83}
84func (e *Execution) recordVerifiedReceipt(ctx context.Context, tx *gorm.DB, a runnerAssignment, q RunnerRequest, receipt team.Receipt) error {
85 verified := q
86 verified.RequestID = "verified/" + q.RequestID
87 verified.Receipt = &receipt
88 if _, err := observeRunner(tx, a, "verified_receipt", verified, e.store.runnerClock.Now().UTC()); err != nil {
89 return err
90 }
91 return e.Core.Record(ctx, q.Scope, q.ActionID, receipt)
92}
93