| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 9 | "amadan.net/rastrillo/rastrillo/db" |
| 10 | "gorm.io/gorm" |
| 11 | ) |
| 12 | |
| 13 | type Execution struct { |
| 14 | lifetime *executionLifetime |
| 15 | Core *team.Store |
| 16 | store *Store |
| 17 | validators RunnerValidators |
| 18 | db *db.DB |
| 19 | } |
| 20 | |
| 21 | // AttachExecution is a startup operation, before serving requests. |
| 22 | func (s *Store) AttachExecution(ctx context.Context, path string, options team.Options) (*Execution, error) { |
| 23 | if s == nil || s.db == nil { |
| 24 | return nil, ErrUnavailable |
| 25 | } |
| 26 | if s.execution != nil { |
| 27 | return nil, ErrConflict |
| 28 | } |
| 29 | options.RenderedApproval = s.validateRenderedOwner |
| 30 | options.SourceReference = s.validateSourceReference |
| 31 | options.ObserveWrite = s.observeWrite |
| 32 | options.ObserverFailed = observerFailed |
| 33 | original := options.Provenance |
| 34 | options.Provenance = s.runnerProvenance(original) |
| 35 | core, err := team.OpenShared(ctx, s.db.Writer(), path, options) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | x := &Execution{Core: core, db: s.db, store: s} |
| 40 | if err := x.Write(ctx, func(tx *gorm.DB) error { |
| 41 | var instance Team |
| 42 | if err := tx.First(&instance).Error; err != nil { |
| 43 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 44 | return nil |
| 45 | } |
| 46 | return err |
| 47 | } |
| 48 | var row budgetRecord |
| 49 | if err := tx.Where("team_id = ?", instance.ID).First(&row).Error; err != nil { |
| 50 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 51 | return nil |
| 52 | } |
| 53 | return err |
| 54 | } |
| 55 | var config BudgetConfig |
| 56 | if err := json.Unmarshal([]byte(row.Config), &config); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if row.Revision != config.Revision { |
| 60 | return ErrUnavailable |
| 61 | } |
| 62 | raw, err := executionTx(tx) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | return core.SyncBudgetsTx(ctx, raw, team.AccountID(instance.ID), team.BudgetSet{Revision: config.Revision, Team: config.Team, Projects: config.Projects}) |
| 67 | }); err != nil { |
| 68 | core.Close() |
| 69 | return nil, err |
| 70 | } |
| 71 | s.execution = x |
| 72 | return x, nil |
| 73 | } |
| 74 | |
| 75 | // Write shares one commit with core SQL and audit records. The callback must |
| 76 | // use its GORM handle throughout and leave commit and rollback to the core. |
| 77 | func (e *Execution) Write(ctx context.Context, fn func(*gorm.DB) error) error { |
| 78 | if e == nil || e.Core == nil || e.db == nil { |
| 79 | return ErrUnavailable |
| 80 | } |
| 81 | return e.atomic(ctx, func(ctx context.Context, tx *gorm.DB) error { |
| 82 | if err := fn(tx); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | return e.syncExposure(ctx, tx) |
| 86 | }) |
| 87 | } |
| 88 | |